Spaces:
Sleeping
Sleeping
niikun
commited on
Commit
·
9826bc1
1
Parent(s):
fcdebb0
update tools
Browse files
app.py
CHANGED
|
@@ -28,42 +28,36 @@ class State(TypedDict):
|
|
| 28 |
class BasicAgent:
|
| 29 |
def __init__(self):
|
| 30 |
print("BasicAgent initialized.")
|
| 31 |
-
|
| 32 |
|
| 33 |
-
#
|
| 34 |
tool = TavilySearchResults(max_results=5)
|
| 35 |
-
tools = [tool]
|
| 36 |
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 37 |
-
llm_with_tools = llm.bind_tools(
|
| 38 |
|
| 39 |
-
#
|
| 40 |
def chatbot_node(state: State):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return {"messages": state["messages"] + [response_msg]}
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# ツールノードからチャットノードへ戻る
|
| 56 |
-
graph_builder.add_edge("tools", "chatbot")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
self.graph =
|
| 60 |
|
| 61 |
def __call__(self, question: str) -> str:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# 最後のメッセージを返す
|
| 66 |
-
return final_state["messages"][-1]
|
| 67 |
|
| 68 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 69 |
"""
|
|
|
|
| 28 |
class BasicAgent:
|
| 29 |
def __init__(self):
|
| 30 |
print("BasicAgent initialized.")
|
| 31 |
+
gb = StateGraph(State)
|
| 32 |
|
| 33 |
+
# 1) define LLM + tool
|
| 34 |
tool = TavilySearchResults(max_results=5)
|
|
|
|
| 35 |
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 36 |
+
llm_with_tools = llm.bind_tools([tool])
|
| 37 |
|
| 38 |
+
# 2) chatbot node
|
| 39 |
def chatbot_node(state: State):
|
| 40 |
+
resp = llm_with_tools.invoke(state["messages"])
|
| 41 |
+
return {"messages": state["messages"] + [resp]}
|
|
|
|
| 42 |
|
| 43 |
+
gb.add_node("chatbot", chatbot_node)
|
| 44 |
|
| 45 |
+
# 3) tool node must be named "tools"
|
| 46 |
+
tools_node = ToolNode(tools=[tool])
|
| 47 |
+
gb.add_node("tools", tools_node)
|
| 48 |
|
| 49 |
+
# 4) hook up the conditional edges
|
| 50 |
+
gb.add_conditional_edges("chatbot", tools_condition)
|
| 51 |
+
gb.add_edge(START, "chatbot")
|
| 52 |
+
gb.add_edge("tools", "chatbot")
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# compile once
|
| 55 |
+
self.graph = gb.compile()
|
| 56 |
|
| 57 |
def __call__(self, question: str) -> str:
|
| 58 |
+
state = {"messages": [question]}
|
| 59 |
+
out = self.graph(state)
|
| 60 |
+
return out["messages"][-1]
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 63 |
"""
|