praisonaiagents 0.0.6__py3-none-any.whl → 0.0.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- praisonaiagents/agent/agent.py +112 -49
- {praisonaiagents-0.0.6.dist-info → praisonaiagents-0.0.7.dist-info}/METADATA +1 -1
- {praisonaiagents-0.0.6.dist-info → praisonaiagents-0.0.7.dist-info}/RECORD +5 -5
- {praisonaiagents-0.0.6.dist-info → praisonaiagents-0.0.7.dist-info}/WHEEL +0 -0
- {praisonaiagents-0.0.6.dist-info → praisonaiagents-0.0.7.dist-info}/top_level.txt +0 -0
praisonaiagents/agent/agent.py
CHANGED
@@ -17,6 +17,85 @@ from ..main import (
|
|
17
17
|
)
|
18
18
|
|
19
19
|
class Agent:
|
20
|
+
def _generate_tool_definition(self, function_name):
|
21
|
+
"""
|
22
|
+
Generate a tool definition from a function name by inspecting the function.
|
23
|
+
"""
|
24
|
+
# First try to get the tool definition if it exists
|
25
|
+
tool_def_name = f"{function_name}_definition"
|
26
|
+
tool_def = globals().get(tool_def_name)
|
27
|
+
if not tool_def:
|
28
|
+
import __main__
|
29
|
+
tool_def = getattr(__main__, tool_def_name, None)
|
30
|
+
|
31
|
+
if tool_def:
|
32
|
+
return tool_def
|
33
|
+
|
34
|
+
# If no definition exists, try to generate one from the function
|
35
|
+
func = globals().get(function_name)
|
36
|
+
if not func:
|
37
|
+
import __main__
|
38
|
+
func = getattr(__main__, function_name, None)
|
39
|
+
|
40
|
+
if not func or not callable(func):
|
41
|
+
return None
|
42
|
+
|
43
|
+
import inspect
|
44
|
+
sig = inspect.signature(func)
|
45
|
+
parameters = {
|
46
|
+
"type": "object",
|
47
|
+
"properties": {},
|
48
|
+
"required": []
|
49
|
+
}
|
50
|
+
|
51
|
+
# Parse docstring for parameter descriptions
|
52
|
+
docstring = inspect.getdoc(func)
|
53
|
+
param_descriptions = {}
|
54
|
+
if docstring:
|
55
|
+
import re
|
56
|
+
param_section = re.split(r'\s*Args:\s*', docstring)
|
57
|
+
if len(param_section) > 1:
|
58
|
+
param_lines = param_section[1].split('\n')
|
59
|
+
for line in param_lines:
|
60
|
+
line = line.strip()
|
61
|
+
if line and ':' in line:
|
62
|
+
param_name, param_desc = line.split(':', 1)
|
63
|
+
param_descriptions[param_name.strip()] = param_desc.strip()
|
64
|
+
|
65
|
+
for name, param in sig.parameters.items():
|
66
|
+
param_type = "string" # Default type
|
67
|
+
if param.annotation != inspect.Parameter.empty:
|
68
|
+
if param.annotation == int:
|
69
|
+
param_type = "integer"
|
70
|
+
elif param.annotation == float:
|
71
|
+
param_type = "number"
|
72
|
+
elif param.annotation == bool:
|
73
|
+
param_type = "boolean"
|
74
|
+
elif param.annotation == list:
|
75
|
+
param_type = "array"
|
76
|
+
elif param.annotation == dict:
|
77
|
+
param_type = "object"
|
78
|
+
|
79
|
+
param_info = {"type": param_type}
|
80
|
+
if name in param_descriptions:
|
81
|
+
param_info["description"] = param_descriptions[name]
|
82
|
+
|
83
|
+
parameters["properties"][name] = param_info
|
84
|
+
if param.default == inspect.Parameter.empty:
|
85
|
+
parameters["required"].append(name)
|
86
|
+
|
87
|
+
# Extract description from docstring
|
88
|
+
description = docstring.split('\n')[0] if docstring else f"Function {function_name}"
|
89
|
+
|
90
|
+
return {
|
91
|
+
"type": "function",
|
92
|
+
"function": {
|
93
|
+
"name": function_name,
|
94
|
+
"description": description,
|
95
|
+
"parameters": parameters
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
20
99
|
def __init__(
|
21
100
|
self,
|
22
101
|
name: str,
|
@@ -53,7 +132,7 @@ class Agent:
|
|
53
132
|
self.goal = goal
|
54
133
|
self.backstory = backstory
|
55
134
|
self.llm = llm
|
56
|
-
self.tools = tools if tools else []
|
135
|
+
self.tools = tools if tools else [] # Store original tools
|
57
136
|
self.function_calling_llm = function_calling_llm
|
58
137
|
self.max_iter = max_iter
|
59
138
|
self.max_rpm = max_rpm
|
@@ -79,18 +158,25 @@ class Agent:
|
|
79
158
|
self.max_reflection_iter = max_reflection_iter
|
80
159
|
|
81
160
|
def execute_tool(self, function_name, arguments):
|
161
|
+
"""
|
162
|
+
Execute a tool dynamically based on the function name and arguments.
|
163
|
+
"""
|
82
164
|
logging.debug(f"{self.name} executing tool {function_name} with arguments: {arguments}")
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
165
|
+
|
166
|
+
# Try to get the function from globals first
|
167
|
+
func = globals().get(function_name)
|
168
|
+
if not func:
|
169
|
+
# Then try to get from the main module
|
170
|
+
import __main__
|
171
|
+
func = getattr(__main__, function_name, None)
|
172
|
+
|
173
|
+
if func and callable(func):
|
174
|
+
try:
|
175
|
+
return func(**arguments)
|
176
|
+
except Exception as e:
|
177
|
+
return {"error": str(e)}
|
178
|
+
|
179
|
+
return {"error": f"Tool '{function_name}' is not callable"}
|
94
180
|
|
95
181
|
def clear_history(self):
|
96
182
|
self.chat_history = []
|
@@ -104,26 +190,25 @@ class Agent:
|
|
104
190
|
logging.debug(f"{self.name} sending messages to LLM: {messages}")
|
105
191
|
|
106
192
|
formatted_tools = []
|
193
|
+
if tools is None:
|
194
|
+
tools = self.tools
|
107
195
|
if tools:
|
108
196
|
for tool in tools:
|
109
|
-
if isinstance(tool,
|
197
|
+
if isinstance(tool, str):
|
198
|
+
# Generate tool definition for string tool names
|
199
|
+
tool_def = self._generate_tool_definition(tool)
|
200
|
+
if tool_def:
|
201
|
+
formatted_tools.append(tool_def)
|
202
|
+
else:
|
203
|
+
logging.warning(f"Could not generate definition for tool: {tool}")
|
204
|
+
elif isinstance(tool, dict):
|
110
205
|
formatted_tools.append(tool)
|
111
206
|
elif hasattr(tool, "to_openai_tool"):
|
112
207
|
formatted_tools.append(tool.to_openai_tool())
|
113
|
-
elif
|
114
|
-
formatted_tools.append(
|
115
|
-
"type": "function",
|
116
|
-
"function": {
|
117
|
-
"name": tool,
|
118
|
-
"description": f"This is a tool called {tool}",
|
119
|
-
"parameters": {
|
120
|
-
"type": "object",
|
121
|
-
"properties": {},
|
122
|
-
},
|
123
|
-
}
|
124
|
-
})
|
208
|
+
elif callable(tool):
|
209
|
+
formatted_tools.append(self._generate_tool_definition(tool.__name__))
|
125
210
|
else:
|
126
|
-
|
211
|
+
logging.warning(f"Tool {tool} not recognized")
|
127
212
|
|
128
213
|
try:
|
129
214
|
initial_response = client.chat.completions.create(
|
@@ -223,29 +308,7 @@ class Agent:
|
|
223
308
|
if self.verbose:
|
224
309
|
display_instruction(f"Agent {self.name} is processing prompt: {prompt}")
|
225
310
|
|
226
|
-
|
227
|
-
if tools:
|
228
|
-
for tool in tools:
|
229
|
-
if isinstance(tool, dict):
|
230
|
-
formatted_tools.append(tool)
|
231
|
-
elif hasattr(tool, "to_openai_tool"):
|
232
|
-
formatted_tools.append(tool.to_openai_tool())
|
233
|
-
elif isinstance(tool, str):
|
234
|
-
formatted_tools.append({
|
235
|
-
"type": "function",
|
236
|
-
"function": {
|
237
|
-
"name": tool,
|
238
|
-
"description": f"This is a tool called {tool}",
|
239
|
-
"parameters": {
|
240
|
-
"type": "object",
|
241
|
-
"properties": {},
|
242
|
-
},
|
243
|
-
}
|
244
|
-
})
|
245
|
-
else:
|
246
|
-
display_error(f"Warning: Tool {tool} not recognized")
|
247
|
-
|
248
|
-
response = self._chat_completion(messages, temperature=temperature, tools=formatted_tools if formatted_tools else None)
|
311
|
+
response = self._chat_completion(messages, temperature=temperature, tools=tools if tools else None)
|
249
312
|
if not response:
|
250
313
|
return None
|
251
314
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
praisonaiagents/__init__.py,sha256=gI8vEabBTRPsE_E8GA5sBMi4sTtJI-YokPrH2Nor-k0,741
|
2
2
|
praisonaiagents/main.py,sha256=zDhN5KKtKbfruolDNxlyJkcFlkSt4KQkQTDRfQVAhxc,3960
|
3
3
|
praisonaiagents/agent/__init__.py,sha256=sKO8wGEXvtCrvV1e834r1Okv0XAqAxqZCqz6hKLiTvA,79
|
4
|
-
praisonaiagents/agent/agent.py,sha256=
|
4
|
+
praisonaiagents/agent/agent.py,sha256=D51Ia4jGxG-eLWaztQI03vyx_KrcK46p3eEkj8L0GNU,17381
|
5
5
|
praisonaiagents/agents/__init__.py,sha256=7RDeQNSqZg5uBjD4M_0p_F6YgfWuDuxPFydPU50kDYc,120
|
6
6
|
praisonaiagents/agents/agents.py,sha256=NkosnTo41bB9H0lYt_YQIHwaRyW2Bcp_4KKpYWeaFk0,13696
|
7
7
|
praisonaiagents/build/lib/praisonaiagents/__init__.py,sha256=Nqnn8clbgv-5l0PgxcTOldg8mkMKrFn4TvPL-rYUUGg,1
|
@@ -14,7 +14,7 @@ praisonaiagents/build/lib/praisonaiagents/task/__init__.py,sha256=VL5hXVmyGjINb3
|
|
14
14
|
praisonaiagents/build/lib/praisonaiagents/task/task.py,sha256=4Y1qX8OeEFcid2yhAiPYylvHpuDmWORsyNL16_BiVvI,1831
|
15
15
|
praisonaiagents/task/__init__.py,sha256=VL5hXVmyGjINb34AalxpBMl-YW9m5EDcRkMTKkSSl7c,80
|
16
16
|
praisonaiagents/task/task.py,sha256=4Y1qX8OeEFcid2yhAiPYylvHpuDmWORsyNL16_BiVvI,1831
|
17
|
-
praisonaiagents-0.0.
|
18
|
-
praisonaiagents-0.0.
|
19
|
-
praisonaiagents-0.0.
|
20
|
-
praisonaiagents-0.0.
|
17
|
+
praisonaiagents-0.0.7.dist-info/METADATA,sha256=2b0TkGxlgNiEyFHjeqJhKe9Uph4unxb1QqVlJhFY6js,232
|
18
|
+
praisonaiagents-0.0.7.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
19
|
+
praisonaiagents-0.0.7.dist-info/top_level.txt,sha256=_HsRddrJ23iDx5TTqVUVvXG2HeHBL5voshncAMDGjtA,16
|
20
|
+
praisonaiagents-0.0.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|