-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmultimodal.py
66 lines (53 loc) · 2.22 KB
/
multimodal.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import base64
import requests # type: ignore
import time
from literalai import LiteralClient
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
openai_client = OpenAI()
literalai_client = LiteralClient()
literalai_client.instrument_openai()
def encode_image(url):
return base64.b64encode(requests.get(url).content)
@literalai_client.step(type="run")
def generate_answer(user_query, image_url):
completion = openai_client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": user_query},
{
"type": "image_url",
"image_url": {"url": image_url},
},
],
},
],
max_tokens=300,
)
return completion.choices[0].message.content
def main():
with literalai_client.thread(name="Meal Analyzer") as thread:
welcome_message = "Welcome to the meal analyzer, please upload an image of your plate!"
literalai_client.message(content=welcome_message, type="assistant_message", name="My Assistant")
user_query = "Is this a healthy meal?"
user_image = "https://www.eatthis.com/wp-content/uploads/sites/4/2021/05/healthy-plate.jpg"
user_step = literalai_client.message(content=user_query, type="user_message", name="User")
time.sleep(1) # to make sure the user step has arrived at Literal AI
# create and link the image attachment to the user message step
literalai_client.api.create_attachment(
thread_id = thread.id,
step_id = user_step.id,
name = "meal_image",
content = encode_image(user_image)
)
answer = generate_answer(user_query=user_query, image_url=user_image)
literalai_client.message(content=answer, type="assistant_message", name="My Assistant")
main()
# Network requests by the SDK are performed asynchronously.
# Invoke flush_and_stop() to guarantee the completion of all requests prior to the process termination.
# WARNING: If you run a continuous server, you should not use this method.
literalai_client.flush_and_stop()