mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-08 22:21:35 +00:00
fix: remove slow string operation (#14955)
* fix: remove slow string operation * fix: behavior change * fix: behavior change * fix: avoid default call
This commit is contained in:
parent
35930d7ccb
commit
2973ff8be9
2 changed files with 16 additions and 6 deletions
|
|
@ -23,6 +23,11 @@ class Rules:
|
|||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def has_pre_call_rules() -> bool:
|
||||
"""Check if any pre-call rules are configured"""
|
||||
return len(litellm.pre_call_rules) > 0
|
||||
|
||||
def pre_call_rules(self, input: str, model: str):
|
||||
for rule in litellm.pre_call_rules:
|
||||
if callable(rule):
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#
|
||||
# Thank you users! We ❤️ you! - Krrish & Ishaan
|
||||
|
||||
from io import StringIO
|
||||
import ast
|
||||
import asyncio
|
||||
import base64
|
||||
|
|
@ -724,17 +725,21 @@ def function_setup( # noqa: PLR0915
|
|||
messages = kwargs["messages"]
|
||||
### PRE-CALL RULES ###
|
||||
if (
|
||||
isinstance(messages, list)
|
||||
Rules.has_pre_call_rules()
|
||||
and isinstance(messages, list)
|
||||
and len(messages) > 0
|
||||
and isinstance(messages[0], dict)
|
||||
and "content" in messages[0]
|
||||
):
|
||||
|
||||
buffer = StringIO()
|
||||
for m in messages:
|
||||
content = m.get("content", "")
|
||||
if content is not None and isinstance(content, str):
|
||||
buffer.write(content)
|
||||
|
||||
rules_obj.pre_call_rules(
|
||||
input="".join(
|
||||
m.get("content", "")
|
||||
for m in messages
|
||||
if "content" in m and isinstance(m["content"], str)
|
||||
),
|
||||
input=buffer.getvalue(),
|
||||
model=model,
|
||||
)
|
||||
elif (
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue