sqfoo's picture
Upload two files: agent.py and run.py [remove the agent class]
03cd67b verified
raw
history blame
4.47 kB
import os
from typing import TypedDict, List, Dict, Any, Optional
from langgraph.graph import StateGraph, START, END
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate
# %pip install -qU duckduckgo-search langchain-community
# pip install requests
# pip install pandas
# pip install pypdf
class AgentState(TypedDict):
messages: List
current_question: str
final_answer: str
# 1. Web Browsing
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.document_loaders import ImageCaptionLoader
import requests
import pandas as pd
from pypdf import PdfReader
@tool
def web_search(query: str) -> str:
"""Allows search through DuckDuckGo.
Args:
query: what you want to search
"""
search = DuckDuckGoSearchRun()
results = search.invoke(query)
return "\n".join(results)
@tool
def visit_webpage(url: str) -> str:
"""Fetches raw HTML content of a web page.
Args:
url: the webpage url
"""
try:
response = requests.get(url, timeout=5)
return response.text
except Exception as e:
return f"[ERROR fetching {url}]: {str(e)}"
# 4. File Reading
@tool
def read_file(dir: str) -> str:
"""Read the content of the provided file
Args:
dir: the filepath
"""
extension = dir.split['.'][-1]
if extension == 'xlsx':
dataframe = pd.read_excel(dir)
return dataframe.to_string()
elif extension == 'pdf':
reader = PdfReader(dir)
contents = [p.extract_text() for p in reader.pages]
return "\n".join(contents)
else:
with open(dir) as f:
return f.read()
# 5. Image Open
@tool
def image_caption(dir: str) -> str:
"""Understand the content of the provided image
Args:
dir: the image url link
"""
loader = ImageCaptionLoader(images=[dir])
metadata = loader.load()
return metadata[0].page_content
# 2. Coding
# 3. Multi-Modality
# ("human", f"Question: {question}\nReport to validate: {final_answer}")
class BasicAgent:
def __init__(self):
model = ChatAnthropic(
model="claude-3-5-sonnet-20240620",
temperature=0,
max_tokens=20000,
timeout=None,
max_retries=2,
api_key=os.getenv("ANTHROPIC_API_KEY"),
# other params...
)
# System Prompt for few shot prompting
self.sys_prompt = """"
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 separared 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 (eg. 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 put in the list is a number or a string.
There are few tools provided: web_search, visit_webpage, read_file and image_caption.
Here are few examples demonstrating how to call and use the tools.
"""
self.app = self.__graph_compile__()
tools = [web_search, visit_webpage, read_file, image_caption]
self.model = model.bind_tools(tools) # LLM with tools
# self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model)
print("BasicAgent initialized.")
def __call__(self, question: str) -> str:
print(f"Agent received question (first 50 chars): {question[:50]}...")
prompt_msg = [
("system", self.sys_prompt),
("human", f"Question: {question}")
]
response = self.model.invoke(prompt_msg)
fixed_answer = response.content
# fixed_answer = "This is a default answer."
print(f"Agent returning fixed answer: {fixed_answer}")
return fixed_answer
# Maybe we no need this one
def __graph_compile__(self):
graph = StateGraph(AgentState)
pass