Update agent.py
Browse files
agent.py
CHANGED
|
@@ -24,9 +24,7 @@ class BasicAgent():
|
|
| 24 |
self.chat_with_tools = chat.bind_tools(self.tools)
|
| 25 |
self._initialize_graph()
|
| 26 |
self._initialize_telemetry()
|
| 27 |
-
print("BasicAgent initialized.")
|
| 28 |
|
| 29 |
-
|
| 30 |
def _initialize_graph(self):
|
| 31 |
builder = StateGraph(AgentState)
|
| 32 |
|
|
@@ -41,6 +39,7 @@ class BasicAgent():
|
|
| 41 |
|
| 42 |
# Compile the graph
|
| 43 |
self.agent = builder.compile()
|
|
|
|
| 44 |
|
| 45 |
def _initialize_telemetry(self):
|
| 46 |
LANGFUSE_PUBLIC_KEY = os.getenv("LANGFUSE_PUBLIC_KEY")
|
|
@@ -52,33 +51,32 @@ class BasicAgent():
|
|
| 52 |
secret_key=LANGFUSE_SECRET_KEY,
|
| 53 |
host=LANGFUSE_HOST
|
| 54 |
)
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
def __call__(self, question: str, file_name : str) -> str:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if file_name is not None and file_name!='':
|
| 60 |
-
|
|
|
|
| 61 |
else:
|
| 62 |
-
messages=[HumanMessage(content=question)]
|
|
|
|
| 63 |
response = self.agent.invoke({"messages":messages}, config={"callbacks": [self.langfuse_handler]})
|
| 64 |
answer = response['messages'][-1].content
|
| 65 |
print(f"Agent returning answer: {answer}")
|
| 66 |
return answer
|
| 67 |
|
| 68 |
-
|
| 69 |
def assistant(self, state: AgentState):
|
| 70 |
-
|
| 71 |
-
sys_msg = SystemMessage(content=f"""
|
| 72 |
-
You are a general AI assistant. I will ask you a question. Reason step by step and search for the information you need using available tools, one step at a time.
|
| 73 |
-
If you do not have enough information to answer, use the tools available to search for it.
|
| 74 |
-
Finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 75 |
-
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.
|
| 76 |
-
When providing the final answer, ONLY give [YOUR FINAL ANSWER]. Do not add anything else, no additional motivation or explanation, and do not return 'FINAL ANSWER:'.
|
| 77 |
-
""")
|
| 78 |
-
response = self.chat_with_tools.invoke([sys_msg] + state["messages"])
|
| 79 |
-
|
| 80 |
return {
|
| 81 |
"messages": state["messages"] + [response],
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 24 |
self.chat_with_tools = chat.bind_tools(self.tools)
|
| 25 |
self._initialize_graph()
|
| 26 |
self._initialize_telemetry()
|
|
|
|
| 27 |
|
|
|
|
| 28 |
def _initialize_graph(self):
|
| 29 |
builder = StateGraph(AgentState)
|
| 30 |
|
|
|
|
| 39 |
|
| 40 |
# Compile the graph
|
| 41 |
self.agent = builder.compile()
|
| 42 |
+
print("Agent initialized.")
|
| 43 |
|
| 44 |
def _initialize_telemetry(self):
|
| 45 |
LANGFUSE_PUBLIC_KEY = os.getenv("LANGFUSE_PUBLIC_KEY")
|
|
|
|
| 51 |
secret_key=LANGFUSE_SECRET_KEY,
|
| 52 |
host=LANGFUSE_HOST
|
| 53 |
)
|
| 54 |
+
print("Telemetry initialized.")
|
| 55 |
|
|
|
|
| 56 |
def __call__(self, question: str, file_name : str) -> str:
|
| 57 |
+
|
| 58 |
+
sys_msg = SystemMessage(content=f"""
|
| 59 |
+
You are a general AI assistant. I will ask you a question. Reason step by step and search for the information you need using available tools.
|
| 60 |
+
Finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 61 |
+
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.
|
| 62 |
+
When providing the final answer, ONLY give [YOUR FINAL ANSWER]. Do not add anything else, no additional motivation or explanation, and do not return 'FINAL ANSWER:'.
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
print(f"Agent received question: {question}.")
|
| 66 |
+
|
| 67 |
if file_name is not None and file_name!='':
|
| 68 |
+
print(f"Provided file: {file_name}.")
|
| 69 |
+
messages=[[sys_msg] + HumanMessage(content=f"{question}. The file you have access to is {file_name}.")]
|
| 70 |
else:
|
| 71 |
+
messages=[[sys_msg] + HumanMessage(content=question)]
|
| 72 |
+
|
| 73 |
response = self.agent.invoke({"messages":messages}, config={"callbacks": [self.langfuse_handler]})
|
| 74 |
answer = response['messages'][-1].content
|
| 75 |
print(f"Agent returning answer: {answer}")
|
| 76 |
return answer
|
| 77 |
|
|
|
|
| 78 |
def assistant(self, state: AgentState):
|
| 79 |
+
response = self.chat_with_tools.invoke(state["messages"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return {
|
| 81 |
"messages": state["messages"] + [response],
|
| 82 |
}
|
|
|
|
|
|