Skip to content

Commit 0f676c4

Browse files
authored
Merge pull request #68 from somethingwentwell/add-azure-openai
Add azure openai
2 parents 5b8ce1b + c52a030 commit 0f676c4

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ OPENAI_API_KEY=
44
# CODEBOX_API_KEY=
55
# (set True to enable logging)
66
VERBOSE=False
7+
# (optional, required for Azure OpenAI)
8+
# OPENAI_API_TYPE=azure
9+
# OPENAI_API_VERSION=2023-07-01-preview
10+
# OPENAI_API_BASE=
11+
# DEPLOYMENT_NAME=

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,23 @@ For deployments, you can use `pip install codeinterpreterapi` instead which does
2727

2828
## Usage
2929

30-
Make sure to set the `OPENAI_API_KEY` environment variable (or use a `.env` file)
30+
To configure OpenAI and Azure OpenAI, ensure that you set the appropriate environment variables (or use a .env file):
31+
32+
For OpenAI, set the OPENAI_API_KEY environment variable:
33+
```
34+
export OPENAI_API_KEY=your_openai_api_key
35+
```
36+
37+
For Azure OpenAI, set the following environment variables:
38+
```
39+
export OPENAI_API_TYPE=azure
40+
export OPENAI_API_VERSION=your_api_version
41+
export OPENAI_API_BASE=your_api_base
42+
export OPENAI_API_KEY=your_azure_openai_api_key
43+
export DEPLOYMENT_NAME=your_deployment_name
44+
```
45+
46+
Remember to replace the placeholders with your actual API keys and other required information.
3147

3248
```python
3349
from codeinterpreterapi import CodeInterpreterSession

codeinterpreterapi/session.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ConversationalAgent,
1515
ConversationalChatAgent,
1616
)
17-
from langchain.chat_models import ChatAnthropic, ChatOpenAI
17+
from langchain.chat_models import AzureChatOpenAI, ChatAnthropic, ChatOpenAI
1818
from langchain.chat_models.base import BaseChatModel
1919
from langchain.memory import ConversationBufferMemory
2020
from langchain.prompts.chat import MessagesPlaceholder
@@ -87,13 +87,33 @@ def _choose_llm(
8787
"OpenAI API key missing. Set OPENAI_API_KEY env variable "
8888
"or pass `openai_api_key` to session."
8989
)
90-
return ChatOpenAI(
91-
temperature=0.03,
92-
model=model,
93-
openai_api_key=openai_api_key,
94-
max_retries=3,
95-
request_timeout=60 * 3,
96-
) # type: ignore
90+
openai_api_version = getenv("OPENAI_API_VERSION")
91+
openai_api_base = getenv("OPENAI_API_BASE")
92+
deployment_name = getenv("DEPLOYMENT_NAME")
93+
openapi_type = getenv("OPENAI_API_TYPE")
94+
if (
95+
openapi_type == "azure"
96+
and openai_api_version
97+
and openai_api_base
98+
and deployment_name
99+
):
100+
return AzureChatOpenAI(
101+
temperature=0.03,
102+
openai_api_base=openai_api_base,
103+
openai_api_version=openai_api_version,
104+
deployment_name=deployment_name,
105+
openai_api_key=openai_api_key,
106+
max_retries=3,
107+
request_timeout=60 * 3,
108+
)
109+
else:
110+
return ChatOpenAI(
111+
temperature=0.03,
112+
model=model,
113+
openai_api_key=openai_api_key,
114+
max_retries=3,
115+
request_timeout=60 * 3,
116+
)
97117
elif "claude" in model:
98118
return ChatAnthropic(model=model)
99119
else:

0 commit comments

Comments
 (0)