Update tools/tool_agent.py
Browse files- tools/tool_agent.py +14 -18
tools/tool_agent.py
CHANGED
@@ -3,29 +3,25 @@ import json
|
|
3 |
|
4 |
class ToolCallingAgent:
|
5 |
def __init__(self):
|
|
|
6 |
self.model = pipeline(
|
7 |
"text-generation",
|
8 |
-
model="cognitivecomputations/dolphin-2.
|
9 |
-
|
|
|
10 |
)
|
11 |
|
12 |
def generate(self, prompt, tools):
|
13 |
-
# Format the tools specification
|
14 |
-
tools_json = json.dumps(tools, ensure_ascii=False)
|
15 |
-
|
16 |
-
# Create the tool-calling prompt
|
17 |
-
system_msg = f"""You are an AI assistant that can call tools.
|
18 |
-
Available tools: {tools_json}
|
19 |
-
Respond with JSON containing 'tool_name' and 'parameters'."""
|
20 |
-
|
21 |
-
# Generate the response
|
22 |
-
response = self.model(
|
23 |
-
f"<|system|>{system_msg}</s><|user|>{prompt}</s>",
|
24 |
-
max_new_tokens=200,
|
25 |
-
do_sample=True
|
26 |
-
)
|
27 |
-
|
28 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
return json.loads(response[0]['generated_text'])
|
30 |
except:
|
31 |
-
return {"error": "Failed to
|
|
|
3 |
|
4 |
class ToolCallingAgent:
|
5 |
def __init__(self):
|
6 |
+
# Force CPU and smaller model
|
7 |
self.model = pipeline(
|
8 |
"text-generation",
|
9 |
+
model="cognitivecomputations/dolphin-2.1-mistral-7b", # Smaller than llama3
|
10 |
+
device=-1, # Force CPU
|
11 |
+
torch_dtype="float32" # Better for CPU
|
12 |
)
|
13 |
|
14 |
def generate(self, prompt, tools):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
+
tools_json = json.dumps(tools, ensure_ascii=False)
|
17 |
+
prompt = f"""Respond with JSON for one tool call. Tools: {tools_json}\nInput: {prompt}"""
|
18 |
+
|
19 |
+
response = self.model(
|
20 |
+
prompt,
|
21 |
+
max_new_tokens=150, # Shorter responses
|
22 |
+
do_sample=False # More deterministic
|
23 |
+
)
|
24 |
+
|
25 |
return json.loads(response[0]['generated_text'])
|
26 |
except:
|
27 |
+
return {"tool_name": "error", "parameters": {"message": "Failed to process request"}}
|