mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-30 03:43:55 +08:00
Update __init__.py
This commit is contained in:
@@ -5,7 +5,7 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import openai
|
from openai import AsyncOpenAI
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@@ -37,9 +37,13 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up HA Text AI from a config entry."""
|
"""Set up HA Text AI from a config entry."""
|
||||||
|
client = AsyncOpenAI(
|
||||||
|
api_key=entry.data[CONF_API_KEY],
|
||||||
|
base_url=entry.data.get(CONF_API_BASE, DEFAULT_API_BASE)
|
||||||
|
)
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
hass.data[DOMAIN][entry.entry_id] = {
|
||||||
CONF_API_KEY: entry.data[CONF_API_KEY],
|
"client": client,
|
||||||
CONF_API_BASE: entry.data.get(CONF_API_BASE, DEFAULT_API_BASE),
|
|
||||||
CONF_REQUEST_INTERVAL: entry.data.get(CONF_REQUEST_INTERVAL, DEFAULT_REQUEST_INTERVAL),
|
CONF_REQUEST_INTERVAL: entry.data.get(CONF_REQUEST_INTERVAL, DEFAULT_REQUEST_INTERVAL),
|
||||||
"queue": [],
|
"queue": [],
|
||||||
"processing": False,
|
"processing": False,
|
||||||
@@ -95,24 +99,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return
|
return
|
||||||
|
|
||||||
hass.data[DOMAIN][entry_id]["processing"] = True
|
hass.data[DOMAIN][entry_id]["processing"] = True
|
||||||
|
client = hass.data[DOMAIN][entry_id]["client"]
|
||||||
|
|
||||||
while hass.data[DOMAIN][entry_id]["queue"]:
|
while hass.data[DOMAIN][entry_id]["queue"]:
|
||||||
request = hass.data[DOMAIN][entry_id]["queue"].pop(0)
|
request = hass.data[DOMAIN][entry_id]["queue"].pop(0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
openai.api_key = hass.data[DOMAIN][entry_id][CONF_API_KEY]
|
response = await client.chat.completions.create(
|
||||||
openai.api_base = hass.data[DOMAIN][entry_id][CONF_API_BASE]
|
model=request["model"],
|
||||||
|
messages=[{"role": "user", "content": request["prompt"]}],
|
||||||
response = await hass.async_add_executor_job(
|
temperature=request["temperature"],
|
||||||
lambda: openai.ChatCompletion.create(
|
max_tokens=request["max_tokens"],
|
||||||
model=request["model"],
|
top_p=request["top_p"],
|
||||||
messages=[{"role": "user", "content": request["prompt"]}],
|
frequency_penalty=request["frequency_penalty"],
|
||||||
temperature=request["temperature"],
|
presence_penalty=request["presence_penalty"],
|
||||||
max_tokens=request["max_tokens"],
|
|
||||||
top_p=request["top_p"],
|
|
||||||
frequency_penalty=request["frequency_penalty"],
|
|
||||||
presence_penalty=request["presence_penalty"],
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
response_text = response.choices[0].message.content
|
response_text = response.choices[0].message.content
|
||||||
|
|||||||
Reference in New Issue
Block a user