Skip to content

Add AzureOpenAI completer with code sharing #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
.idea

/build
*.egg-info
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,9 @@ pandas_gpt.completer = pandas_gpt.LiteLLM('huggingface/meta-llama/Meta-Llama-3.1
pandas_gpt.completer = pandas_gpt.OpenRouter('anthropic/claude-3.5-sonnet')
```

### Anything
### Azure

```python
def my_custom_completer(prompt: str) -> str:
# Use an LLM or any other method to create a `process()` function that
# takes a pandas DataFrame as a single argument, does some operations on it,
# and return a DataFrame.
return 'def process(df): ...'

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),
If you want to use the [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
Expand All @@ -129,13 +119,27 @@ openai.api_version = '<Version>'
openai.api_key = '<API Key>'

import pandas_gpt
pandas_gpt.completer = pandas_gpt.OpenAI(
pandas_gpt.completer = pandas_gpt.AzureOpenAI(
model='gpt-3.5-turbo',
engine='<Engine>',
deployment_id='<Deployment ID>',
)
```

### Custom

It's also possible to use fully custom code generation:

```python
def my_custom_completer(prompt: str) -> str:
# Use an LLM or any other method to create a `process()` function that
# takes a pandas DataFrame as a single argument, does some operations on it,
# and return a DataFrame.
return 'def process(df): ...'

pandas_gpt.completer = my_custom_completer
```

## Alternatives

- [GitHub Copilot](https://github.com/features/copilot): General-purpose code completion (paid subscription)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "pandas-gpt"
description = "Power up your data science workflow with ChatGPT"
version = "1.0.0"
version = "1.0.1"
authors = [{ name = "Ryan Vandersmith" }]
readme = "README.md"
requires-python = ">= 3.10"
Expand Down
1 change: 1 addition & 0 deletions src/pandas_gpt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Completers
"completers",
"AzureOpenAI",
"LiteLLM",
"OpenAI",
"OpenRouter",
Expand Down
1 change: 1 addition & 0 deletions src/pandas_gpt/completers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .azureopenai import AzureOpenAI
from .litellm import LiteLLM
from .openai import OpenAI
from .openrouter import OpenRouter
5 changes: 5 additions & 0 deletions src/pandas_gpt/completers/azureopenai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pandas_gpt.completers.openai import OpenAI

class AzureOpenAI(OpenAI):
def _create_client(openai, **kw):
return openai.AzureOpenAI(**kw)
5 changes: 4 additions & 1 deletion src/pandas_gpt/completers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ def run_completion_function(self, **kw):
api_key = os.environ.get("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.OpenAI(**client_config)
self._client = self._create_client(openai, **client_config)
return self._client.chat.completions.create(**kw)

def _create_client(openai, **kw):
return openai.OpenAI(**kw)