Final_Assignment_Template / real_agent.py
tu101's picture
s
196b889 verified
Raw History Blame Contribute Delete
2.96 kB
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage, SystemMessage
from langgraph.prebuilt import ToolNode
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import tools_condition
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
from langchain_openai import ChatOpenAI
import os
from tools import hub_stats_tool, extract_image_text, add, divide, multiply, subtract, calculate_cargo_travel_time, web_search_tool, wiki_search_tool, arvix_search_tool, excel_search_tool, audio_search_tool, YT_search_tool
from retriever import guest_info_tool
import gradio as gr
from langfuse.langchain import CallbackHandler
from smolagents import VisitWebpageTool, GoogleSearchTool, PythonInterpreterTool
import json
from langchain.schema import Document
# load the system prompt from the file
with open("system_prompt.txt", "r", encoding="utf-8") as f:
system_prompt = f.read()
# System message
os.environ["OPENAI_API_KEY"] = "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"
os.environ["SERPAPI_API_KEY"] = "82491710ce6c6aa2d767d7796023a5ca11aa182fcda2a703cd44aea8d6da4489"
os.environ["TAVILY_API_KEY"] = "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"
def call_build():
# Initialize the web search tool
# search_tool = GoogleSearchTool(provider = "serpapi")
# search_tool.name = "GoogleSearchTool"
# visit_webpage_tool = VisitWebpageTool()
# visit_webpage_tool.name = "VisitWebpageTool"
# Generate the chat interface, including the tools
chat = ChatOpenAI(model="gpt-4o")
tools = [guest_info_tool, hub_stats_tool, excel_search_tool, audio_search_tool, extract_image_text, add, divide, multiply, subtract,
calculate_cargo_travel_time, web_search_tool, wiki_search_tool, arvix_search_tool, YT_search_tool]
chat_with_tools = chat.bind_tools(tools)
# Generate the AgentState and Agent graph
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
def assistant(state: AgentState):
return {
"messages": [chat_with_tools.invoke(state["messages"])],
}
## The graph
builder = StateGraph(AgentState)
# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message requires a tool, route to tools
# Otherwise, provide a direct response
tools_condition,
)
builder.add_edge("tools", "assistant")
return builder.compile()