Try to rely only ad langgraph
Browse files
app.py
CHANGED
|
@@ -64,60 +64,98 @@ class BasicAgent:
|
|
| 64 |
print(f"Agent returning fixed answer: {final_answer}")
|
| 65 |
return final_answer, result["messages"]
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def assistant(self, state: State):
|
| 68 |
print("Assistant call state:", state)
|
| 69 |
messages = state.get("messages", [])
|
| 70 |
-
|
| 71 |
# Add system message only once
|
| 72 |
if not any(isinstance(m, SystemMessage) for m in messages):
|
| 73 |
-
textual_description_of_tool="""
|
| 74 |
search_tool(question: str, max_length: int = 2048) -> str:
|
| 75 |
-
Search info
|
| 76 |
Call example:
|
| 77 |
-
|
| 78 |
self.search_tool("Who won the election 2008 in the USA?", max_length = 4096)
|
| 79 |
-
|
| 80 |
Args:
|
| 81 |
question: Question string
|
| 82 |
-
max_length: maximum chars in the output (if exceeded the first "max_length" characters will be taken)
|
| 83 |
-
|
| 84 |
Returns:
|
| 85 |
A single string containing the info from the web.
|
| 86 |
-
|
| 87 |
"""
|
| 88 |
|
| 89 |
sys_msg = SystemMessage(
|
| 90 |
-
content=f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending
|
| 91 |
-
|
| 92 |
)
|
| 93 |
messages = [sys_msg] + messages
|
| 94 |
|
| 95 |
print("Calling model with tools invoke...")
|
| 96 |
new_msg = self.model_with_tools.invoke(messages)
|
| 97 |
print("Calling model with tools invoke finished, result:", new_msg)
|
| 98 |
-
|
| 99 |
-
# Detect tool call
|
| 100 |
-
if "tool_calls" in new_msg.additional_kwargs:
|
| 101 |
-
tool_call = new_msg.additional_kwargs["tool_calls"][0]
|
| 102 |
-
tool_response = self.search_tool(**eval(tool_call["function"]["arguments"]))
|
| 103 |
-
|
| 104 |
-
tool_msg = ToolMessage(
|
| 105 |
-
tool_call_id=tool_call["id"],
|
| 106 |
-
content=tool_response,
|
| 107 |
-
name=tool_call["function"]["name"]
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
return {
|
| 111 |
-
"question": state["question"],
|
| 112 |
-
"messages": messages + [new_msg, tool_msg],
|
| 113 |
-
}
|
| 114 |
|
| 115 |
-
#
|
| 116 |
return {
|
| 117 |
"question": state["question"],
|
| 118 |
-
"messages": messages
|
| 119 |
}
|
| 120 |
|
|
|
|
| 121 |
def search_tool(self, question: str, max_length: int = 2048) -> str:
|
| 122 |
"""
|
| 123 |
Search info in the web.
|
|
|
|
| 64 |
print(f"Agent returning fixed answer: {final_answer}")
|
| 65 |
return final_answer, result["messages"]
|
| 66 |
|
| 67 |
+
# def assistant(self, state: State):
|
| 68 |
+
# print("Assistant call state:", state)
|
| 69 |
+
# messages = state.get("messages", [])
|
| 70 |
+
|
| 71 |
+
# # Add system message only once
|
| 72 |
+
# if not any(isinstance(m, SystemMessage) for m in messages):
|
| 73 |
+
# textual_description_of_tool="""
|
| 74 |
+
# search_tool(question: str, max_length: int = 2048) -> str:
|
| 75 |
+
# Search info in the web.
|
| 76 |
+
# Call example:
|
| 77 |
+
|
| 78 |
+
# self.search_tool("Who won the election 2008 in the USA?", max_length = 4096)
|
| 79 |
+
|
| 80 |
+
# Args:
|
| 81 |
+
# question: Question string
|
| 82 |
+
# max_length: maximum chars in the output (if exceeded the first "max_length" characters will be taken)
|
| 83 |
+
|
| 84 |
+
# Returns:
|
| 85 |
+
# A single string containing the info from the web.
|
| 86 |
+
|
| 87 |
+
# """
|
| 88 |
+
|
| 89 |
+
# sys_msg = SystemMessage(
|
| 90 |
+
# content=f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 91 |
+
# You can use provided tools:\n{textual_description_of_tool}"""
|
| 92 |
+
# )
|
| 93 |
+
# messages = [sys_msg] + messages
|
| 94 |
+
|
| 95 |
+
# print("Calling model with tools invoke...")
|
| 96 |
+
# new_msg = self.model_with_tools.invoke(messages)
|
| 97 |
+
# print("Calling model with tools invoke finished, result:", new_msg)
|
| 98 |
+
|
| 99 |
+
# # Detect tool call
|
| 100 |
+
# if "tool_calls" in new_msg.additional_kwargs:
|
| 101 |
+
# tool_call = new_msg.additional_kwargs["tool_calls"][0]
|
| 102 |
+
# tool_response = self.search_tool(**eval(tool_call["function"]["arguments"]))
|
| 103 |
+
|
| 104 |
+
# tool_msg = ToolMessage(
|
| 105 |
+
# tool_call_id=tool_call["id"],
|
| 106 |
+
# content=tool_response,
|
| 107 |
+
# name=tool_call["function"]["name"]
|
| 108 |
+
# )
|
| 109 |
+
|
| 110 |
+
# return {
|
| 111 |
+
# "question": state["question"],
|
| 112 |
+
# "messages": messages + [new_msg, tool_msg],
|
| 113 |
+
# }
|
| 114 |
+
|
| 115 |
+
# # new_messages = add_messages(messages, [new_msg])
|
| 116 |
+
# return {
|
| 117 |
+
# "question": state["question"],
|
| 118 |
+
# "messages": messages + [new_msg],
|
| 119 |
+
# }
|
| 120 |
+
|
| 121 |
def assistant(self, state: State):
|
| 122 |
print("Assistant call state:", state)
|
| 123 |
messages = state.get("messages", [])
|
| 124 |
+
|
| 125 |
# Add system message only once
|
| 126 |
if not any(isinstance(m, SystemMessage) for m in messages):
|
| 127 |
+
textual_description_of_tool = """
|
| 128 |
search_tool(question: str, max_length: int = 2048) -> str:
|
| 129 |
+
Search info on the web.
|
| 130 |
Call example:
|
| 131 |
+
|
| 132 |
self.search_tool("Who won the election 2008 in the USA?", max_length = 4096)
|
| 133 |
+
|
| 134 |
Args:
|
| 135 |
question: Question string
|
| 136 |
+
max_length: maximum chars in the output (if exceeded, the first "max_length" characters will be taken)
|
| 137 |
+
|
| 138 |
Returns:
|
| 139 |
A single string containing the info from the web.
|
|
|
|
| 140 |
"""
|
| 141 |
|
| 142 |
sys_msg = SystemMessage(
|
| 143 |
+
content=f"""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
|
| 144 |
+
You can use provided tools:\n{textual_description_of_tool}"""
|
| 145 |
)
|
| 146 |
messages = [sys_msg] + messages
|
| 147 |
|
| 148 |
print("Calling model with tools invoke...")
|
| 149 |
new_msg = self.model_with_tools.invoke(messages)
|
| 150 |
print("Calling model with tools invoke finished, result:", new_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# Let LangGraph handle tool routing
|
| 153 |
return {
|
| 154 |
"question": state["question"],
|
| 155 |
+
"messages": add_messages(messages, [new_msg]),
|
| 156 |
}
|
| 157 |
|
| 158 |
+
|
| 159 |
def search_tool(self, question: str, max_length: int = 2048) -> str:
|
| 160 |
"""
|
| 161 |
Search info in the web.
|