forked from i-am-alice/2nd-devs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
C04L03_gnome.py
46 lines (37 loc) · 1.6 KB
/
C04L03_gnome.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
import sys
sys.path.append(r'..')
from task_handler import get_task_token, get_task_info_from_token, send_answer_by_task_token, apikey
# --------------------------------------------------------------
# Get task data
# --------------------------------------------------------------
import json
task_token = get_task_token(taskname='gnome', apikey=apikey)
task_data = get_task_info_from_token(task_token)
print(json.dumps(task_data, indent=4, ensure_ascii=False))
url = task_data['url']
# --------------------------------------------------------------
# Get answer with using OpenAI Vision model
# --------------------------------------------------------------
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage
human_message = \
"""I will give you a drawing of a gnome with a hat on his head.
Tell me what is the color of the hat in POLISH.
If any errors occur (f.e image does not have gnome) return "ERROR" as answer
"""
chat = ChatOpenAI(model="gpt-4-vision-preview", max_tokens=256)
response = chat.invoke([
HumanMessage(content=[
{"type": "text", "text": human_message},
{"type": "image_url", "image_url": {"url": url,"detail": "auto"}}
])
])
print(response.content)
# --------------------------------------------------------------
# Prepare answer
# --------------------------------------------------------------
data = {"answer": response.content}
# --------------------------------------------------------------
# send answer
# --------------------------------------------------------------
response = send_answer_by_task_token(task_token, data)