Update agent.py
Browse files
agent.py
CHANGED
|
@@ -12,6 +12,7 @@ from typing import TypedDict,Annotated
|
|
| 12 |
|
| 13 |
class AgentState(TypedDict):
|
| 14 |
messages: Annotated[list[AnyMessage], add_messages]
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
class BasicAgent():
|
|
@@ -41,26 +42,41 @@ class BasicAgent():
|
|
| 41 |
# Compile the graph
|
| 42 |
self.agent = builder.compile()
|
| 43 |
|
|
|
|
| 44 |
def __call__(self, question: str) -> str:
|
| 45 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 46 |
-
|
| 47 |
-
When providing the final answer, be as concise as possible, provide only the FINAL ANSWER. For example:
|
| 48 |
-
QUESTION: What was the actual enrollment count of the clinical trial on H. pylori in acne vulgaris patients from Jan-May 2018 as listed on the NIH website?
|
| 49 |
-
FINAL ANSWER: 90
|
| 50 |
-
|
| 51 |
-
Now:
|
| 52 |
-
QUESTION: {question}
|
| 53 |
-
FINAL ANSWER: """
|
| 54 |
-
|
| 55 |
-
messages=[HumanMessage(content=agent_prompt)]
|
| 56 |
response = self.agent.invoke({"messages":messages})
|
| 57 |
answer = response['messages'][-1].content
|
| 58 |
print(f"Agent returning answer: {answer}")
|
| 59 |
return answer
|
| 60 |
|
|
|
|
| 61 |
def assistant(self, state: AgentState):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return {
|
| 63 |
-
"messages": [self.chat_with_tools.invoke(state["messages"])],
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
|
|
|
|
| 12 |
|
| 13 |
class AgentState(TypedDict):
|
| 14 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 15 |
+
input_file: Optional[str] # Contains file path (PDF/PNG)
|
| 16 |
|
| 17 |
|
| 18 |
class BasicAgent():
|
|
|
|
| 42 |
# Compile the graph
|
| 43 |
self.agent = builder.compile()
|
| 44 |
|
| 45 |
+
|
| 46 |
def __call__(self, question: str) -> str:
|
| 47 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 48 |
+
messages=[HumanMessage(content=question)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
response = self.agent.invoke({"messages":messages})
|
| 50 |
answer = response['messages'][-1].content
|
| 51 |
print(f"Agent returning answer: {answer}")
|
| 52 |
return answer
|
| 53 |
|
| 54 |
+
|
| 55 |
def assistant(self, state: AgentState):
|
| 56 |
+
image = state["input_file"]
|
| 57 |
+
textual_description_of_tool="""
|
| 58 |
+
extract_text(img_path: str) -> str:
|
| 59 |
+
Extract text from an image file using a multimodal model.
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
img_path: A local image file path (strings).
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
A single string containing the concatenated text extracted from each image.
|
| 66 |
+
search_tool(query: str) -> str:
|
| 67 |
+
Search the web using the DuckDuckGoSearchRun to perform a search query and return a summarized textual result.
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
query: A string representing the search query.
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
A single string containing the search result or summary.
|
| 74 |
+
"""
|
| 75 |
+
sys_msg = SystemMessage(content=f"You are a helpful assistant. You have access to the following tools:\n{textual_description_of_tool} \n You may have access to some optional images. Currently the loaded image is: {image}. \n When providing the final answer, be as concise as possible, provide only the FINAL ANSWER. \n For example: QUESTION: What was the actual enrollment count of the clinical trial on H. pylori in acne vulgaris patients from Jan-May 2018 as listed on the NIH website? \n FINAL ANSWER: 90 \n")
|
| 76 |
+
|
| 77 |
return {
|
| 78 |
+
"messages": [self.chat_with_tools.invoke([sys_msg] + state["messages"])],
|
| 79 |
+
"input_file": image
|
| 80 |
}
|
| 81 |
|
| 82 |
|