-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
317 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,3 +84,5 @@ | |
] | ||
CONF_ATTACH_USERNAME = "attach_username" | ||
DEFAULT_ATTACH_USERNAME = False | ||
|
||
SERVICE_QUERY_IMAGE = "query_image" |
76 changes: 76 additions & 0 deletions
76
custom_components/extended_openai_conversation/services.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import logging | ||
|
||
import voluptuous as vol | ||
from openai import AsyncOpenAI | ||
from openai._exceptions import OpenAIError | ||
|
||
from homeassistant.core import ( | ||
HomeAssistant, | ||
ServiceCall, | ||
ServiceResponse, | ||
SupportsResponse, | ||
) | ||
from homeassistant.exceptions import HomeAssistantError | ||
from homeassistant.helpers.typing import ConfigType | ||
from homeassistant.helpers import selector, config_validation as cv | ||
|
||
from .const import DOMAIN, SERVICE_QUERY_IMAGE | ||
|
||
QUERY_IMAGE_SCHEMA = vol.Schema( | ||
{ | ||
vol.Required("config_entry"): selector.ConfigEntrySelector( | ||
{ | ||
"integration": DOMAIN, | ||
} | ||
), | ||
vol.Required("model", default="gpt-4-vision-preview"): cv.string, | ||
vol.Required("prompt"): cv.string, | ||
vol.Required("images"): vol.All(cv.ensure_list, [{"url": cv.url}]), | ||
vol.Optional("max_tokens", default=300): cv.positive_int, | ||
} | ||
) | ||
|
||
_LOGGER = logging.getLogger(__package__) | ||
|
||
|
||
async def async_setup_services(hass: HomeAssistant, config: ConfigType) -> None: | ||
"""Set up services for the extended openai conversation component.""" | ||
|
||
async def query_image(call: ServiceCall) -> ServiceResponse: | ||
"""Query an image.""" | ||
try: | ||
model = call.data["model"] | ||
images = [ | ||
{"type": "image_url", "image_url": image} | ||
for image in call.data["images"] | ||
] | ||
|
||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": [{"type": "text", "text": call.data["prompt"]}] + images, | ||
} | ||
] | ||
_LOGGER.info("Prompt for %s: %s", model, messages) | ||
|
||
response = await AsyncOpenAI( | ||
api_key=hass.data[DOMAIN][call.data["config_entry"]]["api_key"] | ||
).chat.completions.create( | ||
model=model, | ||
messages=messages, | ||
max_tokens=call.data["max_tokens"], | ||
) | ||
response_dict = response.model_dump() | ||
_LOGGER.info("Response %s", response_dict) | ||
except OpenAIError as err: | ||
raise HomeAssistantError(f"Error generating image: {err}") from err | ||
|
||
return response_dict | ||
|
||
hass.services.async_register( | ||
DOMAIN, | ||
SERVICE_QUERY_IMAGE, | ||
query_image, | ||
schema=QUERY_IMAGE_SCHEMA, | ||
supports_response=SupportsResponse.ONLY, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.