Skip to content

Commit

Permalink
add a b64encode-to-str to cache bc you can't put bytes in json either
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Lindeman <[email protected]>
  • Loading branch information
HenryL27 committed Jan 30, 2025
1 parent 8b6f085 commit 763acc5
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/sycamore/sycamore/llms/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import ABC, abstractmethod
from enum import Enum
import pickle
import base64
from PIL import Image
from typing import Any, Optional
import pydantic
Expand Down Expand Up @@ -129,6 +130,7 @@ def _llm_cache_get(self, prompt: RenderedPrompt, llm_kwargs: Optional[dict]) ->
key = self._llm_cache_key(prompt, llm_kwargs)
hit = self._cache.get(key)
if hit:
hit = base64.b64decode(hit)
hit = pickle.loads(hit)
assert (
len(hit) == 5
Expand All @@ -155,17 +157,19 @@ def _llm_cache_set(self, prompt: RenderedPrompt, llm_kwargs: Optional[dict], res
assert self._cache is not None, "make mypy happy"

key = self._llm_cache_key(prompt, llm_kwargs)
databytes = pickle.dumps(
{
"prompt": RenderedPrompt(messages=prompt.messages),
"prompt.response_format": self._pickleable_response_format(prompt),
"llm_kwargs": llm_kwargs,
"model_name": self._model_name,
"result": result,
}
)
datastr = base64.b64encode(databytes).decode("utf-8")
self._cache.set(
key,
pickle.dumps(
{
"prompt": RenderedPrompt(messages=prompt.messages),
"prompt.response_format": self._pickleable_response_format(prompt),
"llm_kwargs": llm_kwargs,
"model_name": self._model_name,
"result": result,
}
),
datastr,
)

def get_metadata(self, kwargs, response_text, wall_latency, in_tokens, out_tokens) -> dict:
Expand Down

0 comments on commit 763acc5

Please sign in to comment.