Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Avoid serializing Callable kwargs during Completion #1862

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions dspy/clients/lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import threading
import uuid
from datetime import datetime
from typing import Any, Dict, List, Literal, Optional
from typing import Any, Dict, List, Literal, Optional, Callable

import litellm
import ujson
Expand Down Expand Up @@ -84,6 +84,10 @@ def __call__(self, prompt=None, messages=None, **kwargs):
cache = kwargs.pop("cache", self.cache)
messages = messages or [{"role": "user", "content": prompt}]
kwargs = {**self.kwargs, **kwargs}
callable_kwargs = {}
for k, v in list(kwargs.items()):
if isinstance(v, Callable):
callable_kwargs[k] = kwargs.pop(k)

# Make the request and handle LRU & disk caching.
if self.model_type == "chat":
Expand All @@ -94,6 +98,7 @@ def __call__(self, prompt=None, messages=None, **kwargs):
response = completion(
request=ujson.dumps(dict(model=self.model, messages=messages, **kwargs)),
num_retries=self.num_retries,
**callable_kwargs,
)
outputs = [c.message.content if hasattr(c, "message") else c["text"] for c in response["choices"]]

Expand Down Expand Up @@ -213,16 +218,18 @@ def copy(self, **kwargs):


@functools.lru_cache(maxsize=None)
def cached_litellm_completion(request, num_retries: int):
def cached_litellm_completion(request, num_retries: int, **kwargs):
return litellm_completion(
request,
cache={"no-cache": False, "no-store": False},
num_retries=num_retries,
**kwargs,
)


def litellm_completion(request, num_retries: int, cache={"no-cache": True, "no-store": True}):
kwargs = ujson.loads(request)
def litellm_completion(request, num_retries: int, cache={"no-cache": True, "no-store": True}, **kwargs):
req_kwargs = ujson.loads(request)
kwargs = {**req_kwargs, **kwargs}
return litellm.completion(
num_retries=num_retries,
cache=cache,
Expand All @@ -231,17 +238,19 @@ def litellm_completion(request, num_retries: int, cache={"no-cache": True, "no-s


@functools.lru_cache(maxsize=None)
def cached_litellm_text_completion(request, num_retries: int):
def cached_litellm_text_completion(request, num_retries: int,**kwargs):
return litellm_text_completion(
request,
num_retries=num_retries,
cache={"no-cache": False, "no-store": False},
**kwargs,
)


def litellm_text_completion(request, num_retries: int, cache={"no-cache": True, "no-store": True}):
kwargs = ujson.loads(request)

def litellm_text_completion(request, num_retries: int, cache={"no-cache": True, "no-store": True},**kwargs):
req_kwargs = ujson.loads(request)
kwargs = {**req_kwargs, **kwargs}

# Extract the provider and model from the model string.
# TODO: Not all the models are in the format of "provider/model"
model = kwargs.pop("model").split("/", 1)
Expand Down
Loading