Spaces:
Runtime error
Runtime error
Update app1.py
Browse files
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
|
| 9 |
-
|
| 10 |
-
from
|
| 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 |
-
builder.add_node(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
builder.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|