-
Notifications
You must be signed in to change notification settings - Fork 18
/
promptic.py
587 lines (492 loc) · 23.2 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import warnings
warnings.filterwarnings("ignore", message="Valid config keys have changed in V2:*")
import inspect
import json
import logging
import re
from functools import wraps
from textwrap import dedent
from typing import Callable, Dict, Any, List, Optional, Union
import litellm
from jsonschema import validate as validate_json_schema
from pydantic import BaseModel
from litellm.utils import CustomStreamWrapper
__version__ = "4.1.0"
SystemPrompt = Optional[Union[str, List[str], List[Dict[str, str]]]]
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: SystemPrompt = None,
dry_run: bool = False,
debug: bool = False,
memory: bool = False,
state: Optional[State] = None,
json_schema: Optional[Dict] = None,
cache: bool = True,
**litellm_kwargs,
):
"""Initialize a new Promptic instance.
Args:
model (str, optional): The LLM model to use. Defaults to "gpt-4o-mini".
system (SystemPrompt, optional): System prompt(s) to prepend to all conversations.
Can be a string, list of strings, or list of message dictionaries. Defaults to None.
dry_run (bool, optional): If True, tools will not be executed. Defaults to False.
debug (bool, optional): Enable debug logging. Defaults to False.
memory (bool, optional): Enable conversation memory. Defaults to False.
state (State, optional): Custom state instance for memory management. Defaults to None.
json_schema (Dict, optional): JSON schema for response validation. Defaults to None.
cache (bool, optional): Enable response caching for Anthropic models. Defaults to True.
**litellm_kwargs: Additional keyword arguments passed to litellm.completion().
"""
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)
litellm.set_verbose = True
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"))
self.cache = cache
self.anthropic_cached_block_limit = 4
self.cached_count = 0
self.tool_definitions = None
@property
def system_messages(self):
result = []
if not self.system:
return result
if isinstance(self.system, str):
result = [{"content": self.system, "role": "system"}]
elif isinstance(self.system, list) and isinstance(self.system[0], dict):
result = self.system
elif isinstance(self.system, list) and isinstance(self.system[0], str):
result = [{"content": msg, "role": "system"} for msg in self.system]
else:
raise ValueError("Invalid system prompt")
result = self._set_anthropic_cache(result)
if self.state and not self.state.get_messages():
for msg in result:
self.state.add_message(msg)
return result
def _completion(self, messages: list[dict], **kwargs):
new_messages = self._set_anthropic_cache(messages)
previous_messages = self.state.get_messages() if self.state else []
completion_messages = self.system_messages + previous_messages + new_messages
cached_count = 0
for msg in completion_messages:
if msg.get("cache_control"):
if cached_count == self.anthropic_cached_block_limit:
msg.pop("cache_control")
else:
cached_count += 1
result = litellm.completion(
model=self.model,
messages=completion_messages,
tools=self.tool_definitions,
tool_choice="auto" if self.tool_definitions else None,
**(self.litellm_kwargs | kwargs),
)
if self.state:
for msg in new_messages:
self.state.add_message(msg)
return result
def message(self, message: str, **kwargs):
messages = [{"content": message, "role": "user"}]
response = self._completion(messages, **kwargs)
if isinstance(response, CustomStreamWrapper):
return self._stream_response(response)
content = response.choices[0].message.content
return self._parse_and_validate_response(content)
def _set_anthropic_cache(self, messages: List[dict]):
"""Set the cache control for the message if it is an Anthropic message"""
if not (self.cache and self.anthropic):
return messages
for msg in messages:
if len(str(msg.get("content"))) * 4 > 1024:
msg["cache_control"] = {"type": "ephemeral"}
return messages
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"""
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 the function doesn't take any arguments
if self.gemini and not parameters.get("required"):
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=None, json_schema=None
):
"""Parse and validate the response according to the return type"""
# Handle Pydantic model return types
if 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.loads(json_result))
raise ValueError("Failed to extract JSON result from the generated text.")
# Handle json_schema if provided
elif 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
@classmethod
def decorate(cls, func: Callable = None, **kwargs):
"""See Promptic.__init__ for valid kwargs."""
instance = cls(**kwargs)
return instance._decorator(func) if func else instance
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 = }")
self.logger.debug(f"{self.cache = }")
if self.tools:
assert litellm.supports_function_calling(
self.model
), f"Model {self.model} does not support function calling"
self.tool_definitions = (
[
self._generate_tool_definition(tool_fn)
for tool_fn in self.tools.values()
]
if self.tools
else None
)
# 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 = }")
# Create the user message
user_message = {"content": prompt_text, "role": "user"}
messages = [user_message]
# 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)
msg = {
"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."
),
}
messages.append(msg)
elif self.json_schema:
json_schema = json.dumps(self.json_schema, indent=2)
msg = {
"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."
),
}
messages.append(msg)
# 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")
self.logger.debug("Chat History:")
for i, msg in enumerate(messages):
self.logger.debug(f"Message {i}:")
self.logger.debug(f" Role: {msg.get('role', 'unknown')}")
self.logger.debug(f" Content: {msg.get('content')}")
if "tool_calls" in msg:
self.logger.debug(" Tool Calls:")
for tool_call in msg["tool_calls"]:
self.logger.debug(f" Name: {tool_call.function.name}")
self.logger.debug(
f" Arguments: {tool_call.function.arguments}"
)
if "tool_call_id" in msg:
self.logger.debug(f" Tool Call ID: {msg['tool_call_id']}")
self.logger.debug(f" Tool Name: {msg.get('name')}")
if self.tool_definitions:
self.logger.debug("\nAvailable Tools:")
for tool in self.tool_definitions:
self.logger.debug(
f" {tool['function']['name']}: {tool['function']['description']}"
)
while True:
# Call the LLM with the prompt and tools
response = self._completion(messages)
if self.litellm_kwargs.get("stream"):
return self._stream_response(response)
for choice in response.choices:
# Handle tool calls if present
if (
hasattr(choice.message, "tool_calls")
and choice.message.tool_calls
):
tool_calls = choice.message.tool_calls
messages.append(choice.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:
try:
function_response = self.tools[function_name](
**function_args
)
except Exception as e:
self.logger.error(
f"Error calling tool {function_name}({function_args}): {e}"
)
function_response = f"Error calling tool {function_name}({function_args}): {e}"
msg = {
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": to_json(function_response),
}
messages.append(msg)
# GPT and Claude have `stop` when conversation is complete
# Gemini has `stop` as a finish reason when tools are used
elif choice.finish_reason in ["stop", "max_tokens", "length"]:
generated_text = choice.message.content
return self._parse_and_validate_response(
generated_text,
return_type=return_type,
json_schema=self.json_schema,
)
# Add methods explicitly
wrapper.tool = self.tool
wrapper.clear = self.clear
wrapper.message = self.message
# 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()
class CustomJSONEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
if hasattr(o, "__dict__"):
return o.__dict__
return str(o)
def to_json(obj: Any) -> str:
return json.dumps(obj, cls=CustomJSONEncoder, ensure_ascii=False)
llm = Promptic.decorate