| from openai import OpenAI | |
| import base64 | |
| # API configuration | |
| openai_api_key = "token-abc123" # API key must match the one set when launching the service | |
| openai_api_base = "http://localhost:8000/v1" | |
| client = OpenAI( | |
| api_key=openai_api_key, | |
| base_url=openai_api_base, | |
| ) | |
| # Read and encode local image | |
| with open('/mnt/data/xiuying/Code/xywang/demo.jpeg', 'rb') as file: | |
| image = "data:image/jpeg;base64," + base64.b64encode(file.read()).decode('utf-8') | |
| chat_response = client.chat.completions.create( | |
| model="MiniCPM-V-4-Q4_K_M.gguf", # Specify model path or HuggingFace ID | |
| messages=[{ | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": "Please describe this image"}, | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": image, # Supports network image URLs | |
| }, | |
| }, | |
| ], | |
| }], | |
| extra_body={ | |
| "stop_token_ids": [1, 73440] | |
| } | |
| ) | |
| print("Chat response:", chat_response) | |
| print("Chat response content:", chat_response.choices[0].message.content) |