mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-09 03:18:44 +00:00
refactor(caching): keep semantic tool prompt helpers within lint budgets
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
f462fbeb2e
commit
8b08e07f18
2 changed files with 15 additions and 7 deletions
|
|
@ -276,7 +276,7 @@ class RedisSemanticCache(BaseCache):
|
|||
|
||||
@classmethod
|
||||
def _collect_responses_input_text(cls, value: object, prompt_parts: list[str]) -> None:
|
||||
value = cls._coerce_response_input_value(value)
|
||||
value = cls._function_call_as_prompt(cls._coerce_response_input_value(value))
|
||||
if value is None:
|
||||
return
|
||||
|
||||
|
|
@ -292,10 +292,6 @@ class RedisSemanticCache(BaseCache):
|
|||
return
|
||||
|
||||
if isinstance(value, dict):
|
||||
if value.get("type") == "function_call":
|
||||
prompt_parts.append(tool_call_str(value.get("name"), value.get("arguments")))
|
||||
return
|
||||
|
||||
content = value.get("content")
|
||||
if content is not None:
|
||||
cls._collect_responses_input_text(content, prompt_parts)
|
||||
|
|
@ -323,6 +319,12 @@ class RedisSemanticCache(BaseCache):
|
|||
prompt_parts.append(stripped_text)
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def _function_call_as_prompt(value: object) -> object:
|
||||
if isinstance(value, dict) and value.get("type") == "function_call":
|
||||
return tool_call_str(value.get("name"), value.get("arguments"))
|
||||
return value
|
||||
|
||||
@staticmethod
|
||||
def _coerce_response_input_value(value: object) -> object:
|
||||
model_dump: Final = getattr(value, "model_dump", None)
|
||||
|
|
|
|||
|
|
@ -205,7 +205,11 @@ def get_str_from_messages_with_tools(messages: object) -> str:
|
|||
|
||||
|
||||
def tool_call_str(name: object, arguments: object) -> str:
|
||||
return json.dumps({"name": name, "arguments": arguments}, separators=(",", ":"), default=str)
|
||||
return f'{{"name":{_compact_json(name)},"arguments":{_compact_json(arguments)}}}'
|
||||
|
||||
|
||||
def _compact_json(value: object) -> str:
|
||||
return json.dumps(value, separators=(",", ":"), default=str)
|
||||
|
||||
|
||||
def _message_str_with_tools(message: Mapping[str, object]) -> str:
|
||||
|
|
@ -234,7 +238,9 @@ def _block_str_with_tools(block: Mapping[str, object]) -> str:
|
|||
|
||||
|
||||
def _openai_tool_call_str(tool_call: Mapping[str, object]) -> str:
|
||||
function: Final = _as_str_mapping(tool_call.get("function")) or {}
|
||||
function: Final = _as_str_mapping(tool_call.get("function"))
|
||||
if function is None:
|
||||
return tool_call_str(None, None)
|
||||
return tool_call_str(function.get("name"), function.get("arguments"))
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue