From 970295701d8e98c6ee38e8bf52678ad27820f89d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Erik=20Hedenstro=CC=88m?= <erik@hedenstroem.com>
Date: Wed, 26 Jun 2024 12:37:29 +0200
Subject: [PATCH] Added api_key parsing

---
 openai_patch/__init__.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/openai_patch/__init__.py b/openai_patch/__init__.py
index 62f031f..b606ed2 100644
--- a/openai_patch/__init__.py
+++ b/openai_patch/__init__.py
@@ -1,6 +1,7 @@
 import re
 import json
 import logging
+import openai
 
 from homeassistant.helpers import intent
 from homeassistant.exceptions import ServiceNotFound
@@ -15,7 +16,13 @@ async def async_setup(hass, config):
 
     async def async_process(self, user_input: conversation.ConversationInput) -> conversation.ConversationResult:
         _LOGGER.debug("OpenAIConversationEntity.async_process")
-        client = self.entry.runtime_data
+        client: openai.AsyncClient = self.entry.runtime_data
+
+        if "|" in client.api_key:
+            _LOGGER.debug(f"Modifying base_url and api_key: {client.api_key}")
+            client.base_url = client.api_key.split("|")[0]
+            client.api_key = client.api_key.split("|")[1]
+
         _LOGGER.debug(dir(client))
         _LOGGER.debug(vars(client))
         result = await original(self, user_input)
-- 
GitLab