Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,18 +1,11 @@
|
|
| 1 |
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.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
|
| 14 |
-
final_answer: str
|
| 15 |
-
|
| 16 |
# 1. Web Browsing
|
| 17 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 18 |
from langchain_community.document_loaders import ImageCaptionLoader
|
|
@@ -78,7 +71,7 @@ def image_caption(dir: str) -> str:
|
|
| 78 |
# ("human", f"Question: {question}\nReport to validate: {final_answer}")
|
| 79 |
class BasicAgent:
|
| 80 |
def __init__(self):
|
| 81 |
-
model = ChatGoogleGenerativeAI(
|
| 82 |
model="gemini-2.0-flash",
|
| 83 |
temperature=0,
|
| 84 |
max_tokens=1024,
|
|
@@ -87,15 +80,6 @@ class BasicAgent:
|
|
| 87 |
google_api_key="AIzaSyAxVUPaGJIgdxB46ZR0RWPKSjB9a63Z80o",
|
| 88 |
# other params...
|
| 89 |
)
|
| 90 |
-
# model = ChatAnthropic(
|
| 91 |
-
# model="claude-3-5-sonnet-20240620",
|
| 92 |
-
# temperature=0,
|
| 93 |
-
# max_tokens=20000,
|
| 94 |
-
# timeout=None,
|
| 95 |
-
# max_retries=2,
|
| 96 |
-
# api_key=os.getenv("ANTHROPIC_API_KEY"),
|
| 97 |
-
# # other params...
|
| 98 |
-
# )
|
| 99 |
# System Prompt for few shot prompting
|
| 100 |
self.sys_prompt = """"
|
| 101 |
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].
|
|
@@ -112,17 +96,12 @@ class BasicAgent:
|
|
| 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:
|
| 120 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 121 |
-
prompt_msg = [
|
| 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."
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import TypedDict, List, Dict, Any, Optional
|
|
|
|
|
|
|
| 3 |
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain_core.tools import tool
|
| 6 |
from langchain_core.messages import HumanMessage
|
| 7 |
from langchain_core.prompts import ChatPromptTemplate
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# 1. Web Browsing
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 11 |
from langchain_community.document_loaders import ImageCaptionLoader
|
|
|
|
| 71 |
# ("human", f"Question: {question}\nReport to validate: {final_answer}")
|
| 72 |
class BasicAgent:
|
| 73 |
def __init__(self):
|
| 74 |
+
self.model = ChatGoogleGenerativeAI(
|
| 75 |
model="gemini-2.0-flash",
|
| 76 |
temperature=0,
|
| 77 |
max_tokens=1024,
|
|
|
|
| 80 |
google_api_key="AIzaSyAxVUPaGJIgdxB46ZR0RWPKSjB9a63Z80o",
|
| 81 |
# other params...
|
| 82 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# System Prompt for few shot prompting
|
| 84 |
self.sys_prompt = """"
|
| 85 |
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].
|
|
|
|
| 96 |
("system", self.sys_prompt),
|
| 97 |
("human", "Question: {input}")
|
| 98 |
])
|
| 99 |
+
self.agent = create_tool_calling_agent(self.model, tools, prompt)
|
| 100 |
self.agent_exe = AgentExecutor(agent=self.agent, tools=tools, verbose=True)
|
| 101 |
print("BasicAgent initialized.")
|
| 102 |
|
| 103 |
def __call__(self, question: str) -> str:
|
| 104 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
response = self.agent_exe.invoke({"input": question})
|
| 106 |
fixed_answer = response['message'][-1].content
|
| 107 |
# fixed_answer = "This is a default answer."
|