You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

import time
from openai import OpenAI
from WxMini.Milvus.Config.MulvusConfig import *
client = OpenAI(
api_key=MODEL_API_KEY,
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
image_url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/ctdzex/biaozhun.jpg"
completion = client.chat.completions.create(
model="qwen-vl-ocr",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": image_url,
"min_pixels": 28 * 28 * 4,
"max_pixels": 28 * 28 * 1280
},
# 为保证识别效果,目前模型内部会统一使用"Read all the text in the image."进行识别,用户输入的文本不会生效。
{"type": "text", "text": "Read all the text in the image."},
]
}
],
stream=True
)
full_content = ""
print("流式输出内容为:")
for chunk in completion:
if chunk.choices[0].delta.content is None:
continue
# 遍历每个字符并逐个输出
for char in chunk.choices[0].delta.content:
if char!=' ':
print(char, end="", flush=True) # 逐个输出字符,不换行
time.sleep(0.1) # 控制输出速度
full_content += chunk.choices[0].delta.content