ancirius commited on
Commit
ff9eabf
·
verified ·
1 Parent(s): 877d0b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -15
app.py CHANGED
@@ -1,13 +1,49 @@
1
  import gradio as gr
2
  import random
3
- from smolagents import GradioUI, CodeAgent, HfApiModel
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Import our custom tools from their modules
6
- from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
7
- from retriever import load_guest_dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # Initialize the Hugging Face model
10
- model = HfApiModel()
11
 
12
  # Initialize the web search tool
13
  search_tool = DuckDuckGoSearchTool()
@@ -18,16 +54,8 @@ weather_info_tool = WeatherInfoTool()
18
  # Initialize the Hub stats tool
19
  hub_stats_tool = HubStatsTool()
20
 
21
- # Load the guest dataset and initialize the guest info tool
22
- guest_info_tool = load_guest_dataset()
23
-
24
  # Create Alfred with all the tools
25
- alfred = CodeAgent(
26
- tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
27
- model=model,
28
- add_base_tools=True, # Add any additional base tools
29
- planning_interval=3 # Enable planning every 3 steps
30
- )
31
 
32
  if __name__ == "__main__":
33
  GradioUI(alfred).launch()
 
1
  import gradio as gr
2
  import random
3
+ from typing import TypedDict, Annotated
4
+ from langgraph.graph.message import add_messages
5
+ from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
6
+ from langgraph.prebuilt import ToolNode
7
+ from langgraph.graph import START, StateGraph
8
+ from langgraph.prebuilt import tools_condition
9
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
10
+
11
+ # Generate the chat interface, including the tools
12
+ llm = HuggingFaceEndpoint(
13
+ repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
14
+ huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
15
+ )
16
 
17
+ chat = ChatHuggingFace(llm=llm, verbose=True)
18
+ tools = [guest_info_tool]
19
+ chat_with_tools = chat.bind_tools(tools)
20
+
21
+ # Generate the AgentState and Agent graph
22
+ class AgentState(TypedDict):
23
+ messages: Annotated[list[AnyMessage], add_messages]
24
+
25
+ def assistant(state: AgentState):
26
+ return {
27
+ "messages": [chat_with_tools.invoke(state["messages"])],
28
+ }
29
+
30
+ ## The graph
31
+ builder = StateGraph(AgentState)
32
+
33
+ # Define nodes: these do the work
34
+ builder.add_node("assistant", assistant)
35
+ builder.add_node("tools", ToolNode(tools))
36
+
37
+ # Define edges: these determine how the control flow moves
38
+ builder.add_edge(START, "assistant")
39
+ builder.add_conditional_edges(
40
+ "assistant",
41
+ # If the latest message requires a tool, route to tools
42
+ # Otherwise, provide a direct response
43
+ tools_condition,
44
+ )
45
+ builder.add_edge("tools", "assistant")
46
 
 
 
47
 
48
  # Initialize the web search tool
49
  search_tool = DuckDuckGoSearchTool()
 
54
  # Initialize the Hub stats tool
55
  hub_stats_tool = HubStatsTool()
56
 
 
 
 
57
  # Create Alfred with all the tools
58
+ alfred = builder.compile()
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
  GradioUI(alfred).launch()