Update tools_agent.py
Browse files- tools_agent.py +21 -7
tools_agent.py
CHANGED
@@ -4,17 +4,32 @@ from huggingface_hub import InferenceClient
|
|
4 |
import os
|
5 |
import json
|
6 |
|
|
|
|
|
7 |
|
8 |
class Agent:
|
9 |
-
def __init__(self):
|
10 |
"""
|
11 |
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
|
|
|
|
|
12 |
"""
|
13 |
# self.client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
14 |
self.client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
15 |
|
16 |
"""Initialize Agent with empty tool registry."""
|
17 |
self.tools: Dict[str, Tool] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def add_tool(self, tool: Tool) -> None:
|
20 |
"""Register a new tool with the agent."""
|
@@ -164,18 +179,17 @@ Remember to use tools only when they are actually needed for the task."""
|
|
164 |
|
165 |
def plan(self, user_query: str) -> Dict:
|
166 |
"""Use LLM to create a plan for tool usage."""
|
167 |
-
|
168 |
-
|
169 |
-
{"role": "user", "content": user_query}
|
170 |
-
]
|
171 |
|
172 |
response = self.client.chat_completion(
|
173 |
-
messages,
|
174 |
max_tokens=512,
|
175 |
temperature=0,
|
176 |
top_p=0.95,
|
177 |
)
|
178 |
print(response.choices[0])
|
|
|
179 |
try:
|
180 |
return json.loads(response.choices[0].message.content)
|
181 |
except json.JSONDecodeError:
|
@@ -206,7 +220,7 @@ Results: {'. '.join(results)}"""
|
|
206 |
return f"Error executing plan: {str(e)}"
|
207 |
|
208 |
def main():
|
209 |
-
|
210 |
|
211 |
agent = Agent()
|
212 |
agent.add_tool(convert_currency)
|
|
|
4 |
import os
|
5 |
import json
|
6 |
|
7 |
+
from tools import convert_currency
|
8 |
+
|
9 |
|
10 |
class Agent:
|
11 |
+
def __init__(self, tools: list[Tool]):
|
12 |
"""
|
13 |
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
|
14 |
+
Inference API: a service that allows you to run accelerated inference on Hugging Face’s infrastructure for free. This service is a fast way to get started,
|
15 |
+
test different models, and prototype AI products.
|
16 |
"""
|
17 |
# self.client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
18 |
self.client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
19 |
|
20 |
"""Initialize Agent with empty tool registry."""
|
21 |
self.tools: Dict[str, Tool] = {}
|
22 |
+
|
23 |
+
for tool in Tools:
|
24 |
+
self.add_tool(tool)
|
25 |
+
|
26 |
+
self.messages = [
|
27 |
+
{"role": "system", "content": self.create_system_prompt()},
|
28 |
+
]
|
29 |
+
|
30 |
+
def add_message(self, query_message: str) -> None:
|
31 |
+
"""Store the new message to the memory/state of the agent."""
|
32 |
+
self.messages.append({"role": "user", "content": query_message})
|
33 |
|
34 |
def add_tool(self, tool: Tool) -> None:
|
35 |
"""Register a new tool with the agent."""
|
|
|
179 |
|
180 |
def plan(self, user_query: str) -> Dict:
|
181 |
"""Use LLM to create a plan for tool usage."""
|
182 |
+
|
183 |
+
self.add_message(user_query)
|
|
|
|
|
184 |
|
185 |
response = self.client.chat_completion(
|
186 |
+
self.messages,
|
187 |
max_tokens=512,
|
188 |
temperature=0,
|
189 |
top_p=0.95,
|
190 |
)
|
191 |
print(response.choices[0])
|
192 |
+
|
193 |
try:
|
194 |
return json.loads(response.choices[0].message.content)
|
195 |
except json.JSONDecodeError:
|
|
|
220 |
return f"Error executing plan: {str(e)}"
|
221 |
|
222 |
def main():
|
223 |
+
|
224 |
|
225 |
agent = Agent()
|
226 |
agent.add_tool(convert_currency)
|