From a44696d8a126198af7e88c72739ce12b78c82d0b Mon Sep 17 00:00:00 2001 From: Basanth Kalanoor Date: Wed, 31 Jul 2024 18:06:21 +0530 Subject: [PATCH 1/4] Update openai.py AzureChatOpenAI class is inheriting the init method from ChatOpenAI without properly overriding it. --- textgrad/engine/openai.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/textgrad/engine/openai.py b/textgrad/engine/openai.py index 723f04a..7699da4 100644 --- a/textgrad/engine/openai.py +++ b/textgrad/engine/openai.py @@ -59,6 +59,10 @@ def __init__( base_url=base_url, api_key="ollama" ) + elif base_url and "azure" in base_url.lower(): + # Skip client initialization for Azure + # Azure-specific initialization will be handled in AzureChatOpenAI + pass else: raise ValueError("Invalid base URL provided. Please use the default OLLAMA base URL or None.") @@ -181,12 +185,20 @@ def __init__( Raises: ValueError: If the AZURE_OPENAI_API_KEY environment variable is not set. """ + + root = platformdirs.user_cache_dir("textgrad") cache_path = os.path.join(root, f"cache_azure_{model_string}.db") # Changed cache path to differentiate from OpenAI cache - super().__init__(cache_path=cache_path, system_prompt=system_prompt, **kwargs) + azure_endpoint = os.getenv("AZURE_OPENAI_API_BASE") + if azure_endpoint is None: + raise ValueError("Please set the AZURE_OPENAI_API_BASE environment variable if you'd like to use Azure OpenAI models.") + + super().__init__(model_string=model_string, system_prompt=system_prompt, base_url=azure_endpoint, **kwargs) + + + - self.system_prompt = system_prompt api_version = os.getenv("AZURE_OPENAI_API_VERSION", "2023-07-01-preview") if os.getenv("AZURE_OPENAI_API_KEY") is None: raise ValueError("Please set the AZURE_OPENAI_API_KEY, AZURE_OPENAI_API_BASE, and AZURE_OPENAI_API_VERSION environment variables if you'd like to use Azure OpenAI models.") From 07d51ec2198809eac7768dadd125abfd81548ec3 Mon Sep 17 00:00:00 2001 From: Basanth Kalanoor Date: Thu, 19 Sep 2024 14:46:55 +0530 Subject: [PATCH 2/4] Update __init__.py gpt is now generalized --- textgrad/engine/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textgrad/engine/__init__.py b/textgrad/engine/__init__.py index 3e0ee24..be41f02 100644 --- a/textgrad/engine/__init__.py +++ b/textgrad/engine/__init__.py @@ -31,7 +31,7 @@ def get_engine(engine_name: str, **kwargs) -> EngineLM: if engine_name in __ENGINE_NAME_SHORTCUTS__: engine_name = __ENGINE_NAME_SHORTCUTS__[engine_name] - if "seed" in kwargs and "gpt-4" not in engine_name and "gpt-3.5" not in engine_name and "gpt-35" not in engine_name: + if "seed" in kwargs and "gpt" not in engine_name and "gpt-3.5" not in engine_name and "gpt-35" not in engine_name: raise ValueError(f"Seed is currently supported only for OpenAI engines, not {engine_name}") if engine_name.startswith("azure"): From cf33f2f88f67038a076c2a63f26c34c7b204057f Mon Sep 17 00:00:00 2001 From: Basanth Kalanoor Date: Thu, 19 Sep 2024 14:54:27 +0530 Subject: [PATCH 3/4] azure gpt support irrespective of case --- .vscode/settings.json | 5 +++++ textgrad/engine/__init__.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a490b61 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] +} \ No newline at end of file diff --git a/textgrad/engine/__init__.py b/textgrad/engine/__init__.py index be41f02..e525b93 100644 --- a/textgrad/engine/__init__.py +++ b/textgrad/engine/__init__.py @@ -31,7 +31,7 @@ def get_engine(engine_name: str, **kwargs) -> EngineLM: if engine_name in __ENGINE_NAME_SHORTCUTS__: engine_name = __ENGINE_NAME_SHORTCUTS__[engine_name] - if "seed" in kwargs and "gpt" not in engine_name and "gpt-3.5" not in engine_name and "gpt-35" not in engine_name: + if "seed" in kwargs and "gpt" not in engine_name.lower() and "gpt-3.5" not in engine_name and "gpt-35" not in engine_name: raise ValueError(f"Seed is currently supported only for OpenAI engines, not {engine_name}") if engine_name.startswith("azure"): From 26728f38ce76837009b3ded421553f3c7f14f54c Mon Sep 17 00:00:00 2001 From: Basanth Kalanoor Date: Thu, 19 Sep 2024 15:04:30 +0530 Subject: [PATCH 4/4] open ai env variable is changed form base to endpoint --- textgrad/engine/openai.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/textgrad/engine/openai.py b/textgrad/engine/openai.py index 7699da4..e6a863d 100644 --- a/textgrad/engine/openai.py +++ b/textgrad/engine/openai.py @@ -190,9 +190,9 @@ def __init__( root = platformdirs.user_cache_dir("textgrad") cache_path = os.path.join(root, f"cache_azure_{model_string}.db") # Changed cache path to differentiate from OpenAI cache - azure_endpoint = os.getenv("AZURE_OPENAI_API_BASE") + azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") if azure_endpoint is None: - raise ValueError("Please set the AZURE_OPENAI_API_BASE environment variable if you'd like to use Azure OpenAI models.") + raise ValueError("Please set the AZURE_OPENAI_ENDPOINT environment variable if you'd like to use Azure OpenAI models.") super().__init__(model_string=model_string, system_prompt=system_prompt, base_url=azure_endpoint, **kwargs) @@ -201,12 +201,12 @@ def __init__( api_version = os.getenv("AZURE_OPENAI_API_VERSION", "2023-07-01-preview") if os.getenv("AZURE_OPENAI_API_KEY") is None: - raise ValueError("Please set the AZURE_OPENAI_API_KEY, AZURE_OPENAI_API_BASE, and AZURE_OPENAI_API_VERSION environment variables if you'd like to use Azure OpenAI models.") + raise ValueError("Please set the AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and AZURE_OPENAI_API_VERSION environment variables if you'd like to use Azure OpenAI models.") self.client = AzureOpenAI( api_version=api_version, api_key=os.getenv("AZURE_OPENAI_API_KEY"), - azure_endpoint=os.getenv("AZURE_OPENAI_API_BASE"), + azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), azure_deployment=model_string, ) self.model_string = model_string