Skip to content
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

Add support for Azure OpenAI #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ ai("Thanks for your help!", tools=[search, lookup])
'tool': None}
```

## Using Azure OpenAI Service
To use Azure OpenAI with `simpleaichat`, set the `SIMPLEAICHAT_API_URL` environment variable to your specific Azure OpenAI API URL.
Do not forget to use your Azure OpenAI API key as the `api_key` parameter when instantiating `AIChat`.

```bash
export SIMPLEAICHAT_API_URL=https://custom.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2023-07-01-preview
```

All other settings align with standard OpenAI examples. For further details, consult the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line&pivots=rest-api).

## Miscellaneous Notes

- Like [gpt-2-simple](https://github.com/minimaxir/gpt-2-simple) before it, the primary motivation behind releasing simpleaichat is to both democratize access to ChatGPT even more and also offer more transparency for non-engineers into how Chat AI-based apps work under the hood given the disproportionate amount of media misinformation about their capabilities. This is inspired by real-world experience from [my work with BuzzFeed](https://tech.buzzfeed.com/the-right-tools-for-the-job-c05de96e949e) in the domain, where after spending a long time working with the popular [LangChain](https://github.com/hwchase17/langchain), a more-simple implementation was both much easier to maintain and resulted in much better generations. I began focusing development on simpleaichat after reading a [Hacker News thread](https://news.ycombinator.com/item?id=35820931) filled with many similar complaints, indicating value for an easier-to-use interface for modern AI tricks.
Expand Down
13 changes: 11 additions & 2 deletions simpleaichat/chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from pydantic import HttpUrl
from httpx import Client, AsyncClient
from typing import List, Dict, Union, Set, Any
Expand All @@ -12,9 +14,10 @@

{tools}"""

OPENAI_API_URL = "https://api.openai.com/v1/chat/completions"

class ChatGPTSession(ChatSession):
api_url: HttpUrl = "https://api.openai.com/v1/chat/completions"
api_url: HttpUrl = os.getenv("SIMPLEAICHAT_API_URL", OPENAI_API_URL)
input_fields: Set[str] = {"role", "content", "name"}
system: str = "You are a helpful assistant."
params: Dict[str, Any] = {"temperature": 0.7}
Expand All @@ -31,8 +34,14 @@ def prepare_request(
):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.auth['api_key'].get_secret_value()}",
}
api_key = self.auth["api_key"].get_secret_value()

# OpenAI and Azure use different auth headers
if self.api_url == OPENAI_API_URL:
headers["Authorization"] = f"Bearer {api_key}"
else:
headers["api-key"] = api_key

system_message = ChatMessage(role="system", content=system or self.system)
if not input_schema:
Expand Down