trinhhuy commited on
Commit
25d2fd4
·
verified ·
1 Parent(s): 486c58e
Files changed (1) hide show
  1. agent.py +41 -30
agent.py CHANGED
@@ -1,18 +1,26 @@
 
 
 
 
1
  print("agent: > thu vien")
2
  print("-------------")
3
  from langchain.agents import AgentExecutor, create_tool_calling_agent
4
- from langchain.memory import ConversationBufferMemory
5
- from langchain.prompts import MessagesPlaceholder
6
- from langchain.prompts import ChatPromptTemplate
7
  from tools import (model_gemini,
8
  get_weather,
9
  toeic_question)
10
-
11
 
12
  tools = [get_weather, toeic_question]
13
 
14
- # Pull the prompt template from the hub
15
- # prompt = hub.pull("hwchase17/openai-tools-agent")
 
 
 
 
 
16
 
17
  system = """Bạn là một AI có thể sử dụng các công cụ (tool) sau [get_weather, toeic_question]:
18
  1. toeic_question tool: Trả lời các câu hỏi liên quan đến bài thi TOEIC (không hỏi thêm thông tin ngoài lề).
@@ -28,37 +36,40 @@ Quy tắc:
28
  - Không đoán thông tin nếu không chắc, hãy nói rõ nếu bạn không biết.
29
  - Khi dùng tool, chỉ chọn đúng 1 tool phù hợp nhất với câu hỏi.
30
  """
31
- prompt = ChatPromptTemplate.from_messages(
32
- [
33
- ("system", system),
34
- MessagesPlaceholder("chat_history"), # thêm lịch sử trò chuyện
35
- ("human", "{input}"),
36
- MessagesPlaceholder("agent_scratchpad")
37
- ]
38
- )
39
- memory = ConversationBufferMemory(
40
- memory_key="chat_history", return_messages=True) #Tạo memory cho đoạn chat
41
 
42
- # Create the ReAct agent using the create_tool_calling_agent function
43
- # This function sets up an agent capable of calling tools based on the provided prompt.
 
 
 
 
 
44
  agent = create_tool_calling_agent(
45
- llm=model_gemini, # Language model to use
46
- tools=tools, # List of tools available to the agent
47
- prompt=prompt, # Prompt template to guide the agent's responses
48
  )
49
 
50
- # Create the agent executor
51
- agent_executor = AgentExecutor.from_agent_and_tools(
52
- agent=agent, # The agent to execute
53
- tools=tools, # List of tools available to the agent
54
- memory= memory,
55
- verbose=True, # Enable verbose logging
56
- handle_parsing_errors=True, # Handle parsing errors gracefully
57
  )
58
 
 
 
 
 
 
 
59
 
60
- def chat_with_agent(user_input: str):
61
- response = agent_executor.invoke({"input": user_input})
 
 
 
62
  return response["output"]
 
63
  print("agent: DONE")
64
  print("-------------")
 
1
+ import warnings
2
+ warnings.filterwarnings('ignore', category=FutureWarning)
3
+ warnings.filterwarnings('ignore', category=DeprecationWarning)
4
+
5
  print("agent: > thu vien")
6
  print("-------------")
7
  from langchain.agents import AgentExecutor, create_tool_calling_agent
8
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
9
+ from langchain_core.runnables.history import RunnableWithMessageHistory
10
+ from langchain_community.chat_message_histories import ChatMessageHistory
11
  from tools import (model_gemini,
12
  get_weather,
13
  toeic_question)
 
14
 
15
  tools = [get_weather, toeic_question]
16
 
17
+ # Store để lưu chat history
18
+ store = {}
19
+
20
+ def get_session_history(session_id: str):
21
+ if session_id not in store:
22
+ store[session_id] = ChatMessageHistory()
23
+ return store[session_id]
24
 
25
  system = """Bạn là một AI có thể sử dụng các công cụ (tool) sau [get_weather, toeic_question]:
26
  1. toeic_question tool: Trả lời các câu hỏi liên quan đến bài thi TOEIC (không hỏi thêm thông tin ngoài lề).
 
36
  - Không đoán thông tin nếu không chắc, hãy nói rõ nếu bạn không biết.
37
  - Khi dùng tool, chỉ chọn đúng 1 tool phù hợp nhất với câu hỏi.
38
  """
 
 
 
 
 
 
 
 
 
 
39
 
40
+ prompt = ChatPromptTemplate.from_messages([
41
+ ("system", system),
42
+ MessagesPlaceholder("chat_history"),
43
+ ("human", "{input}"),
44
+ MessagesPlaceholder("agent_scratchpad")
45
+ ])
46
+
47
  agent = create_tool_calling_agent(
48
+ llm=model_gemini,
49
+ tools=tools,
50
+ prompt=prompt,
51
  )
52
 
53
+ agent_executor = AgentExecutor(
54
+ agent=agent,
55
+ tools=tools,
56
+ verbose=True,
57
+ handle_parsing_errors=True,
 
 
58
  )
59
 
60
+ agent_with_chat_history = RunnableWithMessageHistory(
61
+ agent_executor,
62
+ get_session_history,
63
+ input_messages_key="input",
64
+ history_messages_key="chat_history",
65
+ )
66
 
67
+ def chat_with_agent(user_input: str, session_id: str = "default"):
68
+ response = agent_with_chat_history.invoke(
69
+ {"input": user_input},
70
+ config={"configurable": {"session_id": session_id}}
71
+ )
72
  return response["output"]
73
+
74
  print("agent: DONE")
75
  print("-------------")