From 02e75cbfb55570867166f360e658f5626020e09c Mon Sep 17 00:00:00 2001 From: Clyde Ancheta Date: Thu, 10 Apr 2025 12:52:17 +0800 Subject: [PATCH 1/5] split out azure openai --- src/pandas_gpt/__init__.py | 3 +- src/pandas_gpt/completers/azureopenai.py | 49 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/pandas_gpt/completers/azureopenai.py diff --git a/src/pandas_gpt/__init__.py b/src/pandas_gpt/__init__.py index 5e00e6b..b7f4638 100644 --- a/src/pandas_gpt/__init__.py +++ b/src/pandas_gpt/__init__.py @@ -2,7 +2,7 @@ from typing import Any, Callable import pandas as pd -from .completers import LiteLLM, OpenAI, OpenRouter +from .completers import AzureOpenAi, LiteLLM, OpenAI, OpenRouter __all__ = [ # Global config @@ -20,6 +20,7 @@ "LiteLLM", "OpenAI", "OpenRouter", + "AzureOpenAi", ] # Override with `pandas_gpt.verbose = True` diff --git a/src/pandas_gpt/completers/azureopenai.py b/src/pandas_gpt/completers/azureopenai.py new file mode 100644 index 0000000..83e2f7d --- /dev/null +++ b/src/pandas_gpt/completers/azureopenai.py @@ -0,0 +1,49 @@ +from dataclasses import dataclass +import os +from typing import Any + +__all__ = ["OpenAI"] + +default_system_prompt = "Write the function in a Python code block with all necessary imports and no example usage." + + +@dataclass +class AzureOpenAI: + completion_config: dict[str, Any] + client_config: dict[str, Any] + system_prompt: str + _cache: dict[str, str] + _client: Any + + def __init__(self, model: str, **client_config): + self.completion_config = {"model": model} + self.client_config = client_config + self.system_prompt = default_system_prompt + self._cache = {} + self._client = None + + def __call__(self, prompt: str) -> str: + completion = self._cache.get(prompt) or self.run_completion_function( + **self.completion_config, + messages=[ + dict(role="system", content=self.system_prompt), + dict(role="user", content=prompt), + ], + ) + self._cache[prompt] = completion + return completion.choices[0].message.content + + def run_completion_function(self, **kw): + if self._client is None: + try: + import openai + except ImportError: + raise Exception( + "The package `openai` could not be found. You can fix this error by running `pip install pandas-gpt[openai]` or passing a custom `completer` argument." + ) + client_config = dict(self.client_config) + api_key = os.environ.get("AZURE_OPENAI_API_KEY", openai.api_key) + if api_key is not None and "api_key" not in client_config: + client_config["api_key"] = api_key + self._client = openai.AzureOpenAI(**client_config) + return self._client.chat.completions.create(**kw) From 2d37926df516ddf9dd95f79586fc49066861a189 Mon Sep 17 00:00:00 2001 From: Clyde Ancheta Date: Thu, 10 Apr 2025 13:54:23 +0800 Subject: [PATCH 2/5] more adjustments --- src/pandas_gpt/completers/__init__.py | 1 + src/pandas_gpt/completers/azureopenai.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pandas_gpt/completers/__init__.py b/src/pandas_gpt/completers/__init__.py index 7e6bd6d..a7d7152 100644 --- a/src/pandas_gpt/completers/__init__.py +++ b/src/pandas_gpt/completers/__init__.py @@ -1,3 +1,4 @@ +from .azureopenai import AzureOpenAi from .litellm import LiteLLM from .openai import OpenAI from .openrouter import OpenRouter diff --git a/src/pandas_gpt/completers/azureopenai.py b/src/pandas_gpt/completers/azureopenai.py index 83e2f7d..7247ecb 100644 --- a/src/pandas_gpt/completers/azureopenai.py +++ b/src/pandas_gpt/completers/azureopenai.py @@ -2,7 +2,7 @@ import os from typing import Any -__all__ = ["OpenAI"] +__all__ = ["AzureOpenAI"] default_system_prompt = "Write the function in a Python code block with all necessary imports and no example usage." From 7eb5435e855c195e3fc1c1891c00effd135e0769 Mon Sep 17 00:00:00 2001 From: Clyde Ancheta Date: Thu, 10 Apr 2025 13:57:09 +0800 Subject: [PATCH 3/5] more adjustments --- src/pandas_gpt/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pandas_gpt/__init__.py b/src/pandas_gpt/__init__.py index b7f4638..465b1f3 100644 --- a/src/pandas_gpt/__init__.py +++ b/src/pandas_gpt/__init__.py @@ -2,7 +2,7 @@ from typing import Any, Callable import pandas as pd -from .completers import AzureOpenAi, LiteLLM, OpenAI, OpenRouter +from .completers import AzureOpenAI, LiteLLM, OpenAI, OpenRouter __all__ = [ # Global config @@ -20,7 +20,7 @@ "LiteLLM", "OpenAI", "OpenRouter", - "AzureOpenAi", + "AzureOpenAI", ] # Override with `pandas_gpt.verbose = True` From 6475bad14b8225600de810805479f78fc4a0faa0 Mon Sep 17 00:00:00 2001 From: Clyde Ancheta Date: Thu, 10 Apr 2025 14:00:04 +0800 Subject: [PATCH 4/5] more adjustments --- src/pandas_gpt/completers/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pandas_gpt/completers/__init__.py b/src/pandas_gpt/completers/__init__.py index a7d7152..52539d0 100644 --- a/src/pandas_gpt/completers/__init__.py +++ b/src/pandas_gpt/completers/__init__.py @@ -1,4 +1,4 @@ -from .azureopenai import AzureOpenAi +from .azureopenai import AzureOpenAI from .litellm import LiteLLM from .openai import OpenAI from .openrouter import OpenRouter From c5787209cf6bb83847a7c4cd1cdc568f51aa1f04 Mon Sep 17 00:00:00 2001 From: Clyde Ancheta Date: Fri, 11 Apr 2025 06:38:48 +0800 Subject: [PATCH 5/5] sorted out changes, updated docu --- README.md | 35 ++++++++++++------------ src/pandas_gpt/completers/azureopenai.py | 4 +-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 4b06010..ed29170 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,23 @@ pandas_gpt.completer = pandas_gpt.LiteLLM('huggingface/meta-llama/Meta-Llama-3.1 pandas_gpt.completer = pandas_gpt.OpenRouter('anthropic/claude-3.5-sonnet') ``` +### Azure OpenAI + +Get your settings from [Azure AI Foundry](https://oai.azure.com/) or your system administrator. + +```python +import openai +import pandas_gpt + +openai.api_key = '' + +pandas_gpt.completer = pandas_gpt.AzureOpenAI( + deployment=, + azure_endpoint=, + api_version=, +) +``` + ### Anything ```python @@ -118,24 +135,6 @@ def my_custom_completer(prompt: str) -> str: pandas_gpt.completer = my_custom_completer ``` -If you want to use a fully customized API host such as [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/cognitive-services/openai-service), -you can globally configure the `openai` and `pandas-gpt` packages: - -```python -import openai -openai.api_type = 'azure' -openai.api_base = '' -openai.api_version = '' -openai.api_key = '' - -import pandas_gpt -pandas_gpt.completer = pandas_gpt.OpenAI( - model='gpt-3.5-turbo', - engine='', - deployment_id='', -) -``` - ## Alternatives - [GitHub Copilot](https://github.com/features/copilot): General-purpose code completion (paid subscription) diff --git a/src/pandas_gpt/completers/azureopenai.py b/src/pandas_gpt/completers/azureopenai.py index 7247ecb..82dcb20 100644 --- a/src/pandas_gpt/completers/azureopenai.py +++ b/src/pandas_gpt/completers/azureopenai.py @@ -15,8 +15,8 @@ class AzureOpenAI: _cache: dict[str, str] _client: Any - def __init__(self, model: str, **client_config): - self.completion_config = {"model": model} + def __init__(self, deployment: str, **client_config): + self.completion_config = {"model": deployment} self.client_config = client_config self.system_prompt = default_system_prompt self._cache = {}