Add PDF support for openai_conversation.generate_content service (#141588)

Add PDF support for openai_conversation.generate_content service
This commit is contained in:
Denis Shulyaka 2025-03-28 03:05:54 +03:00 committed by GitHub
parent 31479056ed
commit 195919b5fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 14 deletions

View File

@ -11,6 +11,7 @@ from openai.types.images_response import ImagesResponse
from openai.types.responses import (
EasyInputMessageParam,
Response,
ResponseInputFileParam,
ResponseInputImageParam,
ResponseInputMessageContentListParam,
ResponseInputParam,
@ -132,19 +133,28 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
if not Path(filename).exists():
raise HomeAssistantError(f"`{filename}` does not exist")
mime_type, base64_file = encode_file(filename)
if "image/" not in mime_type:
if "image/" in mime_type:
content.append(
ResponseInputImageParam(
type="input_image",
file_id=filename,
image_url=f"data:{mime_type};base64,{base64_file}",
detail="auto",
)
)
elif "application/pdf" in mime_type:
content.append(
ResponseInputFileParam(
type="input_file",
filename=filename,
file_data=f"data:{mime_type};base64,{base64_file}",
)
)
else:
raise HomeAssistantError(
"Only images are supported by the OpenAI API,"
f"`{filename}` is not an image file"
"Only images and PDF are supported by the OpenAI API,"
f"`{filename}` is not an image file or PDF"
)
content.append(
ResponseInputImageParam(
type="input_image",
file_id=filename,
image_url=f"data:{mime_type};base64,{base64_file}",
detail="auto",
)
)
if CONF_FILENAMES in call.data:
await hass.async_add_executor_job(append_files_to_content)

View File

@ -89,7 +89,7 @@
},
"generate_content": {
"name": "Generate content",
"description": "Sends a conversational query to ChatGPT including any attached image files",
"description": "Sends a conversational query to ChatGPT including any attached image or PDF files",
"fields": {
"config_entry": {
"name": "Config entry",

View File

@ -262,6 +262,27 @@ async def test_init_error(
},
0,
),
(
{"prompt": "Picture of a dog", "filenames": ["/a/b/c.pdf"]},
{
"input": [
{
"content": [
{
"type": "input_text",
"text": "Picture of a dog",
},
{
"type": "input_file",
"file_data": "data:application/pdf;base64,BASE64IMAGE1",
"filename": "/a/b/c.pdf",
},
],
},
],
},
1,
),
(
{"prompt": "Picture of a dog", "filenames": ["/a/b/c.jpg"]},
{
@ -415,8 +436,8 @@ async def test_generate_content_service(
[True, False],
),
(
{"prompt": "Not a picture of a dog", "filenames": ["/a/b/c.pdf"]},
"Only images are supported by the OpenAI API,`/a/b/c.pdf` is not an image file",
{"prompt": "Not a picture of a dog", "filenames": ["/a/b/c.mov"]},
"Only images and PDF are supported by the OpenAI API,`/a/b/c.mov` is not an image file or PDF",
1,
[True],
[True],