-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathassist.py
More file actions
104 lines (94 loc) · 3.9 KB
/
Copy pathassist.py
File metadata and controls
104 lines (94 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import logging
from openai import OpenAI, OpenAIError
import settings
logger = logging.getLogger(__name__)
def get_client(api_key: str | None = None):
"""Get an OpenAI API Client"""
if not api_key:
api_key = settings.OPENAI_API_KEY
if not api_key:
raise OpenAIError('The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable')
return OpenAI(api_key=api_key or settings.OPENAI_API_KEY)
def generate_cocktails(pump_to_drink: dict, requests_for_bartender: str = '', exclude_existing: bool = True, api_key: str | None = None) -> dict:
"""Generate a JSON list of cocktails"""
prompt = (
'You are a creative cocktail mixologist. Based on the following pump configuration, '
'generate a list of cocktail recipes. For each cocktail, provide a normal cocktail name, '
'a fun cocktail name, and a dictionary of ingredients (with their measurements, e.g., "2 oz").\n\n'
'Please output only valid JSON that follows this format:\n\n'
'{\n'
' "cocktails": [\n'
' {\n'
' "normal_name": "Margarita",\n'
' "fun_name": "Citrus Snap",\n'
' "ingredients": {\n'
' "Tequila": "2 oz",\n'
' "Triple Sec": "1 oz",\n'
' "Lime Juice": "1 oz"\n'
' }\n'
' }\n'
' ]\n'
'}\n\n'
'Now, use the following pump configuration creatively to generate your cocktail recipes:\n'
f'{json.dumps(pump_to_drink, indent=2)}\n\n'
)
if exclude_existing:
from helpers import load_cocktails
prompt += (
'Do not include the following cocktails, which I already have recipes for:\n\n'
f'{json.dumps(load_cocktails(), indent=2)}\n\n'
)
if requests_for_bartender.strip():
prompt += f'Requests for the bartender: {requests_for_bartender.strip()}\n'
try:
client = get_client(api_key=api_key)
completion = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{
'role': 'system',
'content': (
'You are a creative cocktail mixologist. Generate cocktail recipes in JSON format. '
'Make sure your entire response is a valid JSON object.'
)
},
{'role': 'user', 'content': prompt}
],
response_format={'type': 'json_object'},
)
json_output = completion.choices[0].message.content
data = json.loads(json_output)
return data
except Exception as e:
logger.exception('Error generating cocktails')
raise e
def generate_image(prompt: str, api_key: str | None = None, use_gpt_transparency: bool | None = None) -> str:
"""Generate an image using OpenAI"""
if use_gpt_transparency is None:
use_gpt_transparency = settings.USE_GPT_TRANSPARENCY
try:
generation_kwargs = {
'model': 'dall-e-3',
'prompt': prompt,
'size': '1024x1024',
'quality': 'standard',
'n': 1,
}
if use_gpt_transparency:
generation_kwargs.update({
'model': 'gpt-image-1',
'background': 'transparent',
'output_format': 'png',
'quality': 'auto'
})
else:
generation_kwargs.update({
'response_format': 'b64_json'
})
client = get_client(api_key)
response = client.images.generate(**generation_kwargs)
image_url = response.data[0].b64_json
return image_url
except Exception as e:
raise Exception(f'Image generation error')