|
| 1 | +import os |
| 2 | +import random |
| 3 | + |
| 4 | +import requests |
| 5 | +from openai import OpenAI |
| 6 | +from selfie_lib import cache_selfie, cache_selfie_binary, cache_selfie_json |
| 7 | + |
| 8 | + |
| 9 | +def test_cache_selfie(): |
| 10 | + cache_selfie(lambda: str(random.random())).to_be("0.06699295946441819") |
| 11 | + |
| 12 | + |
| 13 | +def openai(): |
| 14 | + return OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) |
| 15 | + |
| 16 | + |
| 17 | +def test_gen_ai(): |
| 18 | + # Fetch the chat response with caching |
| 19 | + chat: dict = cache_selfie_json( |
| 20 | + lambda: openai() |
| 21 | + .chat.completions.create( |
| 22 | + model="gpt-4o", |
| 23 | + messages=[ |
| 24 | + { |
| 25 | + "role": "user", |
| 26 | + "content": "Expressive but brief language describing a robot creating a self portrait.", |
| 27 | + } |
| 28 | + ], |
| 29 | + ) |
| 30 | + .to_dict() |
| 31 | + ).to_be("""{ |
| 32 | + "id": "chatcmpl-Af1Nf34netAfGW7ZIQArEHavfuYtg", |
| 33 | + "choices": [ |
| 34 | + { |
| 35 | + "finish_reason": "stop", |
| 36 | + "index": 0, |
| 37 | + "logprobs": null, |
| 38 | + "message": { |
| 39 | + "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.", |
| 40 | + "refusal": null, |
| 41 | + "role": "assistant" |
| 42 | + } |
| 43 | + } |
| 44 | + ], |
| 45 | + "created": 1734340119, |
| 46 | + "model": "gpt-4o-2024-08-06", |
| 47 | + "object": "chat.completion", |
| 48 | + "system_fingerprint": "fp_9faba9f038", |
| 49 | + "usage": { |
| 50 | + "completion_tokens": 62, |
| 51 | + "prompt_tokens": 20, |
| 52 | + "total_tokens": 82, |
| 53 | + "completion_tokens_details": { |
| 54 | + "accepted_prediction_tokens": 0, |
| 55 | + "audio_tokens": 0, |
| 56 | + "reasoning_tokens": 0, |
| 57 | + "rejected_prediction_tokens": 0 |
| 58 | + }, |
| 59 | + "prompt_tokens_details": { |
| 60 | + "audio_tokens": 0, |
| 61 | + "cached_tokens": 0 |
| 62 | + } |
| 63 | + } |
| 64 | +}""") |
| 65 | + # raise ValueError(f"KEYS={chat.keys()} TYPE={type(chat)}") |
| 66 | + image_url: dict = cache_selfie_json( |
| 67 | + lambda: openai() |
| 68 | + .images.generate( |
| 69 | + model="dall-e-3", prompt=chat["choices"][0]["message"]["content"] |
| 70 | + ) |
| 71 | + .to_dict() |
| 72 | + ).to_be("""{ |
| 73 | + "created": 1734340142, |
| 74 | + "data": [ |
| 75 | + { |
| 76 | + "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.", |
| 77 | + "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" |
| 78 | + } |
| 79 | + ] |
| 80 | +}""") |
| 81 | + |
| 82 | + url = image_url["data"][0]["url"] |
| 83 | + cache_selfie_binary(lambda: requests.get(url).content).to_be_file( |
| 84 | + "self-portrait.png" |
| 85 | + ) |
0 commit comments