Files
chat-bot/test/ts.py
lychang 64ce30fdfd init
2025-08-26 09:35:29 +08:00

50 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import yaml
import base64
import requests
import os
from openai import OpenAI
api_key = "ollama" # 示例遮蔽后的API密钥
api_url = "http://192.168.1.95:11434/v1"
client = OpenAI(api_key=api_key, base_url=api_url)
def get_common_advice(image_path, prompt):
# 内部函数编码图像为Base64
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# 获取Base64字符串
base64_image = encode_image(image_path)
response = client.chat.completions.create(
model="minicpm-v:latest",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
}
}
]
}
]
)
return response.choices[0].message.content
if __name__ == '__main__':
image_path = "img.png"
prompt = "图片说了什么信息"
advice = get_common_advice(image_path, prompt)
print(advice)