Spaces:
Sleeping
Sleeping
update 1
Browse files
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
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
from tools import (model_gemini,
|
| 8 |
get_weather,
|
| 9 |
toeic_question)
|
| 10 |
-
|
| 11 |
|
| 12 |
tools = [get_weather, toeic_question]
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
agent = create_tool_calling_agent(
|
| 45 |
-
llm=model_gemini,
|
| 46 |
-
tools=tools,
|
| 47 |
-
prompt=prompt,
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 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 =
|
|
|
|
|
|
|
|
|
|
| 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("-------------")
|