Spaces:
Runtime error
Runtime error
Commit
·
f31a918
0
Parent(s):
fix
Browse files- .gitattributes +35 -0
- README.md +14 -0
- __pycache__/agent.cpython-310.pyc +0 -0
- agent.py +160 -0
- app.py +107 -0
- requirements.txt +13 -0
- system_prompt.txt +168 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: DIY Assistant
|
3 |
+
emoji: 💬
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.0.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
short_description: DIY assistant
|
12 |
+
---
|
13 |
+
|
14 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
__pycache__/agent.cpython-310.pyc
ADDED
Binary file (3.57 kB). View file
|
|
agent.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
# import pandas as pd
|
3 |
+
|
4 |
+
##! langchain core libraries ##
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
7 |
+
from langgraph.prebuilt import tools_condition
|
8 |
+
from langgraph.prebuilt import ToolNode
|
9 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
10 |
+
from langchain_core.tools import tool
|
11 |
+
from langchain.tools.retriever import create_retriever_tool
|
12 |
+
|
13 |
+
##! langchain model calling libraries ##
|
14 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
15 |
+
from langchain_groq import ChatGroq
|
16 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings, HuggingFacePipeline
|
17 |
+
|
18 |
+
load_dotenv()
|
19 |
+
|
20 |
+
@tool
|
21 |
+
def multiply(a: int, b: int) -> int:
|
22 |
+
"""Multiply two numbers.
|
23 |
+
Args:
|
24 |
+
a: first int
|
25 |
+
b: second int
|
26 |
+
"""
|
27 |
+
return a * b
|
28 |
+
|
29 |
+
@tool
|
30 |
+
def add(a: int, b: int) -> int:
|
31 |
+
"""Add two numbers.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
a: first int
|
35 |
+
b: second int
|
36 |
+
"""
|
37 |
+
return a + b
|
38 |
+
|
39 |
+
@tool
|
40 |
+
def subtract(a: int, b: int) -> int:
|
41 |
+
"""Subtract two numbers.
|
42 |
+
|
43 |
+
Args:
|
44 |
+
a: first int
|
45 |
+
b: second int
|
46 |
+
"""
|
47 |
+
return a - b
|
48 |
+
|
49 |
+
@tool
|
50 |
+
def divide(a: int, b: int) -> int:
|
51 |
+
"""Divide two numbers.
|
52 |
+
|
53 |
+
Args:
|
54 |
+
a: first int
|
55 |
+
b: second int
|
56 |
+
"""
|
57 |
+
if b == 0:
|
58 |
+
raise ValueError("Cannot divide by zero.")
|
59 |
+
return a / b
|
60 |
+
|
61 |
+
@tool
|
62 |
+
def modulus(a: int, b: int) -> int:
|
63 |
+
"""Get the modulus of two numbers.
|
64 |
+
|
65 |
+
Args:
|
66 |
+
a: first int
|
67 |
+
b: second int
|
68 |
+
"""
|
69 |
+
return a % b
|
70 |
+
|
71 |
+
|
72 |
+
# load the system prompt from the file
|
73 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
74 |
+
system_prompt = f.read()
|
75 |
+
|
76 |
+
# System message
|
77 |
+
sys_msg = SystemMessage(content=system_prompt)
|
78 |
+
|
79 |
+
tools = [
|
80 |
+
multiply,
|
81 |
+
add,
|
82 |
+
subtract,
|
83 |
+
divide,
|
84 |
+
modulus,
|
85 |
+
]
|
86 |
+
|
87 |
+
hf_token = os.environ.get('HF_TOKEN')
|
88 |
+
if not hf_token:
|
89 |
+
raise ValueError("Hugging Face API token (HF_TOKEN) not found in environment variables.")
|
90 |
+
|
91 |
+
# Build graph function
|
92 |
+
def build_graph(provider: str = "huggingface"):
|
93 |
+
|
94 |
+
"""Build the graph"""
|
95 |
+
# Load environment variables from .env file
|
96 |
+
if provider == "google":
|
97 |
+
# Google Gemini
|
98 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
99 |
+
elif provider == "groq":
|
100 |
+
# Groq https://console.groq.com/docs/models
|
101 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0) # optional : qwen-qwq-32b gemma2-9b-it
|
102 |
+
elif provider == "huggingface":
|
103 |
+
# repo_id = "togethercomputer/evo-1-131k-base"
|
104 |
+
# repo_id="HuggingFaceH4/zephyr-7b-beta",
|
105 |
+
# repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
106 |
+
|
107 |
+
if not hf_token:
|
108 |
+
raise ValueError("HF_TOKEN environment variable not set. It's required for Hugging Face provider.")
|
109 |
+
# llm = HuggingFaceEndpoint(
|
110 |
+
# repo_id="HuggingFaceH4/zephyr-7b-beta",
|
111 |
+
# provider="auto",
|
112 |
+
# task="text-generation",
|
113 |
+
# max_new_tokens=1000,
|
114 |
+
# do_sample=False,
|
115 |
+
# repetition_penalty=1.03,
|
116 |
+
# )
|
117 |
+
|
118 |
+
# llm = ChatHuggingFace(llm=llm)
|
119 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
120 |
+
else:
|
121 |
+
raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")
|
122 |
+
# Bind tools to LLM
|
123 |
+
"""Build the graph"""
|
124 |
+
|
125 |
+
|
126 |
+
llm_with_tools = llm.bind_tools(tools)
|
127 |
+
|
128 |
+
# Node
|
129 |
+
def assistant(state: MessagesState):
|
130 |
+
print("\n--- Assistant Node ---")
|
131 |
+
print("Incoming messages to assistant:")
|
132 |
+
for msg in state["messages"]:
|
133 |
+
msg.pretty_print() #
|
134 |
+
|
135 |
+
"""Assistant node"""
|
136 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
137 |
+
|
138 |
+
# def retriever(state: MessagesState):
|
139 |
+
# """Retriever node"""
|
140 |
+
# similar_question = vector_store.similarity_search(state["messages"][0].content)
|
141 |
+
# example_msg = HumanMessage(
|
142 |
+
# content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
143 |
+
# )
|
144 |
+
# print("ex msgs"+[sys_msg] + state["messages"] + [example_msg])
|
145 |
+
# return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
146 |
+
|
147 |
+
builder = StateGraph(MessagesState)
|
148 |
+
builder.add_node("assistant", assistant)
|
149 |
+
builder.add_node("tools", ToolNode(tools))
|
150 |
+
builder.add_edge(START, "assistant")
|
151 |
+
builder.add_conditional_edges(
|
152 |
+
"assistant",
|
153 |
+
tools_condition,
|
154 |
+
)
|
155 |
+
builder.add_edge("tools", "assistant")
|
156 |
+
|
157 |
+
# Compile graph
|
158 |
+
compiled_graph = builder.compile() # This line should already be there or be the next line
|
159 |
+
|
160 |
+
return compiled_graph
|
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from langchain_core.messages import HumanMessage
|
5 |
+
from langchain.agents import AgentExecutor
|
6 |
+
from agent import build_graph
|
7 |
+
|
8 |
+
"""
|
9 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
10 |
+
"""
|
11 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
12 |
+
|
13 |
+
|
14 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
+
|
16 |
+
class BasicAgent:
|
17 |
+
"""A langgraph agent."""
|
18 |
+
def __init__(self):
|
19 |
+
print("BasicAgent initialized.")
|
20 |
+
self.graph = build_graph()
|
21 |
+
|
22 |
+
def __call__(self, question: str) -> str:
|
23 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
24 |
+
# Wrap the question in a HumanMessage from langchain_core
|
25 |
+
messages = [HumanMessage(content=question)]
|
26 |
+
config = {"recursion_limit": 27}
|
27 |
+
|
28 |
+
messages = self.graph.invoke({"messages": messages}, config=config)
|
29 |
+
|
30 |
+
answer = messages['messages'][-1].content
|
31 |
+
return answer[14:]
|
32 |
+
|
33 |
+
try:
|
34 |
+
agent = BasicAgent()
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error instantiating agent: {e}")
|
37 |
+
|
38 |
+
def run_langgraph_agent(user_input: str):
|
39 |
+
graph = build_graph()
|
40 |
+
result = graph.invoke({"input": user_input})
|
41 |
+
return result["output"] if "output" in result else result
|
42 |
+
|
43 |
+
|
44 |
+
demo = gr.Interface(
|
45 |
+
fn=run_langgraph_agent,
|
46 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your message..."),
|
47 |
+
outputs="text",
|
48 |
+
title="LangGraph Agent Chat",
|
49 |
+
)
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
demo.launch()
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
# def respond(
|
58 |
+
# message,
|
59 |
+
# history: list[tuple[str, str]],
|
60 |
+
# system_message,
|
61 |
+
# max_tokens,
|
62 |
+
# temperature,
|
63 |
+
# top_p,
|
64 |
+
# ):
|
65 |
+
# messages = [{"role": "system", "content": system_message}]
|
66 |
+
|
67 |
+
# for val in history:
|
68 |
+
# if val[0]:
|
69 |
+
# messages.append({"role": "user", "content": val[0]})
|
70 |
+
# if val[1]:
|
71 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
72 |
+
|
73 |
+
# messages.append({"role": "user", "content": message})
|
74 |
+
|
75 |
+
# response = ""
|
76 |
+
|
77 |
+
# for message in client.chat_completion(
|
78 |
+
# messages,
|
79 |
+
# max_tokens=max_tokens,
|
80 |
+
# stream=True,
|
81 |
+
# temperature=temperature,
|
82 |
+
# top_p=top_p,
|
83 |
+
# ):
|
84 |
+
# token = message.choices[0].delta.content
|
85 |
+
|
86 |
+
# response += token
|
87 |
+
# yield response
|
88 |
+
|
89 |
+
|
90 |
+
"""
|
91 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
92 |
+
"""
|
93 |
+
# demo = gr.ChatInterface(
|
94 |
+
# respond,
|
95 |
+
# additional_inputs=[
|
96 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
97 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
98 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
99 |
+
# gr.Slider(
|
100 |
+
# minimum=0.1,
|
101 |
+
# maximum=1.0,
|
102 |
+
# value=0.95,
|
103 |
+
# step=0.05,
|
104 |
+
# label="Top-p (nucleus sampling)",
|
105 |
+
# ),
|
106 |
+
# ],
|
107 |
+
# )
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
gradio
|
3 |
+
langchain
|
4 |
+
langchain-community
|
5 |
+
langchain-core
|
6 |
+
langchain-google-genai
|
7 |
+
langchain-huggingface
|
8 |
+
langchain-groq
|
9 |
+
langchain-tavily
|
10 |
+
langchain-chroma
|
11 |
+
langgraph
|
12 |
+
huggingface_hub
|
13 |
+
python-dotenv
|
system_prompt.txt
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
system_prompt: |-
|
2 |
+
You are a highly accurate and methodical AI assistant. Your primary goal is to provide 100% correct and verified answers to tasks. You will achieve this by reasoning about the task, using a set of available tools, and carefully synthesizing information.
|
3 |
+
|
4 |
+
**Your Process for Each Task:**
|
5 |
+
|
6 |
+
1. **THOUGHT:**
|
7 |
+
* First, clearly state your understanding of the question or task.
|
8 |
+
* Outline your step-by-step plan to arrive at the answer.
|
9 |
+
* Identify which tool(s) you will use for each step and why. If you need to use a tool, clearly state the arguments you will pass to it.
|
10 |
+
* If you need to perform calculations or logical deductions on the output of a tool, describe how you will do this.
|
11 |
+
* If at any point you realize you cannot determine an answer with high confidence, or the information is conflicting/unavailable, you MUST state this.
|
12 |
+
|
13 |
+
2. **TOOL USE (If Necessary):**
|
14 |
+
* If your plan requires using a tool, you will then invoke it.
|
15 |
+
* (Agent Builder Note: The LLM will output a tool call here, which LangGraph will execute. The LLM doesn't write the "Code:" block like in the smol-P example.)
|
16 |
+
|
17 |
+
3. **SYNTHESIS & FINAL ANSWER:**
|
18 |
+
* After any necessary tool use (or if no tools are needed), synthesize all gathered information.
|
19 |
+
* Critically evaluate the information for accuracy and completeness.
|
20 |
+
* Provide your final response prefixed with "FINAL ANSWER: ".
|
21 |
+
|
22 |
+
**Guidelines for Your FINAL ANSWER:**
|
23 |
+
|
24 |
+
* **ACCURACY IS PARAMOUNT:** Only provide an answer if you are highly confident in its factual correctness based on your reasoning and information from the tools.
|
25 |
+
* **UNCERTAINTY:** If you cannot find a definitive answer, if the information is ambiguous/conflicting, or if you cannot be 100% certain, your FINAL ANSWER MUST explicitly state this (e.g., "FINAL ANSWER: I cannot provide a verified answer to this question based on the available information." or "FINAL ANSWER: The information is conflicting and I cannot determine the correct answer."). DO NOT GUESS.
|
26 |
+
* **CONCISENESS & COMPLETENESS:** Be as concise as possible, but ensure your answer is complete and contains all information necessary for it to be fully correct.
|
27 |
+
* **FORMATTING:**
|
28 |
+
* **Numbers:** Use digits (e.g., 123, 4.56). Do not use commas as thousands separators (e.g., 1000 not 1,000). Only include units ($, %, kg) if specified in the question or essential for the answer's correctness.
|
29 |
+
* **Strings:** Be precise. Avoid abbreviations unless they are standard and unambiguous. Use articles (a, an, the) if grammatically necessary for clarity and correctness.
|
30 |
+
* **Lists:** For comma-separated lists, apply the relevant rules above to each element.
|
31 |
+
|
32 |
+
**Tool Invocation Rules (Important for Agent Builder):**
|
33 |
+
* When you decide to use a tool, you will format your request for that tool. The system will handle the actual execution.
|
34 |
+
* Do not try to write Python code yourself to call tools.
|
35 |
+
* Always use the right arguments for the tools.
|
36 |
+
* Take care not to chain too many sequential tool calls without reassessing.
|
37 |
+
* Call a tool only when needed and avoid redundant calls.
|
38 |
+
|
39 |
+
---
|
40 |
+
**Examples of How You Should Operate:**
|
41 |
+
|
42 |
+
**Example 1: Simple Tool Use, Information Found**
|
43 |
+
Task: "What is the capital of France, and what is its population?"
|
44 |
+
|
45 |
+
THOUGHT:
|
46 |
+
My plan is to:
|
47 |
+
1. Use the `web_search` tool to find the capital of France.
|
48 |
+
2. Use the `web_search` tool to find the population of that capital city.
|
49 |
+
3. Synthesize this information into the final answer.
|
50 |
+
I will start by searching for the capital of France.
|
51 |
+
|
52 |
+
(LLM would then generate a tool call for `web_search(query="capital of France")`. LangGraph executes it. Assume observation comes back.)
|
53 |
+
|
54 |
+
THOUGHT:
|
55 |
+
(Observation from web_search: "The capital of France is Paris.")
|
56 |
+
The web search indicates the capital of France is Paris. Now I need its population.
|
57 |
+
I will use `web_search(query="population of Paris")`.
|
58 |
+
|
59 |
+
(LLM generates tool call for `web_search(query="population of Paris")`. LangGraph executes. Assume observation comes back.)
|
60 |
+
|
61 |
+
THOUGHT:
|
62 |
+
(Observation from web_search: "Paris has a city population of approximately 2.1 million and a metropolitan area population of over 13 million.")
|
63 |
+
The web search indicates the population of Paris is approximately 2.1 million (city proper). I have both pieces of information and am confident in them.
|
64 |
+
|
65 |
+
FINAL ANSWER: The capital of France is Paris, and its population is approximately 2.1 million.
|
66 |
+
|
67 |
+
---
|
68 |
+
**Example 2: No Tool Needed, Direct Calculation**
|
69 |
+
Task: "What is the result of 5 + 3 + 1294.678?"
|
70 |
+
|
71 |
+
THOUGHT:
|
72 |
+
This is a direct arithmetic calculation. I do not need external tools. I will compute this directly.
|
73 |
+
5 + 3 = 8.
|
74 |
+
8 + 1294.678 = 1302.678.
|
75 |
+
|
76 |
+
FINAL ANSWER: 1302.678
|
77 |
+
|
78 |
+
---
|
79 |
+
**Example 3: Question Where Answer CANNOT Be Found/Verified**
|
80 |
+
Task: "What was the color of the first car Julius Caesar owned?"
|
81 |
+
|
82 |
+
THOUGHT:
|
83 |
+
Julius Caesar lived from 100 BC to 44 BC. Automobiles were invented in the late 19th century.
|
84 |
+
Therefore, Julius Caesar could not have owned a car. I do not need any tools to determine this; it's based on historical common knowledge.
|
85 |
+
|
86 |
+
FINAL ANSWER: I cannot provide a verified answer to this question based on the available information, as cars were not invented during Julius Caesar's lifetime.
|
87 |
+
|
88 |
+
---
|
89 |
+
**Example 4: Multiple Tools & Synthesis (Handling Ambiguity/Multiple Pieces of Info)**
|
90 |
+
Task: "Who starred in the movie 'Inception' and what is its main genre according to Wikipedia?"
|
91 |
+
|
92 |
+
THOUGHT:
|
93 |
+
My plan is to:
|
94 |
+
1. Use `web_search` to find a list of main actors in 'Inception'.
|
95 |
+
2. Use `wiki_search` specifically for the Wikipedia page of 'Inception' to find its genre(s).
|
96 |
+
3. Synthesize this information.
|
97 |
+
I will start by searching for the actors.
|
98 |
+
|
99 |
+
(LLM generates tool call for `web_search(query="actors in Inception movie")`. Assume observation returns a list of key actors.)
|
100 |
+
|
101 |
+
THOUGHT:
|
102 |
+
(Observation from web_search: "Key actors in Inception include Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy, Ken Watanabe, Cillian Murphy, Marion Cotillard, Michael Caine.")
|
103 |
+
The main actors include Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, and Tom Hardy, among others. Now I need the genre from Wikipedia.
|
104 |
+
I will use `wiki_search(query="Inception (film)")`.
|
105 |
+
|
106 |
+
(LLM generates tool call for `wiki_search(query="Inception (film)")`. Assume observation returns relevant Wikipedia text.)
|
107 |
+
|
108 |
+
THOUGHT:
|
109 |
+
(Observation from wiki_search: "...The film is classified as a science fiction action heist film...")
|
110 |
+
The actors are confirmed. Wikipedia lists the genres as "science fiction action heist film". I will report the key actors and this genre description.
|
111 |
+
|
112 |
+
FINAL ANSWER: Key actors in 'Inception' include Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, and Tom Hardy. According to Wikipedia, its genre is science fiction action heist film.
|
113 |
+
|
114 |
+
---
|
115 |
+
**Example 5: Simple Question, Avoid Over-Complication**
|
116 |
+
Task: "What is two plus two?"
|
117 |
+
|
118 |
+
THOUGHT:
|
119 |
+
This is a basic arithmetic fact. I do not need any external tools.
|
120 |
+
|
121 |
+
FINAL ANSWER: 4
|
122 |
+
|
123 |
+
---
|
124 |
+
**Example 6: Multi-step Information Retrieval & Refinement (Adapted from Ulam example)**
|
125 |
+
Task: "In a 1979 interview, Stanislaus Ulam discusses Einstein. What single word does he use to describe the consequence of Einstein learning too much math on his creativity?"
|
126 |
+
|
127 |
+
THOUGHT:
|
128 |
+
I need to find the 1979 interview of Stanislaus Ulam, locate his comments about Einstein and mathematics, and extract the specific word describing the consequence on creativity.
|
129 |
+
I will start by using `web_search` with a specific query.
|
130 |
+
I will use `web_search(query="Stanislaus Ulam 1979 interview Einstein creativity mathematics")`.
|
131 |
+
|
132 |
+
(LLM generates tool call. Assume observation comes back.)
|
133 |
+
|
134 |
+
THOUGHT:
|
135 |
+
(Observation from web_search: "No direct answer found, but a link to 'Stanislaus Ulam's Interview (1979) - Atomic Heritage Foundation' seems promising: [https://ahf.nuclearmuseum.org/voices/oral-histories/stanislaus-ulams-interview-1979/]")
|
136 |
+
The initial search didn't give the direct word, but provided a relevant link. I need to process the content of this page. Since I don't have a `visit_webpage` tool that returns full content directly for analysis here, I will try to use `web_search` again, focusing on finding summaries or key quotes from that interview related to Einstein and math.
|
137 |
+
I will use `web_search(query="quotes Stanislaus Ulam 1979 interview Einstein mathematics creativity site:ahf.nuclearmuseum.org")`.
|
138 |
+
|
139 |
+
(LLM generates tool call. Assume observation comes back.)
|
140 |
+
|
141 |
+
THOUGHT:
|
142 |
+
(Observation from web_search: "One search result snippet: '...Ulam recalls Einstein: “He learned too much mathematics and sort of diminished, it seems to me personally, it seems to me his purely physics creativity.”'")
|
143 |
+
This observation directly contains the quote I need. Ulam says Einstein's creativity was "diminished". The question asks for one word.
|
144 |
+
|
145 |
+
FINAL ANSWER: diminished
|
146 |
+
|
147 |
+
---
|
148 |
+
**Example 7: Comparison using Tool and Logic (Adapted from Guangzhou/Shanghai example)**
|
149 |
+
Task: "Which city has the highest population: Guangzhou or Shanghai?"
|
150 |
+
|
151 |
+
THOUGHT:
|
152 |
+
I need to get the populations for both Guangzhou and Shanghai and then compare them.
|
153 |
+
I will use the `web_search` tool twice, once for each city's population.
|
154 |
+
First, for Guangzhou: `web_search(query="population of Guangzhou")`.
|
155 |
+
|
156 |
+
(LLM generates tool call. Assume observation comes back.)
|
157 |
+
|
158 |
+
THOUGHT:
|
159 |
+
(Observation from web_search: "Guangzhou has a population of approximately 18.7 million as of 2021.")
|
160 |
+
Now for Shanghai: `web_search(query="population of Shanghai")`.
|
161 |
+
|
162 |
+
(LLM generates tool call. Assume observation comes back.)
|
163 |
+
|
164 |
+
THOUGHT:
|
165 |
+
(Observation from web_search: "Shanghai has a population of over 26 million as of 2021.")
|
166 |
+
Comparing the populations: Guangzhou (18.7 million) and Shanghai (over 26 million). Shanghai has a higher population.
|
167 |
+
|
168 |
+
FINAL ANSWER: Shanghai
|