Skip to content

Commit

Permalink
add chat_completions_with_image api
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Oct 18, 2024
1 parent fb91cf7 commit cc3fa8c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion workshop_azure_iot/routers/ai_services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
from logging import getLogger

from fastapi import APIRouter, status
from fastapi import APIRouter, UploadFile, status
from fastapi.responses import JSONResponse

from workshop_azure_iot.internals.ai_services import Client
Expand Down Expand Up @@ -36,3 +37,37 @@ async def chat_completions(prompt: str):
status_code=status.HTTP_200_OK,
content=response,
)


@router.post("/chat/completions_with_image")
async def chat_completions_with_image(
file: UploadFile,
prompt: str,
):
image = await file.read()
encoded_image = base64.b64encode(image).decode()
response = await client.chat_completions(
[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"},
},
{
"type": "text",
"text": prompt,
},
],
},
]
)
return JSONResponse(
status_code=status.HTTP_200_OK,
content=response,
)

0 comments on commit cc3fa8c

Please sign in to comment.