llmadapt 0.1.0__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.
- llmadapt/__init__.py +4 -0
- llmadapt/core.py +545 -0
- llmadapt-0.1.0.dist-info/METADATA +112 -0
- llmadapt-0.1.0.dist-info/RECORD +7 -0
- llmadapt-0.1.0.dist-info/WHEEL +5 -0
- llmadapt-0.1.0.dist-info/licenses/LICENSE +21 -0
- llmadapt-0.1.0.dist-info/top_level.txt +1 -0
llmadapt/__init__.py
ADDED
llmadapt/core.py
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import inspect
|
|
4
|
+
import urllib.request
|
|
5
|
+
import urllib.error
|
|
6
|
+
from typing import get_type_hints, Callable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Conversation:
|
|
10
|
+
""" Conversation Class:
|
|
11
|
+
This needs to manage chat memory and has to have information of where the information came from,
|
|
12
|
+
such as from a tool, model or the user.
|
|
13
|
+
This then needs to be able to send such memory when asked for based on the syntax and structure
|
|
14
|
+
required depending on the AI model used.
|
|
15
|
+
Key Providers: Gemini, OpenAI, Anthropic, Ollama (for local models).
|
|
16
|
+
These providers should naturally work within the library.
|
|
17
|
+
Custom Providers are possible to use but a function to specify the structure is required. """
|
|
18
|
+
|
|
19
|
+
def __init__(self, system_instruction: str = ""):
|
|
20
|
+
self.history = []
|
|
21
|
+
self.system_instruction = system_instruction
|
|
22
|
+
self.model_role = "assistant"
|
|
23
|
+
|
|
24
|
+
if self.system_instruction:
|
|
25
|
+
self.history.append({"role": "system", "content": self.system_instruction})
|
|
26
|
+
|
|
27
|
+
def add_user_msg(self, text: str):
|
|
28
|
+
self.history.append({"role": "user", "content": text})
|
|
29
|
+
|
|
30
|
+
def add_model_msg(self, text: str = None, tool_calls: list = None, role: str = None,
|
|
31
|
+
native: dict = None, native_provider: str = None):
|
|
32
|
+
if role is None:
|
|
33
|
+
role = self.model_role
|
|
34
|
+
|
|
35
|
+
msg = {"role": role}
|
|
36
|
+
if text:
|
|
37
|
+
msg["content"] = text
|
|
38
|
+
if tool_calls:
|
|
39
|
+
normalized_calls = []
|
|
40
|
+
for tc in tool_calls:
|
|
41
|
+
func = tc.get("function") or {}
|
|
42
|
+
args = func.get("arguments") or {}
|
|
43
|
+
if isinstance(args, str):
|
|
44
|
+
try:
|
|
45
|
+
args = json.loads(args)
|
|
46
|
+
except json.JSONDecodeError:
|
|
47
|
+
pass
|
|
48
|
+
normalized_calls.append({
|
|
49
|
+
"id": tc.get("id"),
|
|
50
|
+
"type": "function",
|
|
51
|
+
"function": {
|
|
52
|
+
"name": func.get("name"),
|
|
53
|
+
"arguments": args
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
msg["tool_calls"] = normalized_calls
|
|
57
|
+
|
|
58
|
+
if native is not None:
|
|
59
|
+
msg["_native"] = native
|
|
60
|
+
msg["_native_provider"] = native_provider
|
|
61
|
+
self.history.append(msg)
|
|
62
|
+
|
|
63
|
+
def add_tool_response(self, function_name: str, output: str, tool_call_id: str = None):
|
|
64
|
+
msg = {
|
|
65
|
+
"role": "tool",
|
|
66
|
+
"name": function_name,
|
|
67
|
+
"content": output
|
|
68
|
+
}
|
|
69
|
+
if tool_call_id:
|
|
70
|
+
msg["tool_call_id"] = tool_call_id
|
|
71
|
+
self.history.append(msg)
|
|
72
|
+
|
|
73
|
+
def export_for(self, provider: str, special_format: Callable[[list], list] = None) -> list:
|
|
74
|
+
"""Translates local flat memory frames into target external API layouts."""
|
|
75
|
+
if special_format and callable(special_format):
|
|
76
|
+
return special_format(self.history)
|
|
77
|
+
|
|
78
|
+
if provider in ["ollama", "openai", "custom"]:
|
|
79
|
+
cleaned_history = []
|
|
80
|
+
for msg in self.history:
|
|
81
|
+
cleaned_msg = {k: v for k, v in msg.items() if not k.startswith("_")}
|
|
82
|
+
|
|
83
|
+
if provider in ["openai", "custom"] and "tool_calls" in cleaned_msg:
|
|
84
|
+
stringified_calls = []
|
|
85
|
+
for tc in cleaned_msg["tool_calls"]:
|
|
86
|
+
func = tc.get("function") or {}
|
|
87
|
+
args = func.get("arguments") or {}
|
|
88
|
+
if not isinstance(args, str):
|
|
89
|
+
args = json.dumps(args)
|
|
90
|
+
|
|
91
|
+
stringified_calls.append({
|
|
92
|
+
"id": tc.get("id"),
|
|
93
|
+
"type": "function",
|
|
94
|
+
"function": {
|
|
95
|
+
"name": func.get("name"),
|
|
96
|
+
"arguments": args
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
cleaned_msg["tool_calls"] = stringified_calls
|
|
100
|
+
|
|
101
|
+
cleaned_history.append(cleaned_msg)
|
|
102
|
+
return cleaned_history
|
|
103
|
+
|
|
104
|
+
elif provider == "anthropic":
|
|
105
|
+
return self._export_anthropic()
|
|
106
|
+
|
|
107
|
+
elif provider == "gemini":
|
|
108
|
+
return self._export_gemini()
|
|
109
|
+
|
|
110
|
+
def change_system_instruction(self, new_instruction: str):
|
|
111
|
+
self.system_instruction = new_instruction
|
|
112
|
+
updated = False
|
|
113
|
+
for msg in self.history:
|
|
114
|
+
if msg["role"] == "system":
|
|
115
|
+
msg["content"] = self.system_instruction
|
|
116
|
+
updated = True
|
|
117
|
+
break
|
|
118
|
+
if not updated and self.system_instruction:
|
|
119
|
+
self.history.insert(0, {"role": "system", "content": self.system_instruction})
|
|
120
|
+
|
|
121
|
+
def change_model_role(self, new_role: str):
|
|
122
|
+
self.model_role = new_role
|
|
123
|
+
|
|
124
|
+
def _export_anthropic(self) -> list:
|
|
125
|
+
"""Structures conversation blocks to comply with Anthropic context constraints."""
|
|
126
|
+
out = []
|
|
127
|
+
pending_tool_results = []
|
|
128
|
+
emitted_native_ids = set()
|
|
129
|
+
|
|
130
|
+
def flush():
|
|
131
|
+
if pending_tool_results:
|
|
132
|
+
out.append({"role": "user", "content": pending_tool_results.copy()})
|
|
133
|
+
pending_tool_results.clear()
|
|
134
|
+
|
|
135
|
+
for msg in self.history:
|
|
136
|
+
if msg["role"] == "system":
|
|
137
|
+
continue
|
|
138
|
+
|
|
139
|
+
if msg.get("_native_provider") == "anthropic" and "_native" in msg:
|
|
140
|
+
native_hash = json.dumps(msg["_native"], sort_keys=True)
|
|
141
|
+
if native_hash in emitted_native_ids:
|
|
142
|
+
continue
|
|
143
|
+
emitted_native_ids.add(native_hash)
|
|
144
|
+
flush()
|
|
145
|
+
out.append(msg["_native"])
|
|
146
|
+
continue
|
|
147
|
+
|
|
148
|
+
if msg["role"] == "tool":
|
|
149
|
+
pending_tool_results.append({
|
|
150
|
+
"type": "tool_result",
|
|
151
|
+
"tool_use_id": msg.get("tool_call_id", ""),
|
|
152
|
+
"content": msg["content"]
|
|
153
|
+
})
|
|
154
|
+
continue
|
|
155
|
+
|
|
156
|
+
flush()
|
|
157
|
+
|
|
158
|
+
if msg["role"] == "user":
|
|
159
|
+
out.append({"role": "user", "content": msg["content"]})
|
|
160
|
+
elif msg["role"] == "assistant":
|
|
161
|
+
content = []
|
|
162
|
+
if msg.get("content"):
|
|
163
|
+
content.append({"type": "text", "text": msg["content"]})
|
|
164
|
+
for tool_call in msg.get("tool_calls", []):
|
|
165
|
+
content.append({
|
|
166
|
+
"type": "tool_use",
|
|
167
|
+
"id": tool_call["id"],
|
|
168
|
+
"name": tool_call["function"]["name"],
|
|
169
|
+
"input": tool_call["function"]["arguments"]
|
|
170
|
+
})
|
|
171
|
+
out.append({"role": "assistant", "content": content if content else (msg.get("content") or "")})
|
|
172
|
+
|
|
173
|
+
flush()
|
|
174
|
+
return out
|
|
175
|
+
|
|
176
|
+
def _export_gemini(self) -> list:
|
|
177
|
+
"""Structures conversation blocks to comply with Gemini context constraints."""
|
|
178
|
+
gemini_history = []
|
|
179
|
+
for msg in self.history:
|
|
180
|
+
if msg["role"] == "system":
|
|
181
|
+
continue
|
|
182
|
+
if msg.get("_native_provider") == "gemini" and "_native" in msg:
|
|
183
|
+
gemini_history.append(msg["_native"])
|
|
184
|
+
continue
|
|
185
|
+
if msg["role"] == "user":
|
|
186
|
+
gemini_history.append({"role": "user", "parts": [{"text": msg["content"]}]})
|
|
187
|
+
elif msg["role"] == self.model_role:
|
|
188
|
+
parts = []
|
|
189
|
+
if msg.get("content"):
|
|
190
|
+
parts.append({"text": msg["content"]})
|
|
191
|
+
if "tool_calls" in msg:
|
|
192
|
+
for tool_call in msg["tool_calls"]:
|
|
193
|
+
function_call_part = {
|
|
194
|
+
"name": tool_call["function"]["name"],
|
|
195
|
+
"args": tool_call["function"]["arguments"]
|
|
196
|
+
}
|
|
197
|
+
if tool_call.get("id"):
|
|
198
|
+
function_call_part["id"] = tool_call["id"]
|
|
199
|
+
parts.append({"functionCall": function_call_part})
|
|
200
|
+
gemini_history.append({"role": "model", "parts": parts})
|
|
201
|
+
elif msg["role"] == "tool":
|
|
202
|
+
function_response_part = {"name": msg["name"], "response": {"result": msg["content"]}}
|
|
203
|
+
if msg.get("tool_call_id"):
|
|
204
|
+
function_response_part["id"] = msg["tool_call_id"]
|
|
205
|
+
gemini_history.append({
|
|
206
|
+
"role": "user",
|
|
207
|
+
"parts": [{"functionResponse": function_response_part}]
|
|
208
|
+
})
|
|
209
|
+
return gemini_history
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class ToolRegistry:
|
|
213
|
+
""" Tool Registry Class:
|
|
214
|
+
This class needs to handle tool storage and usage.
|
|
215
|
+
Methods to register tool use tool.
|
|
216
|
+
It should be able to export for all AI models and work with all providers.
|
|
217
|
+
Again, custom providers are possible to use but a function to specify the structure is required."""
|
|
218
|
+
|
|
219
|
+
def __init__(self):
|
|
220
|
+
self.functions_maps = {}
|
|
221
|
+
self.schemas = {}
|
|
222
|
+
|
|
223
|
+
def register(self, python_function, schema: dict = None):
|
|
224
|
+
if schema is None:
|
|
225
|
+
func_name = python_function.__name__
|
|
226
|
+
func_doc = inspect.getdoc(python_function) or "No description available."
|
|
227
|
+
type_mapping = {
|
|
228
|
+
str: "string", int: "integer", float: "number", bool: "boolean"
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
type_hints = get_type_hints(python_function)
|
|
232
|
+
signature = inspect.signature(python_function)
|
|
233
|
+
properties = {}
|
|
234
|
+
required_params = []
|
|
235
|
+
|
|
236
|
+
for param_name, param in signature.parameters.items():
|
|
237
|
+
param_type = type_hints.get(param_name, str)
|
|
238
|
+
gemini_type = type_mapping.get(param_type, "string")
|
|
239
|
+
|
|
240
|
+
properties[param_name] = {
|
|
241
|
+
"type": gemini_type,
|
|
242
|
+
"description": f"The {param_name} parameter"
|
|
243
|
+
}
|
|
244
|
+
if param.default == inspect.Parameter.empty:
|
|
245
|
+
required_params.append(param_name)
|
|
246
|
+
|
|
247
|
+
schema = {
|
|
248
|
+
"name": func_name,
|
|
249
|
+
"description": func_doc,
|
|
250
|
+
"parameters": {
|
|
251
|
+
"type": "object",
|
|
252
|
+
"properties": properties,
|
|
253
|
+
"required": required_params
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else:
|
|
257
|
+
func_name = schema["name"]
|
|
258
|
+
|
|
259
|
+
self.functions_maps[func_name] = python_function
|
|
260
|
+
self.schemas[func_name] = schema
|
|
261
|
+
|
|
262
|
+
def execute(self, name: str, args: dict) -> str:
|
|
263
|
+
"""Invokes a registered tool, safely converting crashes into explicit textual logs."""
|
|
264
|
+
if name not in self.functions_maps:
|
|
265
|
+
return f"Error: Tool '{name}' is not registered in this system."
|
|
266
|
+
|
|
267
|
+
try:
|
|
268
|
+
if isinstance(args, str):
|
|
269
|
+
try:
|
|
270
|
+
args = json.loads(args)
|
|
271
|
+
except json.JSONDecodeError:
|
|
272
|
+
pass
|
|
273
|
+
|
|
274
|
+
if not isinstance(args, dict):
|
|
275
|
+
return f"Error: Arguments for tool '{name}' must be passed as a dictionary."
|
|
276
|
+
|
|
277
|
+
res = self.functions_maps[name](**args)
|
|
278
|
+
return _stringify_tool_output(res)
|
|
279
|
+
except Exception as e:
|
|
280
|
+
error_type = e.__class__.__name__
|
|
281
|
+
return f"Tool Execution Failure ({error_type}): {str(e)}. Please adjust your input arguments."
|
|
282
|
+
|
|
283
|
+
def export_for(self, provider: str, special_format: Callable[[list], list] = None) -> list:
|
|
284
|
+
if not self.schemas:
|
|
285
|
+
return []
|
|
286
|
+
|
|
287
|
+
schema_list = list(self.schemas.values())
|
|
288
|
+
|
|
289
|
+
if special_format and callable(special_format):
|
|
290
|
+
return special_format(schema_list)
|
|
291
|
+
|
|
292
|
+
if provider in ["ollama", "openai"]:
|
|
293
|
+
return [{"type": "function", "function": schema} for schema in schema_list]
|
|
294
|
+
elif provider == "anthropic":
|
|
295
|
+
return [{"name": s["name"], "description": s["description"], "input_schema": s["parameters"]} for s in schema_list]
|
|
296
|
+
elif provider == "gemini":
|
|
297
|
+
return [{"functionDeclarations": schema_list}]
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
class Agent:
|
|
301
|
+
""" Agent Class:
|
|
302
|
+
This is the core agent class that needs to have several key features:
|
|
303
|
+
This agent handles the Conversation and Tool object.
|
|
304
|
+
When an AI responds asking for the results of certain tools, this must run it; however, there
|
|
305
|
+
should be a max to how many times this iteration can happen - function to change max iterations.
|
|
306
|
+
A function to switch APIs and all - will make later additions easier.
|
|
307
|
+
Add tool and send request to AI model functions are needed.
|
|
308
|
+
An overall Chat function that handles sending the payload to the AI model and handles the response. """
|
|
309
|
+
|
|
310
|
+
DEFAULT_MODELS = {
|
|
311
|
+
"gemini": "gemini-1.5-flash",
|
|
312
|
+
"anthropic": "claude-3-5-sonnet-20241022",
|
|
313
|
+
"openai": "gpt-4o-mini",
|
|
314
|
+
"ollama": "llama3.1:8b"
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
def __init__(self, provider: str, model: str = None, base_url: str = None,
|
|
318
|
+
api_key: str = None, system_instruction: str = "", max_tool_iterations: int = 6,
|
|
319
|
+
max_tokens: int = None):
|
|
320
|
+
self.conversation = Conversation(system_instruction=system_instruction)
|
|
321
|
+
self.tool_registry = ToolRegistry()
|
|
322
|
+
self.change_api(provider=provider, model=model, base_url=base_url, api_key=api_key)
|
|
323
|
+
self.set_max_tool_iterations(max_tool_iterations)
|
|
324
|
+
self.max_tokens = max_tokens
|
|
325
|
+
|
|
326
|
+
def set_max_tool_iterations(self, n: int):
|
|
327
|
+
"""Sets the upper threshold for consecutive tool execution cycles."""
|
|
328
|
+
if not isinstance(n, int) or n < 1:
|
|
329
|
+
raise ValueError("max_tool_iterations must be an integer >= 1")
|
|
330
|
+
self.max_tool_iterations = n
|
|
331
|
+
|
|
332
|
+
def change_api(self, provider: str, model: str = None, base_url: str = None, api_key: str = None):
|
|
333
|
+
"""Re-routes active transport endpoints with fluent default model fallbacks."""
|
|
334
|
+
self.provider = provider.lower()
|
|
335
|
+
self.model = model or self.DEFAULT_MODELS.get(self.provider, "custom-model")
|
|
336
|
+
self.api_key = api_key
|
|
337
|
+
|
|
338
|
+
if base_url:
|
|
339
|
+
self.url = base_url
|
|
340
|
+
elif self.provider == "gemini":
|
|
341
|
+
self.url = f"https://generativelanguage.googleapis.com/v1beta/models/{self.model}:generateContent"
|
|
342
|
+
elif self.provider == "openai":
|
|
343
|
+
self.url = "https://api.openai.com/v1/chat/completions"
|
|
344
|
+
elif self.provider == "anthropic":
|
|
345
|
+
self.url = "https://api.anthropic.com/v1/messages"
|
|
346
|
+
elif self.provider == "ollama":
|
|
347
|
+
self.url = "http://localhost:11434/api/chat"
|
|
348
|
+
else:
|
|
349
|
+
self.url = ""
|
|
350
|
+
|
|
351
|
+
if not self.api_key and self.provider != "ollama":
|
|
352
|
+
self.api_key = os.getenv(f"{self.provider.upper()}_API_KEY")
|
|
353
|
+
|
|
354
|
+
def add_tool(self, python_function: Callable, schema: dict = None):
|
|
355
|
+
self.tool_registry.register(python_function, schema)
|
|
356
|
+
|
|
357
|
+
def _send_request(self, payload: dict, headers: dict) -> dict:
|
|
358
|
+
"""Executes native standard library post requests using zero external code."""
|
|
359
|
+
if not self.url:
|
|
360
|
+
raise ValueError(f"API endpoint URL is not configured for provider '{self.provider}'. "
|
|
361
|
+
f"Please supply a valid base_url when calling change_api().")
|
|
362
|
+
|
|
363
|
+
data_bytes = json.dumps(payload).encode("utf-8")
|
|
364
|
+
req = urllib.request.Request(self.url, data=data_bytes, headers=headers, method="POST")
|
|
365
|
+
try:
|
|
366
|
+
with urllib.request.urlopen(req) as response:
|
|
367
|
+
return json.loads(response.read().decode("utf-8"))
|
|
368
|
+
except urllib.error.HTTPError as e:
|
|
369
|
+
raise RuntimeError(f"\n[{self.provider.upper()} HTTP Error {e.code}]: {e.read().decode('utf-8')}")
|
|
370
|
+
except urllib.error.URLError as e:
|
|
371
|
+
raise RuntimeError(f"\n[Network Unreachable]: Check route {self.url}. Reason: {e.reason}")
|
|
372
|
+
|
|
373
|
+
def chat(self, user_input: str, custom_format_func: Callable[[list], list] = None, max_tokens: int = None) -> str:
|
|
374
|
+
self.conversation.add_user_msg(user_input)
|
|
375
|
+
max_tokens = max_tokens if max_tokens is not None else self.max_tokens
|
|
376
|
+
|
|
377
|
+
iterations = 0
|
|
378
|
+
while True:
|
|
379
|
+
iterations += 1
|
|
380
|
+
if iterations > self.max_tool_iterations:
|
|
381
|
+
raise RuntimeError(
|
|
382
|
+
f"Exceeded max_tool_iterations ({self.max_tool_iterations}) without a final "
|
|
383
|
+
f"text response. Call agent.set_max_tool_iterations(n) to raise this limit."
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
history = self.conversation.export_for(self.provider, special_format=custom_format_func)
|
|
387
|
+
tools = self.tool_registry.export_for(self.provider, special_format=custom_format_func)
|
|
388
|
+
headers = {"Content-Type": "application/json"}
|
|
389
|
+
|
|
390
|
+
# --- GEMINI RUNTIME ---
|
|
391
|
+
if self.provider == "gemini":
|
|
392
|
+
if self.api_key: headers["x-goog-api-key"] = self.api_key
|
|
393
|
+
payload = {"contents": history}
|
|
394
|
+
if tools: payload["tools"] = tools
|
|
395
|
+
if self.conversation.system_instruction:
|
|
396
|
+
payload["systemInstruction"] = {"parts": [{"text": self.conversation.system_instruction}]}
|
|
397
|
+
|
|
398
|
+
if max_tokens is not None:
|
|
399
|
+
payload["generationConfig"] = {"maxOutputTokens": max_tokens}
|
|
400
|
+
|
|
401
|
+
res = self._send_request(payload, headers)
|
|
402
|
+
|
|
403
|
+
if 'candidates' not in res or not res['candidates']:
|
|
404
|
+
error_msg = res.get('error', {}).get('message', 'Unknown Gemini API Error')
|
|
405
|
+
raise RuntimeError(f"Gemini API empty response or error: {error_msg}")
|
|
406
|
+
|
|
407
|
+
parts = res['candidates'][0]['content']['parts']
|
|
408
|
+
|
|
409
|
+
function_calls = [p['functionCall'] for p in parts if 'functionCall' in p]
|
|
410
|
+
text_parts = [p['text'] for p in parts if 'text' in p]
|
|
411
|
+
|
|
412
|
+
if function_calls:
|
|
413
|
+
# Gemini now returns a unique "id" with each functionCall and
|
|
414
|
+
# expects that same id echoed back in the functionResponse so
|
|
415
|
+
# it can match results to requests (older model versions may
|
|
416
|
+
# not send an id at all, so fallback to "" when absent).
|
|
417
|
+
tool_calls = [{"id": function_call.get("id", ""),
|
|
418
|
+
"function": {"name": function_call['name'], "arguments": function_call.get('args', {})}}
|
|
419
|
+
for function_call in function_calls]
|
|
420
|
+
self.conversation.add_model_msg(
|
|
421
|
+
text="".join(text_parts) or None,
|
|
422
|
+
tool_calls=tool_calls,
|
|
423
|
+
native={"role": "model", "parts": parts},
|
|
424
|
+
native_provider="gemini"
|
|
425
|
+
)
|
|
426
|
+
for function_call in function_calls:
|
|
427
|
+
name, args = function_call['name'], function_call.get('args', {})
|
|
428
|
+
call_id = function_call.get("id", "")
|
|
429
|
+
out_str = self.tool_registry.execute(name, args)
|
|
430
|
+
function_response = {"name": name, "response": {"result": out_str}}
|
|
431
|
+
if call_id:
|
|
432
|
+
function_response["id"] = call_id
|
|
433
|
+
self.conversation.history.append({
|
|
434
|
+
"role": "tool", "name": name, "content": out_str,
|
|
435
|
+
"tool_call_id": call_id,
|
|
436
|
+
"_native": {"role": "user", "parts": [{"functionResponse": function_response}]},
|
|
437
|
+
"_native_provider": "gemini"
|
|
438
|
+
})
|
|
439
|
+
continue
|
|
440
|
+
text = "".join(text_parts)
|
|
441
|
+
self.conversation.add_model_msg(text=text)
|
|
442
|
+
return text
|
|
443
|
+
|
|
444
|
+
# --- ANTHROPIC RUNTIME ---
|
|
445
|
+
elif self.provider == "anthropic":
|
|
446
|
+
if max_tokens is None:
|
|
447
|
+
max_tokens = 4096
|
|
448
|
+
headers.update({"x-api-key": self.api_key, "anthropic-version": "2023-06-01"})
|
|
449
|
+
payload = {"model": self.model, "messages": history, "max_tokens": max_tokens}
|
|
450
|
+
if tools: payload["tools"] = tools
|
|
451
|
+
if self.conversation.system_instruction: payload["system"] = self.conversation.system_instruction
|
|
452
|
+
|
|
453
|
+
res = self._send_request(payload, headers)
|
|
454
|
+
t_calls, final_text = [], ""
|
|
455
|
+
|
|
456
|
+
for block in res.get("content", []):
|
|
457
|
+
if block["type"] == "text": final_text += block["text"]
|
|
458
|
+
elif block["type"] == "tool_use":
|
|
459
|
+
t_calls.append({"id": block["id"], "function": {"name": block["name"], "arguments": block["input"]}})
|
|
460
|
+
|
|
461
|
+
if t_calls:
|
|
462
|
+
self.conversation.add_model_msg(
|
|
463
|
+
text=final_text if final_text else None,
|
|
464
|
+
tool_calls=t_calls,
|
|
465
|
+
native={"role": "assistant", "content": res.get("content", [])},
|
|
466
|
+
native_provider="anthropic"
|
|
467
|
+
)
|
|
468
|
+
tool_result_blocks = []
|
|
469
|
+
for tc in t_calls:
|
|
470
|
+
out_str = self.tool_registry.execute(tc["function"]["name"], tc["function"]["arguments"])
|
|
471
|
+
tool_result_blocks.append({"type": "tool_result", "tool_use_id": tc["id"], "content": out_str})
|
|
472
|
+
native_user_msg = {"role": "user", "content": tool_result_blocks}
|
|
473
|
+
for tc, block in zip(t_calls, tool_result_blocks):
|
|
474
|
+
self.conversation.history.append({
|
|
475
|
+
"role": "tool", "name": tc["function"]["name"], "content": block["content"],
|
|
476
|
+
"tool_call_id": tc["id"],
|
|
477
|
+
"_native": native_user_msg, "_native_provider": "anthropic"
|
|
478
|
+
})
|
|
479
|
+
continue
|
|
480
|
+
self.conversation.add_model_msg(text=final_text)
|
|
481
|
+
return final_text
|
|
482
|
+
|
|
483
|
+
# --- OPENAI / CUSTOM MOCK RUNTIME ---
|
|
484
|
+
elif self.provider in ["openai", "custom"]:
|
|
485
|
+
if self.api_key: headers["Authorization"] = f"Bearer {self.api_key}"
|
|
486
|
+
payload = {"model": self.model, "messages": history}
|
|
487
|
+
if tools: payload["tools"] = tools
|
|
488
|
+
|
|
489
|
+
if max_tokens is not None:
|
|
490
|
+
payload["max_completion_tokens"] = max_tokens
|
|
491
|
+
payload["max_tokens"] = max_tokens
|
|
492
|
+
|
|
493
|
+
res = self._send_request(payload, headers)
|
|
494
|
+
|
|
495
|
+
if 'choices' not in res or not res['choices']:
|
|
496
|
+
error_msg = res.get('error', {}).get('message', 'Unknown OpenAI API Error')
|
|
497
|
+
raise RuntimeError(f"OpenAI API empty response or error: {error_msg}")
|
|
498
|
+
|
|
499
|
+
msg = res['choices'][0]['message']
|
|
500
|
+
|
|
501
|
+
if msg.get("tool_calls"):
|
|
502
|
+
self.conversation.add_model_msg(text=msg.get("content"), tool_calls=msg["tool_calls"])
|
|
503
|
+
for tool_call in msg["tool_calls"]:
|
|
504
|
+
name = tool_call["function"]["name"]
|
|
505
|
+
args = json.loads(tool_call["function"]["arguments"]) if isinstance(tool_call["function"]["arguments"], str) else tool_call["function"]["arguments"]
|
|
506
|
+
out_str = self.tool_registry.execute(name, args)
|
|
507
|
+
self.conversation.add_tool_response(name, out_str, tool_call["id"])
|
|
508
|
+
continue
|
|
509
|
+
text = msg.get("content", "")
|
|
510
|
+
self.conversation.add_model_msg(text=text)
|
|
511
|
+
return text
|
|
512
|
+
|
|
513
|
+
# --- OLLAMA RUNTIME ---
|
|
514
|
+
elif self.provider == "ollama":
|
|
515
|
+
payload = {"model": self.model, "messages": history, "stream": False}
|
|
516
|
+
if tools: payload["tools"] = tools
|
|
517
|
+
if max_tokens is not None:
|
|
518
|
+
payload["options"] = {"num_predict": max_tokens}
|
|
519
|
+
|
|
520
|
+
res = self._send_request(payload, headers)
|
|
521
|
+
msg = res.get("message", {})
|
|
522
|
+
|
|
523
|
+
if msg.get("tool_calls"):
|
|
524
|
+
self.conversation.add_model_msg(text=msg.get("content"), tool_calls=msg["tool_calls"])
|
|
525
|
+
for tool_call in msg["tool_calls"]:
|
|
526
|
+
name, args = tool_call["function"]["name"], tool_call["function"]["arguments"]
|
|
527
|
+
out_str = self.tool_registry.execute(name, args)
|
|
528
|
+
self.conversation.add_tool_response(name, out_str, tool_call.get("id"))
|
|
529
|
+
continue
|
|
530
|
+
text = msg.get("content", "")
|
|
531
|
+
self.conversation.add_model_msg(text=text)
|
|
532
|
+
return text
|
|
533
|
+
|
|
534
|
+
def change_default_max_tokens(self, max_tokens: int):
|
|
535
|
+
self.max_tokens = max_tokens
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
def _stringify_tool_output(out) -> str:
|
|
539
|
+
"""Serializes tool return values into valid JSON strings for model consumption."""
|
|
540
|
+
if isinstance(out, str):
|
|
541
|
+
return out
|
|
542
|
+
try:
|
|
543
|
+
return json.dumps(out)
|
|
544
|
+
except TypeError:
|
|
545
|
+
return str(out)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llmadapt
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight, zero-dependency, provider-agnostic LLM agent with tool-calling support for Anthropic, OpenAI, Gemini, and Ollama.
|
|
5
|
+
Author-email: Shyam Parikh <shyamparikh@yahoo.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Shyam-Parikh-2025/llmadapt
|
|
8
|
+
Project-URL: Issues, https://github.com/Shyam-Parikh-2025/llmadapt/issues
|
|
9
|
+
Keywords: llm,agent,tool-calling,anthropic,openai,gemini,ollama
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# llmadapt
|
|
24
|
+
|
|
25
|
+
A small, zero-dependency, provider-agnostic agent for chatting with LLMs and
|
|
26
|
+
giving them tools. Works with Anthropic, OpenAI, Gemini, and Ollama using only
|
|
27
|
+
the Python standard library for HTTP — no `requests`, no provider SDKs.
|
|
28
|
+
v0.1.0
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
From source (until published to PyPI):
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
git clone https://github.com/Shyam-Parikh-2025/llmadapt.git
|
|
36
|
+
cd llmadapt
|
|
37
|
+
pip install -e .
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Once published:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install llmadapt
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quick start
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from llmadapt import Agent
|
|
50
|
+
|
|
51
|
+
agent = Agent(
|
|
52
|
+
provider="anthropic",
|
|
53
|
+
model="claude-sonnet-4-6",
|
|
54
|
+
api_key="sk-ant-...", # or set ANTHROPIC_API_KEY in your environment
|
|
55
|
+
system_instruction="You are a helpful assistant.",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
print(agent.chat("What's 12 * 7?"))
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Giving the agent tools
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
def get_weather(city: str) -> dict:
|
|
65
|
+
"""Look up the current weather for a city."""
|
|
66
|
+
return {"city": city, "tempF": 72, "condition": "sunny"}
|
|
67
|
+
|
|
68
|
+
agent.add_tool(get_weather)
|
|
69
|
+
print(agent.chat("What's the weather in NYC?"))
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
A JSON schema is auto-generated from the function's type hints, default
|
|
73
|
+
values, and docstring. Tool outputs are JSON-serialized automatically
|
|
74
|
+
(plain strings pass through untouched).
|
|
75
|
+
|
|
76
|
+
## Switching providers
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
agent.switch_api(provider="openai", model="gpt-4o", api_key="sk-...")
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Limiting tool-call loops
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
agent.set_max_tool_iterations(20) # default is 10
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
If the model gets stuck calling tools repeatedly without producing a final
|
|
89
|
+
answer, `chat()` raises a `RuntimeError` instead of looping forever.
|
|
90
|
+
|
|
91
|
+
## Supported providers
|
|
92
|
+
|
|
93
|
+
| provider | env var for API key | notes |
|
|
94
|
+
|-------------|------------------------|--------------------------------|
|
|
95
|
+
| `anthropic` | `ANTHROPIC_API_KEY` | Messages API |
|
|
96
|
+
| `openai` | `OPENAI_API_KEY` | Chat Completions API |
|
|
97
|
+
| `gemini` | `GEMINI_API_KEY` | generateContent endpoint |
|
|
98
|
+
| `ollama` | *(none — local)* | defaults to `localhost:11434` |
|
|
99
|
+
| `custom` | *(none — pass directly)* | requires `base_url` + a `custom_format_func` you supply to `chat()` |
|
|
100
|
+
|
|
101
|
+
## Running tests
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
python tests/test_agent.py
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Tests run entirely offline against scripted fake HTTP responses — no API key
|
|
108
|
+
or network access required.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
llmadapt/__init__.py,sha256=ZBU1JvJqqS-P7YBJ__SNw7hPLlooI6iNt5_WKg63K3Y,127
|
|
2
|
+
llmadapt/core.py,sha256=lnz2n0Y5lc8V7H6_A4d1FiYz1J60xK7k_drhIZDi0a4,25636
|
|
3
|
+
llmadapt-0.1.0.dist-info/licenses/LICENSE,sha256=v2spsd7N1pKFFh2G8wGP_45iwe5S0DYiJzG4im8Rupc,1066
|
|
4
|
+
llmadapt-0.1.0.dist-info/METADATA,sha256=NALUOxJVi-BJeaqkCzvIphSDztBZczDVZKfKKJduUwA,3390
|
|
5
|
+
llmadapt-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
6
|
+
llmadapt-0.1.0.dist-info/top_level.txt,sha256=bT7NMnd6dQJIpY0Qk8dOWX1ha-8_jlm0D9I6peGWPSM,9
|
|
7
|
+
llmadapt-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Your Name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
llmadapt
|