Spaces:
Runtime error
Runtime error
File size: 2,962 Bytes
bbd26e0 81c1bae bbd26e0 196b889 bbd26e0 4db7b5c d3cef9d 6f8eb35 bbd26e0 81c1bae 073b7ff bbd26e0 a45af70 1ee047b 18315a4 f41d3f9 acf4155 81c1bae a45af70 81c1bae b3534d9 d8efcec b3534d9 847196d 196b889 b3534d9 bbd26e0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 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()
|