-
Notifications
You must be signed in to change notification settings - Fork 11
/
promptic.py
487 lines (419 loc) · 18.6 KB
/
promptic.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import inspect
import json
import logging
import re
from functools import wraps
from textwrap import dedent
from typing import Callable, Dict, Any, List, Optional
import litellm
from jsonschema import validate as validate_json_schema
from pydantic import BaseModel
class State:
"""Base state class for managing conversation memory"""
def __init__(self):
self._messages: List[Dict[str, str]] = []
def add_message(self, message: Dict[str, str]) -> None:
"""Add a message to the conversation history"""
self._messages.append(message)
def get_messages(
self, prompt: str = None, limit: int = None
) -> List[Dict[str, str]]:
"""Retrieve messages from the conversation history
Args:
prompt: Optional prompt to filter messages by
limit: Optional number of most recent messages to return
"""
if limit is None:
return self._messages
return self._messages[-limit:]
def clear(self) -> None:
"""Clear all messages from memory"""
self._messages = []
class Promptic:
def __init__(
self,
model="gpt-4o-mini",
system: str = None,
dry_run: bool = False,
debug: bool = False,
memory: bool = False,
state: Optional[State] = None,
json_schema: Optional[Dict] = None,
**litellm_kwargs,
):
self.model = model
self.system = system
self.dry_run = dry_run
self.litellm_kwargs = litellm_kwargs
self.tools: Dict[str, Callable] = {}
self.json_schema = json_schema
self.logger = logging.getLogger("promptic")
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s"
)
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.debug = debug
if debug:
self.logger.setLevel(logging.DEBUG)
else:
self.logger.setLevel(logging.WARNING)
self.result_regex = re.compile(r"```(?:json)?(.*?)```", re.DOTALL)
self.memory = memory or state is not None
if memory and state is None:
self.state = State()
else:
self.state = state
self.anthropic = self.model.startswith(("claude", "anthropic"))
self.gemini = self.model.startswith(("gemini", "vertex"))
def __call__(self, fn=None):
return self._decorator(fn) if fn else self._decorator
def tool(self, fn: Callable) -> Callable:
"""Register a function as a tool that can be used by the LLM"""
if self.anthropic and self.tools:
raise ValueError("Anthropic models currently support only one tool.")
self.tools[fn.__name__] = fn
return fn
def _generate_tool_definition(self, fn: Callable) -> dict:
"""Generate a tool definition from a function's metadata"""
sig = inspect.signature(fn)
doc = dedent(fn.__doc__ or "")
parameters = {"type": "object", "properties": {}, "required": []}
for name, param in sig.parameters.items():
param_type = param.annotation if param.annotation != inspect._empty else Any
param_default = None if param.default == inspect._empty else param.default
if param_default is None and param.default == inspect._empty:
parameters["required"].append(name)
param_info = {"type": "string"} # Default to string if no type hint
if param_type == int:
param_info["type"] = "integer"
elif param_type == float:
param_info["type"] = "number"
elif param_type == bool:
param_info["type"] = "boolean"
parameters["properties"][name] = param_info
# Add dummy parameter for Gemini models
if self.gemini:
parameters["properties"]["llm_invocation"] = {
"type": "boolean",
"description": "True if the function was invoked by an LLM",
}
parameters["required"].append("llm_invocation")
return {
"type": "function",
"function": {
"name": fn.__name__,
"description": doc,
"parameters": parameters,
},
}
def _parse_and_validate_response(self, generated_text: str, return_type: Any):
"""Parse and validate the response according to the return type"""
self.logger.debug(f"Parsing response: {generated_text}")
self.logger.debug(f"Return type: {return_type}")
# Handle Pydantic model return types
if (
return_type
and inspect.isclass(return_type)
and issubclass(return_type, BaseModel)
):
match = self.result_regex.search(generated_text)
if match:
json_result = match.group(1)
if self.state:
self.state.add_message(
{"content": json_result, "role": "assistant"}
)
return return_type.model_validate_json(json_result)
raise ValueError("Failed to extract JSON result from the generated text.")
# Handle json_schema if provided
elif self.json_schema:
match = self.result_regex.search(generated_text)
if not match:
raise ValueError(
"Failed to extract JSON result from the generated text."
)
try:
json_result = match.group(1)
parsed_result = json.loads(json_result)
# Validate against the schema
validate_json_schema(instance=parsed_result, schema=self.json_schema)
if self.state:
self.state.add_message(
{"content": json_result, "role": "assistant"}
)
return parsed_result
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in response: {e}")
except Exception as e:
raise ValueError(f"Schema validation failed: {str(e)}")
# Handle plain text responses
else:
if self.state:
self.state.add_message({"content": generated_text, "role": "assistant"})
return generated_text
def _decorator(self, func: Callable):
return_type = func.__annotations__.get("return")
if (
return_type
and inspect.isclass(return_type)
and issubclass(return_type, BaseModel)
and self.json_schema
):
raise ValueError(
"Cannot use both Pydantic return type hints and json_schema validation together"
)
@wraps(func)
def wrapper(*args, **kwargs):
self.logger.debug(f"{self.model = }")
self.logger.debug(f"{self.system = }")
self.logger.debug(f"{self.dry_run = }")
self.logger.debug(f"{self.litellm_kwargs = }")
self.logger.debug(f"{self.tools = }")
self.logger.debug(f"{func = }")
self.logger.debug(f"{args = }")
self.logger.debug(f"{kwargs = }")
if self.tools:
assert litellm.supports_function_calling(
self.model
), f"Model {self.model} does not support function calling"
# Get the function's docstring as the prompt
prompt_template = dedent(func.__doc__)
# Get the argument names, default values and values using inspect
sig = inspect.signature(func)
arg_names = sig.parameters.keys()
arg_values = {
name: (
sig.parameters[name].default
if sig.parameters[name].default is not inspect.Parameter.empty
else None
)
for name in arg_names
}
arg_values.update(zip(arg_names, args))
arg_values.update(kwargs)
self.logger.debug(f"{arg_values = }")
# Replace {name} placeholders with argument values
prompt_text = prompt_template.format(**arg_values)
# Check if the function has a return type hint of a Pydantic model
return_type = func.__annotations__.get("return")
self.logger.debug(f"{return_type = }")
messages = [{"content": prompt_text, "role": "user"}]
if self.system:
messages.insert(0, {"content": self.system, "role": "system"})
# Store the user message in state before making the API call
if self.state:
history = self.state.get_messages()
self.state.add_message({"content": prompt_text, "role": "user"})
if history: # Add previous history if it exists
messages = history + messages
# Add tools if any are registered
tools = None
if self.tools:
tools = [
self._generate_tool_definition(tool_fn)
for tool_fn in self.tools.values()
]
# Add schema instructions before any LLM call if return type requires it
if (
return_type
and inspect.isclass(return_type)
and issubclass(return_type, BaseModel)
):
schema = return_type.model_json_schema()
json_schema = json.dumps(schema, indent=2)
messages.append(
{
"role": "user",
"content": (
"Format your response according to this JSON schema:\n"
f"```json\n{json_schema}\n```\n\n"
"Provide the result enclosed in triple backticks with 'json' "
"on the first line. Don't put control characters in the wrong "
"place or the JSON will be invalid."
),
}
)
elif self.json_schema:
json_schema = json.dumps(self.json_schema, indent=2)
messages.append(
{
"role": "user",
"content": (
"Format your response according to this JSON schema:\n"
f"```json\n{json_schema}\n```\n\n"
"Provide the result enclosed in triple backticks with 'json' "
"on the first line. Don't put control characters in the wrong "
"place or the JSON will be invalid."
),
}
)
# Add check for Gemini streaming with tools
if self.gemini and self.litellm_kwargs.get("stream") and self.tools:
raise ValueError("Gemini models do not support streaming with tools")
# Call the LLM with the prompt and tools
response = litellm.completion(
model=self.model,
messages=messages,
tools=tools if tools else None,
tool_choice="auto" if tools else None,
**self.litellm_kwargs,
)
if self.litellm_kwargs.get("stream"):
return self._stream_response(response)
# Handle tool calls if present
if (
hasattr(response.choices[0].message, "tool_calls")
and response.choices[0].message.tool_calls
):
tool_calls = response.choices[0].message.tool_calls
messages.append(response.choices[0].message)
for tool_call in tool_calls:
function_name = tool_call.function.name
if function_name in self.tools:
function_args = json.loads(tool_call.function.arguments)
if self.gemini and "llm_invocation" in function_args:
function_args.pop("llm_invocation")
if self.dry_run:
self.logger.warning(
f"[DRY RUN]: {function_name = } {function_args = }"
)
function_response = f"[DRY RUN] Would have called {function_name = } {function_args = }"
else:
function_response = self.tools[function_name](
**function_args
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": str(function_response),
}
)
claude_kwargs = {}
# Anthropic requires tools be explicitly set
if self.anthropic and tools:
claude_kwargs["tools"] = tools
claude_kwargs["tool_choice"] = "auto"
# Get final response after tool calls
final_response = litellm.completion(
model=self.model,
messages=messages,
**self.litellm_kwargs,
**claude_kwargs,
)
generated_text = final_response.choices[0].message.content
else:
generated_text = response["choices"][0]["message"]["content"]
return self._parse_and_validate_response(generated_text, return_type)
# Add methods explicitly
wrapper.tool = self.tool
wrapper.clear = self.clear
# Automatically expose all other attributes from self
for attr_name, attr_value in self.__dict__.items():
if not attr_name.startswith("_"): # Skip private attributes
setattr(wrapper, attr_name, attr_value)
return wrapper
def _stream_response(self, response):
current_tool_calls = {}
current_index = None
accumulated_response = ""
for part in response:
# Handle tool calls in streaming mode
if (
hasattr(part.choices[0].delta, "tool_calls")
and part.choices[0].delta.tool_calls
):
tool_calls = part.choices[0].delta.tool_calls
for tool_call in tool_calls:
# If we have an ID and name, this is the start of a new tool call
if tool_call.id:
current_index = tool_call.index
current_tool_calls[current_index] = {
"id": tool_call.id,
"name": tool_call.function.name,
"arguments": "",
}
# If we don't have an ID but have arguments, append to current tool call
elif tool_call.function.arguments and current_index is not None:
current_tool_calls[current_index]["arguments"] += (
tool_call.function.arguments
)
# Try to execute if arguments look complete
tool_info = current_tool_calls[current_index]
try:
args_str = tool_info["arguments"]
if (
args_str.strip() and args_str[-1] == "}"
): # Check if arguments look complete
try:
function_args = json.loads(args_str)
if (
self.gemini
and "llm_invocation" in function_args
):
function_args.pop("llm_invocation")
if tool_info["name"] in self.tools:
if self.dry_run:
self.logger.warning(
f"[DRY RUN] Would have called {tool_info['name']} with {function_args}"
)
else:
self.tools[tool_info["name"]](
**function_args
)
# Clear after successful execution
del current_tool_calls[current_index]
except json.JSONDecodeError:
# Arguments not complete yet, continue accumulating
continue
except Exception as e:
self.logger.error(f"Error executing tool: {e}")
self.logger.exception(e)
continue
# Stream regular content and accumulate
if (
hasattr(part.choices[0].delta, "content")
and part.choices[0].delta.content
):
content = part.choices[0].delta.content
accumulated_response += content
yield content
# After streaming is complete, add to state if memory is enabled
if self.state:
self.state.add_message(
{"content": accumulated_response, "role": "assistant"}
)
def clear(self) -> None:
"""Clear all messages from the state if it exists.
Raises:
ValueError: If memory/state is not enabled
"""
if not self.memory or not self.state:
raise ValueError("Cannot clear state: memory/state is not enabled")
self.state.clear()
def promptic(
fn=None,
model="gpt-4o-mini",
system: str = None,
dry_run: bool = False,
debug: bool = False,
memory: bool = False,
state: Optional[State] = None,
json_schema: Optional[Dict] = None,
**litellm_kwargs,
):
decorator = Promptic(
model=model,
system=system,
dry_run=dry_run,
debug=debug,
memory=memory,
state=state,
json_schema=json_schema,
**litellm_kwargs,
)
return decorator(fn) if fn else decorator
llm = promptic