-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
50 lines (40 loc) · 1.16 KB
/
utils.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
import openai
import os
def image_to_bytes(filename):
with open(filename, "rb") as image_file:
image_bytes = image_file.read()
return image_bytes
def get_image_message_structure(image_bytes, question: str):
messages = [
{
"role": "user",
"content": [
{"image": {"format": "png", "source": {"bytes": image_bytes}}},
{"text": "Given the image, answer the following question: " + question},
],
}
]
return messages
def get_text_message_structure(chunks, question: str):
message = [
{
"role": "user",
"content": [
{
"text": f"given these chunks: {chunks} \n\n answer the following question: {question}"
}
],
}
]
return message
def embedding_function(texts, model="text-embedding-3-large"):
if isinstance(texts, str):
texts = [texts]
try:
texts = [t.replace("\n", " ") for t in texts]
except:
pass
return [
data.embedding
for data in openai.embeddings.create(input=texts, model=model).data
]