Skip to content

Commit

Permalink
Finish up caching for Python (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Dec 16, 2024
2 parents 3027324 + 0f671ea commit b006923
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 95 deletions.
3 changes: 2 additions & 1 deletion python/example-pytest-selfie/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ readme = "README.md"
requires-python = ">=3.9"
dependencies = [
"flask>=3.0.3",
"openai>=1.0.0",
]

[tool.uv.sources]
Expand All @@ -24,9 +23,11 @@ pytest-selfie = { path = "../pytest-selfie", editable = true }
dev = [
"beautifulsoup4>=4.12.3",
"markdownify>=0.12.1",
"openai>=1.0.0",
"pyright>=1.1.350",
"pytest-selfie>=0.1.0",
"pytest>=8.0.0",
"requests>=2.32.3",
"ruff>=0.5.0",
"selfie-lib>=0.1.0",
"werkzeug>=3.0.3",
Expand Down
Binary file added python/example-pytest-selfie/self-portrait.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion python/example-pytest-selfie/test.jpg

This file was deleted.

12 changes: 0 additions & 12 deletions python/example-pytest-selfie/tests/cache_selfie_test.py

This file was deleted.

85 changes: 85 additions & 0 deletions python/example-pytest-selfie/tests/cache_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import random

import requests
from openai import OpenAI
from selfie_lib import cache_selfie, cache_selfie_binary, cache_selfie_json


def test_cache_selfie():
cache_selfie(lambda: str(random.random())).to_be("0.06699295946441819")


def openai():
return OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))


def test_gen_ai():
# Fetch the chat response with caching
chat: dict = cache_selfie_json(
lambda: openai()
.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": "Expressive but brief language describing a robot creating a self portrait.",
}
],
)
.to_dict()
).to_be("""{
"id": "chatcmpl-Af1Nf34netAfGW7ZIQArEHavfuYtg",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "A sleek robot, its mechanical fingers dancing with precision, deftly wields a brush against the canvas. Whirs and clicks echo softly as vibrant strokes emerge, each infused with an unexpected soulfulness. Metal meets art as synthetic imagination captures its own intricate reflection\\u2014a symphony of circuitry bathed in delicate hues.",
"refusal": null,
"role": "assistant"
}
}
],
"created": 1734340119,
"model": "gpt-4o-2024-08-06",
"object": "chat.completion",
"system_fingerprint": "fp_9faba9f038",
"usage": {
"completion_tokens": 62,
"prompt_tokens": 20,
"total_tokens": 82,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_tokens_details": {
"audio_tokens": 0,
"cached_tokens": 0
}
}
}""")
# raise ValueError(f"KEYS={chat.keys()} TYPE={type(chat)}")
image_url: dict = cache_selfie_json(
lambda: openai()
.images.generate(
model="dall-e-3", prompt=chat["choices"][0]["message"]["content"]
)
.to_dict()
).to_be("""{
"created": 1734340142,
"data": [
{
"revised_prompt": "Visualize a sleek robot adorned in a metallic shell. Its highly precise mechanical digits engage rhythmically with a paintbrush, swirling it flawlessly over a robust canvas. The environment is immersed in resonating mechanical sounds blended with the aura of creativity unfurling. Strikingly vivid strokes of paint materialize from the robot's calculated artistry, each stroke conveying a depth and emotion unanticipated of a mechanical entity. This metallic artist exhibits its self-inspired art by meticulously crafting its own intricate reflection\\u2014an orchestra of electronics bathed in a palette of gentle colors.",
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-SUepmbCtftBix3RViJYKuYKY/user-KFRqcsnjZPSTulNaxrY5wjL3/img-JVxDCOAuLoIky3ucNNJWo7fG.png?st=2024-12-16T08%3A09%3A02Z&se=2024-12-16T10%3A09%3A02Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-16T00%3A47%3A43Z&ske=2024-12-17T00%3A47%3A43Z&sks=b&skv=2024-08-04&sig=nIiMMZBNnqPO2jblJ8pDvWS2AFTOaicAWAD6BDqP9jU%3D"
}
]
}""")

url = image_url["data"][0]["url"]
cache_selfie_binary(lambda: requests.get(url).content).to_be_file(
"self-portrait.png"
)
118 changes: 113 additions & 5 deletions python/example-pytest-selfie/uv.lock

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions python/selfie-lib/selfie_lib/CacheSelfie.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ def cache_selfie(
raise TypeError("Invalid arguments provided to cache_selfie")


def cache_selfie_json(to_cache: Callable[..., T]) -> "CacheSelfie[T]":
return cache_selfie(to_cache, Roundtrip.json())


@overload
def cache_selfie_binary(
to_cache: Callable[..., bytes],
) -> "CacheSelfieBinary[bytes]": ...


@overload
def cache_selfie_binary(
to_cache: Callable[..., T], roundtrip: Roundtrip[T, bytes]
) -> "CacheSelfieBinary[T]": ...


def cache_selfie_binary(
to_cache: Union[Callable[..., bytes], Callable[..., T]],
roundtrip: Optional[Roundtrip[T, bytes]] = None,
) -> Union["CacheSelfieBinary[bytes]", "CacheSelfieBinary[T]"]:
if roundtrip is None:
# the cacheable better be a bytes!
return cache_selfie_binary(to_cache, Roundtrip.identity()) # type: ignore
elif isinstance(roundtrip, Roundtrip) and to_cache is not None:
deferred_disk_storage = _selfieSystem().disk_thread_local()
return CacheSelfieBinary(deferred_disk_storage, roundtrip, to_cache) # type: ignore
else:
raise TypeError("Invalid arguments provided to cache_selfie")


class CacheSelfie(Generic[T]):
def __init__(
self,
Expand Down
14 changes: 14 additions & 0 deletions python/selfie-lib/selfie_lib/Roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ def parse(self, serialized: Any) -> Any:
return serialized

return Identity()

@classmethod
def json(cls) -> "Roundtrip[T, str]":
"""Return a Roundtrip that serializes to/from JSON strings."""
import json

class JsonRoundtrip(Roundtrip[Any, str]):
def serialize(self, value: Any) -> str:
return json.dumps(value, indent=4)

def parse(self, serialized: str) -> Any:
return json.loads(serialized)

return JsonRoundtrip()
2 changes: 2 additions & 0 deletions python/selfie-lib/selfie_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .ArrayMap import ArraySet as ArraySet
from .Atomic import AtomicReference as AtomicReference
from .CacheSelfie import cache_selfie as cache_selfie
from .CacheSelfie import cache_selfie_binary as cache_selfie_binary
from .CacheSelfie import cache_selfie_json as cache_selfie_json
from .CommentTracker import CommentTracker as CommentTracker
from .FS import FS as FS
from .Lens import Camera as Camera
Expand Down
Binary file added selfie.dev/public/dalle-3-jvm.webp
Binary file not shown.
Binary file added selfie.dev/public/dalle-3-py.webp
Binary file not shown.
2 changes: 1 addition & 1 deletion selfie.dev/src/pages/jvm/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ cacheSelfieBinary { HttpClient().request(images[0].url).readBytes() }

Since we used `toBeFile`, we can open `com/example/kotest/dalle-3.png` in Mac Preview / Windows Explorer.

<img alt="Robot self portrait" src="/dalle-3.webp" width="400px"/>
<img alt="Robot self portrait" src="/dalle-3-jvm.webp" width="400px"/>

*Pull requests to improve the landing page and documentation are greatly appreciated, you can find the [source code here](https://github.com/diffplug/selfie).*
Loading

0 comments on commit b006923

Please sign in to comment.