-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprompt.py
47 lines (39 loc) · 1.33 KB
/
prompt.py
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
import requests
def sent_request_to_openai(prompt, base64_image, api_key):
# this code is based on https://platform.openai.com/docs/guides/vision/uploading-base-64-encoded-images
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
},
],
}
],
"max_tokens": 4096,
}
response = requests.post(
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload
)
r = response.json()
print(r)
text_response = r["choices"][0]["message"]["content"]
import re
# Regular expression to find the block of text
pattern = r"```python(.*?)```"
match = re.search(pattern, text_response, re.DOTALL)
if match:
code_inside = match.group(
1
).strip() # .strip() to remove leading/trailing whitespace
# print(code_inside)
else:
print("No code block found")
# print(code_inside)
return code_inside