ExerciseOCR/test.py

36 lines
937 B
Python
Raw Normal View History

2024-10-25 12:53:51 +00:00
import base64
import openai
with open("test.png", "rb") as f:
image_data = f.read()
image_base64 = base64.b64encode(image_data).decode('utf-8')
with open("key", "r") as f:
key = f.read()
client = openai.OpenAI(api_key=key,
base_url="https://open.bigmodel.cn/api/paas/v4/"
)
# 构造请求给API
response = client.chat.completions.create(
model="glm-4v",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "请描述该图片",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_base64}"
},
},
],
}
],
stream=True
)
for chunk in response:
print(chunk.choices[0].delta.content)