-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
97 lines (74 loc) · 3.76 KB
/
Copy pathagent.py
File metadata and controls
97 lines (74 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import sys
from typing import List
import json
from tools import Tool,read_file
import ast
class Agent:
def __init__(self, client, get_user_message,tools):
self.client = client
self.get_user_message = get_user_message
self.conversation = []
self.tools = tools
def run(self):
print("\nChat with LLAMA (type 'exit' or 'ctrl+c' to quit)\n")
try:
while True:
user_input = self.get_user_message()
if user_input == 'exit':
print("\n👋 Exiting chat. Goodbye!")
break
self.conversation.append({"role": "user", "content": user_input})
message = self.run_inference(self.conversation)
if not message:
print("Error getting response.")
return
try:
parsed = ast.literal_eval(message)
except Exception:
parsed = {}
if isinstance(parsed, dict) and "tool_use" in parsed:
tool_name = parsed["tool_use"]["name"]
tool_input = json.dumps(parsed["tool_use"]["input"]) # input as json string
tool = next((t for t in self.tools if t.name == tool_name), None)
if tool:
tool_result = tool.func(tool_input)
else:
tool_result = f"Error: Tool '{tool_name}' not found."
# Detect if the tool_result contains an error skip if does move to next user input
if isinstance(tool_result, str) and tool_result.startswith("Error:"):
error_message = f"Tool returned an error: {tool_result}"
print(f"\nAI 🤖: {error_message}\n")
self.conversation.append({"role": "assistant", "content": error_message})
print("\n" + "-"*60 + "\n")
continue
self.conversation.append({"role": "assistant", "content": message})
self.conversation.append({"role": "user", "content": tool_result})
# Run inference again to get final reply
response = self.run_inference(self.conversation)
print(f"\nAI 🤖: {response}\n")
self.conversation.append({"role": "assistant", "content": response})
print("\n" + "-"*60 + "\n")
else:
print(f"\nAI 🤖: {message}\n")
print("\n" + "-"*60 + "\n")
self.conversation.append({"role": "assistant", "content": message})
except KeyboardInterrupt:
print("\n👋Goodbye!")
def run_inference(self, conversation):
try:
tool_descriptions = "\n".join(
[f"Tool: {t.name} - {t.description}" for t in self.tools]
)
system_prompt = f"You have access to the following tools:\n{tool_descriptions}\nIf you want to use a tool, only respond the dictionary not reasoning, respond in the format:\n{{'tool_use': {{'name': <tool_name>, 'input': <json_string>}}}}\nOtherwise, respond normally."
conversation = [{"role": "system", "content": system_prompt}] + conversation
response = self.client.chat.completions.create(
model="meta-llama/llama-3.3-8b-instruct:free",
messages=conversation,
max_tokens=1024,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
print(f"Error during inference: {e}")
return None