Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -2,17 +2,12 @@ import os
|
|
| 2 |
from typing import TypedDict, List, Dict, Any, Optional
|
| 3 |
from langgraph.prebuilt import create_react_agent
|
| 4 |
from langgraph.graph import StateGraph, START, END
|
|
|
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
from langchain_core.tools import tool
|
| 7 |
from langchain_core.messages import HumanMessage
|
| 8 |
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
|
| 10 |
-
# %pip install -qU duckduckgo-search langchain-community
|
| 11 |
-
# pip install requests
|
| 12 |
-
# pip install pandas
|
| 13 |
-
# pip install pypdf
|
| 14 |
-
|
| 15 |
-
|
| 16 |
class AgentState(TypedDict):
|
| 17 |
messages: List
|
| 18 |
current_question: str
|
|
@@ -112,10 +107,13 @@ class BasicAgent:
|
|
| 112 |
There are few tools provided: web_search, visit_webpage, read_file and image_caption.
|
| 113 |
Here are few examples demonstrating how to call and use the tools.
|
| 114 |
"""
|
| 115 |
-
self.app = self.__graph_compile__()
|
| 116 |
tools = [web_search, visit_webpage, read_file, image_caption]
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
print("BasicAgent initialized.")
|
| 120 |
|
| 121 |
def __call__(self, question: str) -> str:
|
|
@@ -124,14 +122,9 @@ class BasicAgent:
|
|
| 124 |
("system", self.sys_prompt),
|
| 125 |
("human", f"Question: {question}")
|
| 126 |
]
|
| 127 |
-
response = self.model.invoke(prompt_msg)
|
|
|
|
| 128 |
fixed_answer = response['message'][-1].content
|
| 129 |
# fixed_answer = "This is a default answer."
|
| 130 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 131 |
-
return fixed_answer
|
| 132 |
-
|
| 133 |
-
# Maybe we no need this one
|
| 134 |
-
def __graph_compile__(self):
|
| 135 |
-
graph = StateGraph(AgentState)
|
| 136 |
-
|
| 137 |
-
pass
|
|
|
|
| 2 |
from typing import TypedDict, List, Dict, Any, Optional
|
| 3 |
from langgraph.prebuilt import create_react_agent
|
| 4 |
from langgraph.graph import StateGraph, START, END
|
| 5 |
+
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
| 6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
from langchain_core.tools import tool
|
| 8 |
from langchain_core.messages import HumanMessage
|
| 9 |
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class AgentState(TypedDict):
|
| 12 |
messages: List
|
| 13 |
current_question: str
|
|
|
|
| 107 |
There are few tools provided: web_search, visit_webpage, read_file and image_caption.
|
| 108 |
Here are few examples demonstrating how to call and use the tools.
|
| 109 |
"""
|
|
|
|
| 110 |
tools = [web_search, visit_webpage, read_file, image_caption]
|
| 111 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 112 |
+
("system", self.sys_prompt),
|
| 113 |
+
("human", "Question: {input}")
|
| 114 |
+
])
|
| 115 |
+
self.agent = create_tool_calling_agent(model, tools, prompt)
|
| 116 |
+
self.agent_exe = AgentExecutor(agent=self.agent, tools=tools, verbose=True)
|
| 117 |
print("BasicAgent initialized.")
|
| 118 |
|
| 119 |
def __call__(self, question: str) -> str:
|
|
|
|
| 122 |
("system", self.sys_prompt),
|
| 123 |
("human", f"Question: {question}")
|
| 124 |
]
|
| 125 |
+
# response = self.model.invoke(prompt_msg)
|
| 126 |
+
response = self.agent_exe.invoke({"input": question})
|
| 127 |
fixed_answer = response['message'][-1].content
|
| 128 |
# fixed_answer = "This is a default answer."
|
| 129 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 130 |
+
return fixed_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|