พัฒนาแอปการสนทนาตามวิสัยทัศน์
Tip
ดูแท็บ ข้อความและรูปภาพ สําหรับรายละเอียดเพิ่มเติม!
ในการพัฒนาแอปไคลเอ็นต์ที่มีส่วนร่วมในการแชทตามการมองเห็นด้วยแบบจําลองหลายรูปแบบ คุณสามารถใช้เทคนิคพื้นฐานเดียวกันกับที่ใช้สําหรับการสนทนาโดยใช้ข้อความได้ คุณจําเป็นต้องเชื่อมต่อกับจุดสิ้นสุดที่มีการปรับใช้แบบจําลอง และคุณใช้จุดสิ้นสุดนั้นเพื่อส่งพร้อมท์ที่ประกอบด้วยข้อความไปยังแบบจําลอง และประมวลผลคําตอบ
ข้อแตกต่างที่สําคัญคือข้อความแจ้งสําหรับการแชทตามการมองเห็นรวมถึงข้อความของผู้ใช้หลายส่วนที่มีทั้งรายการเนื้อหาข้อความและรายการเนื้อหารูปภาพ
ส่งข้อความแจ้งตามรูปภาพโดยใช้ Responses API
หากต้องการรวมรูปภาพในข้อความแจ้งโดยใช้ Responses API ให้ระบุ URL สําหรับไฟล์รูปภาพบนเว็บ หรือโหลดรูปภาพในเครื่องและเข้ารหัสข้อมูลในรูปแบบ Base64 แล้วส่ง URL ในรูปแบบ data:image/jpeg;base64,{image_data} (แทนที่ "jpeg" ด้วย "png" หรือรูปแบบอื่นๆ ตามความเหมาะสม)
ตัวอย่าง Python ต่อไปนี้แสดงวิธีการส่งรูปภาพในพรอมต์โดยใช้ Responses API:
# Read the image data from a local file
image_path = Path("dragon-fruit.jpeg")
image_format = "jpeg"
with open(image_path, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode("utf-8")
data_url = f"data:image/{image_format};base64,{image_data}" # You can also use a web URL
# Send the image data in a prompt to the model
response = client.responses.create(
model="gpt-4.1",
input=[
{"role": "developer", "content": "You are an AI assistant for chefs planning recipes."},
{"role": "user", "content": [
{ "type": "input_text", "text": "What desserts could I make with this?"},
{ "type": "input_image", "image_url": data_url}
] }
]
)
print(response.output_text)
ส่งข้อความแจ้งตามรูปภาพโดยใช้ ChatCompletions API
เมื่อใช้จุดสิ้นสุด Azure OpenAI เพื่อส่งพร้อมท์ไปยังแบบจําลองที่ไม่รองรับ Responses API คุณสามารถใช้ ChatCompletions API ได้ดังนี้:
# Read the image data from a local file
image_path = Path("orange.jpeg")
image_format = "jpeg"
with open(image_path, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode("utf-8")
data_url = f"data:image/{image_format};base64,{image_data}" # You can also use a web URL
# Send the image data in a prompt to the model
response = client.chat.completions.create(
model="Phi-4-multimodal-instruct",
messages=[
{"role": "system", "content": "You are an AI assistant for chefs planning recipes."},
{ "role": "user", "content": [
{ "type": "text", "text": "What can I make with this fruit?"},
{ "type": "image_url", "image_url": {"url": data_url}}
] }
]
)
print(response.choices[0].message.content)