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