Srv04 commited on
Commit
eb24686
·
verified ·
1 Parent(s): 2eb7674

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +60 -68
app1.py CHANGED
@@ -1,68 +1,60 @@
1
- from typing import TypedDict, Annotated
2
- from langgraph.graph.message import add_messages
3
- from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
4
- from langgraph.prebuilt import ToolNode
5
- from langgraph.graph import START, StateGraph
6
- from langgraph.prebuilt import tools_condition
7
- from langgraph.checkpoint.memory import InMemorySaver
8
- from langchain_ollama import ChatOllama
9
- from tool import DuckDuckGoSearchRun,web_search_tool,latest_news_tool, get_weather_tool as weather_info_tool, hub_stats_tool
10
- from retriever import guest_info_tool_1
11
-
12
-
13
-
14
-
15
- model = ChatOllama(
16
- model="qwen2.5:1.5b", # Or try other Ollama-supported models
17
- base_url="http://127.0.0.1:11434", # Default Ollama local server
18
- num_predict=256
19
- )
20
-
21
- tools=[weather_info_tool,web_search_tool,weather_info_tool,hub_stats_tool,guest_info_tool_1]
22
- model_with_tool = model.bind_tools(tools)
23
-
24
- class AgentState(TypedDict):
25
- messages: Annotated[list[AnyMessage],add_messages]
26
-
27
-
28
- def assistant(state: AgentState):
29
- return {
30
- "messages": [model_with_tool.invoke(state["messages"])],
31
- }
32
-
33
- builder = StateGraph(AgentState)
34
-
35
- builder.add_node("assistant",assistant)
36
- builder.add_node('tools',ToolNode(tools))
37
-
38
- builder.add_edge(START,'assistant')
39
- builder.add_conditional_edges(
40
- 'assistant',
41
- tools_condition
42
- )
43
- builder.add_edge('tools','assistant')
44
-
45
- checkpointer = InMemorySaver()
46
-
47
- alfred = builder.compile(checkpointer=checkpointer)
48
- thread_config = {"configurable": {"thread_id": "1"}}
49
-
50
- print("🎩 Alfred: Hello, I am Alfred. How can I assist you today?")
51
- print("Type 'exit' or 'quit' to stop.\n")
52
-
53
- while True:
54
- user_input = input("You: ")
55
-
56
- if user_input.lower() in ["exit", "quit"]:
57
- print("Alfred: Goodbye.")
58
- break
59
-
60
- response = alfred.invoke(
61
- {"messages": [HumanMessage(content=user_input)]},
62
- thread_config
63
- )
64
-
65
- ai_reply = response["messages"][-1].content
66
-
67
- print("\n🎩 Alfred:", ai_reply)
68
- print("-" * 40)
 
1
+ from typing import TypedDict, Annotated
2
+ from langgraph.graph.message import add_messages
3
+ from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
4
+ from langgraph.prebuilt import ToolNode
5
+ from langgraph.graph import START, StateGraph
6
+ from langgraph.prebuilt import tools_condition
7
+ from langgraph.checkpoint.memory import InMemorySaver
8
+ from tool import DuckDuckGoSearchRun,web_search_tool,latest_news_tool, get_weather_tool as weather_info_tool, hub_stats_tool
9
+
10
+ from langchain_huggingface import HuggingFaceEndpoint
11
+
12
+
13
+
14
+ model = HuggingFaceEndpoint(
15
+ repo_id="Qwen/Qwen2.5-7B-Instruct",
16
+ task="text-generation",
17
+ max_new_tokens=256
18
+ )
19
+
20
+ tools=[weather_info_tool,web_search_tool,weather_info_tool,hub_stats_tool]
21
+ model_with_tool = model.bind_tools(tools)
22
+
23
+ class AgentState(TypedDict):
24
+ messages: Annotated[list[AnyMessage],add_messages]
25
+
26
+
27
+ def assistant(state: AgentState):
28
+ return {
29
+ "messages": [model_with_tool.invoke(state["messages"])],
30
+ }
31
+
32
+ builder = StateGraph(AgentState)
33
+
34
+ builder.add_node("assistant",assistant)
35
+ builder.add_node('tools',ToolNode(tools))
36
+
37
+ builder.add_edge(START,'assistant')
38
+ builder.add_conditional_edges(
39
+ 'assistant',
40
+ tools_condition
41
+ )
42
+ builder.add_edge('tools','assistant')
43
+
44
+ checkpointer = InMemorySaver()
45
+
46
+ alfred = builder.compile(checkpointer=checkpointer)
47
+ thread_config = {"configurable": {"thread_id": "1"}}
48
+
49
+
50
+
51
+ def run_agent(question: str):
52
+
53
+ response = alfred.invoke(
54
+ {"messages":[HumanMessage(content=question)]},
55
+ {"configurable":{"thread_id":"evaluation"}}
56
+ )
57
+
58
+ answer = response["messages"][-1].content
59
+
60
+ return answer