From 1901b1d1cf6932a8da154bf57bbff155df321f8a Mon Sep 17 00:00:00 2001
From: tolgadevAI <164843802+tolgadevAI@users.noreply.github.com>
Date: Wed, 17 Jul 2024 09:11:15 +0300
Subject: [PATCH] fix the missing field issue & lint

---
 semantic_router/layer.py       |  2 +-
 semantic_router/llms/openai.py | 31 ++++++++++++-------------------
 2 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/semantic_router/layer.py b/semantic_router/layer.py
index 072b697f..b5c9475a 100644
--- a/semantic_router/layer.py
+++ b/semantic_router/layer.py
@@ -301,7 +301,7 @@ class RouteLayer:
                     logger.warning(
                         "No LLM provided for dynamic route, will use OpenAI LLM default"
                     )
-                    self.llm = OpenAILLM(use_async=True)
+                    self.llm = OpenAILLM()
                     route.llm = self.llm
                 else:
                     route.llm = self.llm
diff --git a/semantic_router/llms/openai.py b/semantic_router/llms/openai.py
index 20360655..f7818201 100644
--- a/semantic_router/llms/openai.py
+++ b/semantic_router/llms/openai.py
@@ -21,7 +21,8 @@ from openai.types.chat.chat_completion_message_tool_call import (
 
 
 class OpenAILLM(BaseLLM):
-    client: Union[openai.AsyncOpenAI, openai.OpenAI]
+    client: Optional[openai.OpenAI]
+    async_client: Optional[openai.AsyncOpenAI]
     temperature: Optional[float]
     max_tokens: Optional[int]
 
@@ -39,21 +40,13 @@ class OpenAILLM(BaseLLM):
         api_key = openai_api_key or os.getenv("OPENAI_API_KEY")
         if api_key is None:
             raise ValueError("OpenAI API key cannot be 'None'.")
-
-        if use_async:
-            try:
-                self.client = openai.AsyncOpenAI(api_key=api_key)
-            except Exception as e:
-                raise ValueError(
-                    f"AsyncOpenAI API client failed to initialize. Error: {e}"
-                ) from e
-        else:
-            try:
-                self.client = openai.OpenAI(api_key=api_key)
-            except Exception as e:
-                raise ValueError(
-                    f"OpenAI API client failed to initialize. Error: {e}"
-                ) from e
+        try:
+            self.async_client = openai.AsyncOpenAI(api_key=api_key)
+            self.client = openai.OpenAI(api_key=api_key)
+        except Exception as e:
+            raise ValueError(
+                f"OpenAI API client failed to initialize. Error: {e}"
+            ) from e
         self.temperature = temperature
         self.max_tokens = max_tokens
 
@@ -123,14 +116,14 @@ class OpenAILLM(BaseLLM):
         messages: List[Message],
         function_schemas: Optional[List[Dict[str, Any]]] = None,
     ) -> str:
-        if self.client is None:
-            raise ValueError("OpenAI client is not initialized.")
+        if self.async_client is None:
+            raise ValueError("OpenAI async_client is not initialized.")
         try:
             tools: Union[List[Dict[str, Any]], NotGiven] = (
                 function_schemas if function_schemas is not None else NOT_GIVEN
             )
 
-            completion = await self.client.chat.completions.create(
+            completion = await self.async_client.chat.completions.create(  # type: ignore
                 model=self.name,
                 messages=[m.to_openai() for m in messages],
                 temperature=self.temperature,
-- 
GitLab