-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbielik_agent
164 lines (133 loc) · 4.62 KB
/
bielik_agent
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Colab set up
!pip install transformers
!pip install torch
!pip install llama-cpp-python
!wget -O Bielik-11B-v2.3-Instruct.Q4_K_M.gguf https://huggingface.co/speakleash/Bielik-11B-v2.3-Instruct-GGUF/resolve/main/Bielik-11B-v2.3-Instruct.Q4_K_M.gguf
"""
import json
from typing import Callable, Dict, List, Union
import re
import random
def get_fn_signature(fn: Callable):
fn_signature: Dict = {
"name": fn.__name__,
"description": fn.__doc__ or "",
"parameters": {
"properties": {}
}
}
schema = {
k: {"type": v.__name__}
for k, v in fn.__annotations__.items() if k != "return"
}
fn_signature["parameters"]["properties"] = schema
return fn_signature
class Tool:
def __init__(self, fn: Callable):
self.fn = fn
self.name = fn.__name__
self.signature = get_fn_signature(fn)
self.fn_signature = self.signature # For compatibility
def run(self, **kwargs):
return self.fn(**kwargs)
def tool(fn: Callable):
return Tool(fn)
# Sample tools
@tool
def add_numbers(a: int, b: int) -> int:
"""Adds two numbers."""
return a + b
@tool
def roll_dice(sides: int = 6, rolls: int = 1) -> str:
"""
Simulates rolling dice with a specified number of sides and number of rolls.
Returns the result of each roll.
"""
if sides < 1 or rolls < 1:
return "Error: 'sides' and 'rolls' must be positive integers."
results = [random.randint(1, sides) for _ in range(rolls)]
return f"Rolling a {sides}-sided dice {rolls} time(s): {results}"
# List of tools
tools = [add_numbers, roll_dice]
from llama_cpp import Llama
class ToolAgent:
def __init__(self, tools: Union[Tool, List[Tool]], model_path: str):
self.tools = tools if isinstance(tools, list) else [tools]
self.tools_dict = {tool.name: tool for tool in self.tools}
self.model = Llama(model_path=model_path)
def build_system_prompt(self):
tools_info = ""
for tool in self.tools:
tools_info += json.dumps(tool.fn_signature) + "\n"
return f"""
You are an AI assistant capable of calling functions to help with the user's request.
Available functions are provided within <tools></tools> tags.
When necessary, output a function call in the following format within <tool_call></tool_call> tags:
<tool_call>
{{"name": "<function_name>", "arguments": <arguments_dict>}}
</tool_call>
Here are the available tools:
<tools>
{tools_info}
</tools>
"""
def parse_tool_call(self, response: str):
pattern = r"<tool_call>(.*?)</tool_call>"
match = re.search(pattern, response, re.DOTALL)
if match:
tool_call_str = match.group(1).strip()
try:
tool_call = json.loads(tool_call_str)
return tool_call
except json.JSONDecodeError:
print("Failed to parse tool call JSON.")
return None
def generate_response(self, prompt: str):
# Use the model to generate a response
response = self.model(
prompt,
max_tokens=256,
stop=["\nUser:", "\nAI:"],
echo=False,
temperature=0.7,
)
return response['choices'][0]['text']
def run(self, user_input: str):
self.model.reset() # added
system_prompt = self.build_system_prompt()
full_prompt = f"{system_prompt}\nUser: {user_input}\nAI:"
response_text = self.generate_response(full_prompt)
# Parse and execute tool call if present
tool_call = self.parse_tool_call(response_text)
if tool_call:
tool_name = tool_call.get("name")
arguments = tool_call.get("arguments", {})
tool = self.tools_dict.get(tool_name)
if tool:
try:
result = tool.run(**arguments)
final_response = f"The result is: {result}"
except Exception as e:
final_response = f"Error executing tool '{tool_name}': {e}"
else:
final_response = f"Tool '{tool_name}' not found."
else:
final_response = response_text.strip()
return final_response
# execution
import time
model_path = "/content/Bielik-11B-v2.3-Instruct.Q4_K_M.gguf"
# Initialize the agent
agent = ToolAgent(tools, model_path)
# User input
user_input1 = "What is the sum of 10 and 15?"
# Run the agent
response1 = agent.run(user_input1)
print(response1)
time.sleep(10)
# User input
user_input2 = "Can you roll a dice for me?"
# Run the agent
response2 = agent.run(user_input2)
print(response2)