-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_open.py
91 lines (78 loc) · 2.46 KB
/
test_open.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# from openai import OpenAI
# client = OpenAI()
# completion = client.chat.completions.create(
# model="gpt-3.5-turbo",
# messages=[
# {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
# {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
# ]
# )
# print(completion.choices[0].message)
# from openai import OpenAI
# client = OpenAI()
# response = client.chat.completions.create(
# model="gpt-4-turbo",
# messages=[
# {
# "role": "user",
# "content": [
# {"type": "text", "text": "What’s in this image?"},
# {
# "type": "image_url",
# "image_url": {
# "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
# },
# },
# ],
# }
# ],
# max_tokens=300,
# )
# print(response.choices[0])
# Adapted from OpenAI's Vision example
# Adapted from OpenAI's Vision example
from openai import OpenAI
import base64
import requests
# Point to the local server
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
# Ask the user for a path on the filesystem:
path = input("Enter a local filepath to an image: ")
# Read the image and encode it to base64:
base64_image = ""
try:
image = open(path.replace("'", ""), "rb").read()
base64_image = base64.b64encode(image).decode("utf-8")
except:
print("Couldn't read the image. Make sure the path is correct and the file exists.")
exit()
completion = client.chat.completions.create(
model="Repository/obsidian-3b-multimodal-q6-gguf",
messages=[
{
"role": "system",
"content": "This is a chat between a user and an assistant. The assistant is helping the user to describe an image.",
},
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
max_tokens=1000,
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
for chunk in completion:
print(chunk)
# if chunk.choices[0].delta.content:
# print(chunk.choices[0].delta.content, end="", flush=True)