Skip to content

Commit d617c0b

Browse files
authored
Added vision support to api_like_OAI (#524)
1 parent a384fd7 commit d617c0b

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

llama.cpp/server/api_like_OAI.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def is_present(json, key):
3535
#convert chat to prompt
3636
def convert_chat(messages):
3737
prompt = "" + args.chat_prompt.replace("\\n", "\n")
38+
image_data = []
39+
image_id = 1
3840

3941
system_n = args.system_name.replace("\\n", "\n")
4042
user_n = args.user_name.replace("\\n", "\n")
@@ -46,17 +48,35 @@ def convert_chat(messages):
4648
if (line["role"] == "system"):
4749
prompt += f"{system_n}{line['content']}"
4850
if (line["role"] == "user"):
49-
prompt += f"{user_n}{line['content']}"
51+
# content can either be a string or an iterable with "text"
52+
# and "image_url" elements
53+
content = line['content']
54+
if type(content) == str:
55+
prompt += f"{user_n}{line['content']}"
56+
else:
57+
# add all elements from array
58+
for content_part in content:
59+
if content_part['type'] == "text":
60+
prompt += f"{user_n}{content_part['text']}{stop}"
61+
elif content_part['type'] == "image_url":
62+
image_data.append(
63+
{"data": content_part['image_url']['url'].split(",")[1],
64+
"id": image_id})
65+
image_id+=1
66+
5067
if (line["role"] == "assistant"):
5168
prompt += f"{ai_n}{line['content']}{stop}"
5269
prompt += ai_n.rstrip()
5370

54-
return prompt
71+
return prompt, image_data
5572

5673
def make_postData(body, chat=False, stream=False):
5774
postData = {}
5875
if (chat):
59-
postData["prompt"] = convert_chat(body["messages"])
76+
prompt, image_data = convert_chat(body["messages"])
77+
postData["prompt"] = prompt
78+
if len(image_data) > 0:
79+
postData["image_data"] = image_data
6080
else:
6181
postData["prompt"] = body["prompt"]
6282
if(is_present(body, "temperature")): postData["temperature"] = body["temperature"]

0 commit comments

Comments
 (0)