50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
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) |