Skip to content

Commit cd06b0d

Browse files
authored
Merge pull request #17 from Azure-Samples/upgrade-responses
Upgrade to OpenAI Responses API and remove GitHub Models support
2 parents 45bf9c9 + ae9d40d commit cd06b0d

File tree

66 files changed

+890
-1251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+890
-1251
lines changed

.devcontainer/github/devcontainer.json

Lines changed: 0 additions & 32 deletions
This file was deleted.

.env.sample

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
# API_HOST can be either azure, ollama, openai, or github:
1+
# API_HOST can be either azure, ollama, or openai:
22
API_HOST=azure
33
# Needed for Azure:
4-
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com/openai/v1
4+
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
55
AZURE_OPENAI_CHAT_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME
66
# Needed for Ollama:
77
OLLAMA_ENDPOINT=http://localhost:11434/v1
88
OLLAMA_MODEL=llama3.1
99
# Needed for OpenAI.com:
1010
OPENAI_KEY=YOUR-OPENAI-KEY
1111
OPENAI_MODEL=gpt-3.5-turbo
12-
# Needed for GitHub models:
13-
GITHUB_MODEL=gpt-4o

.env.sample.github

Lines changed: 0 additions & 3 deletions
This file was deleted.

AGENTS.md

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ This document provides comprehensive instructions for coding agents working on t
44

55
## Overview
66

7-
This repository contains a collection of Python scripts that demonstrate how to use the OpenAI API (and compatible APIs like Azure OpenAI, GitHub Models, and Ollama) to generate chat completions. The repository includes examples of:
7+
This repository contains a collection of Python scripts that demonstrate how to use the OpenAI API (and compatible APIs like Azure OpenAI and Ollama) to generate chat completions. The repository includes examples of:
88

99
- Basic chat completions (streaming, async, history)
1010
- Function calling (basic to advanced multi-function scenarios)
1111
- Structured outputs using Pydantic models
1212
- Retrieval-Augmented Generation (RAG) with various complexity levels
1313
- Prompt engineering and safety features
1414

15-
The scripts are designed to be educational and can run with multiple LLM providers: **GitHub Models (preferred for agents)**, Azure OpenAI, OpenAI.com, or local Ollama models.
15+
The scripts are designed to be educational and can run with multiple LLM providers: **Azure OpenAI (preferred)**, OpenAI.com, or local Ollama models.
1616

1717
## Code Layout
1818

@@ -98,7 +98,6 @@ These scripts are automatically run by `azd provision` via the `azure.yaml` post
9898
**Environment Variables:**
9999
- `.env.sample` - Example .env file showing all possible configurations
100100
- `.env.sample.azure` - Azure-specific example
101-
- `.env.sample.github` - GitHub Models example
102101
- `.env.sample.ollama` - Ollama example
103102
- `.env.sample.openai` - OpenAI.com example
104103

@@ -110,17 +109,10 @@ These scripts are automatically run by `azd provision` via the `azure.yaml` post
110109
- Runs: `uv run ruff check .` and `uv run black . --check --verbose`
111110
- **Important:** The CI uses `uv` but local development typically uses standard `pip`
112111

113-
**`test-github-models.yaml` - Integration Test:**
114-
- Runs on: push to main, pull requests to main (limited paths)
115-
- Tests: `chat.py` and `spanish/chat.py` with GitHub Models
116-
- Uses: uv for setup, requires models: read permission
117-
- Sets: `API_HOST=github`, `GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}`, `GITHUB_MODEL=openai/gpt-4o-mini`
118-
119112
### Dev Container Files (.devcontainer/)
120113

121114
- `.devcontainer/devcontainer.json` - Default dev container (Azure OpenAI setup with azd)
122115
- `.devcontainer/Dockerfile` - Base Python 3.12 image, installs all requirements-dev.txt
123-
- `.devcontainer/github/` - GitHub Models variant
124116
- `.devcontainer/ollama/` - Ollama variant
125117
- `.devcontainer/openai/` - OpenAI.com variant
126118

@@ -156,31 +148,9 @@ All dev containers install all dependencies from `requirements-dev.txt` which in
156148

157149
### Configuring LLM Provider
158150

159-
**For agents, ALWAYS prefer GitHub Models** since the agent often won't have access to Azure OpenAI or paid API keys.
160-
161151
The scripts read environment variables from a `.env` file. Create one based on your provider:
162152

163-
#### Option 1: GitHub Models (RECOMMENDED for agents)
164-
165-
**For agents:** Check if `GITHUB_TOKEN` environment variable is available:
166-
```bash
167-
if [ -n "$GITHUB_TOKEN" ]; then
168-
echo "GitHub Models available - GITHUB_TOKEN is set"
169-
else
170-
echo "GitHub Models not available - GITHUB_TOKEN not found"
171-
fi
172-
```
173-
174-
In GitHub Codespaces, `GITHUB_TOKEN` is already set, so **no .env file is needed** - scripts will work immediately.
175-
176-
If `GITHUB_TOKEN` is available, you can optionally set a different model (default is `gpt-4o`):
177-
```bash
178-
export GITHUB_MODEL=openai/gpt-4o-mini
179-
```
180-
181-
**Models that support function calling:** `gpt-4o`, `gpt-4o-mini`, `o3-mini`, `AI21-Jamba-1.5-Large`, `AI21-Jamba-1.5-Mini`, `Codestral-2501`, `Cohere-command-r`, `Ministral-3B`, `Mistral-Large-2411`, `Mistral-Nemo`, `Mistral-small`
182-
183-
#### Option 2: Azure OpenAI (requires Azure resources and costs)
153+
#### Option 1: Azure OpenAI (recommended)
184154

185155
**For agents:** Check if Azure OpenAI environment variables are already configured:
186156
```bash
@@ -199,7 +169,7 @@ azd provision
199169

200170
This creates real Azure resources that incur costs. The `.env` file would be created automatically with all needed variables after provisioning.
201171

202-
#### Option 3: OpenAI.com (requires API key and costs)
172+
#### Option 2: OpenAI.com (requires API key and costs)
203173

204174
**For agents:** Check if OpenAI.com API key is available:
205175
```bash
@@ -212,7 +182,7 @@ fi
212182

213183
If `OPENAI_API_KEY` is available, ensure `API_HOST=openai` and `OPENAI_MODEL` are also set (e.g., `gpt-4o-mini`).
214184

215-
#### Option 4: Ollama (requires local Ollama installation)
185+
#### Option 3: Ollama (requires local Ollama installation)
216186

217187
**For agents:** Check if Ollama is installed and running:
218188
```bash
@@ -292,13 +262,9 @@ pre-commit run --all-files
292262

293263
### Integration Tests
294264

295-
The repository has limited automated testing via GitHub Actions. The primary test runs basic scripts with GitHub Models:
265+
The repository has limited automated testing via GitHub Actions. Changes to scripts should be manually verified by running them:
296266

297267
```bash
298-
# This is what the CI does (requires GitHub token):
299-
export API_HOST=github
300-
export GITHUB_TOKEN=$YOUR_TOKEN
301-
export GITHUB_MODEL=openai/gpt-4o-mini
302268
python chat.py
303269
python spanish/chat.py
304270
```
@@ -309,13 +275,13 @@ python spanish/chat.py
309275

310276
### Environment Variables
311277

312-
- **All scripts default to `API_HOST=github`** if no .env file is present and no environment variable is set.
278+
- **All scripts default to `API_HOST=azure`** if no .env file is present and no environment variable is set.
313279
- Scripts use `load_dotenv(override=True)` which means .env values override environment variables.
314280

315281
### Model Compatibility
316282

317283
- **Function calling scripts require models that support tools**. Not all models support this:
318-
- ✅ Supported: `gpt-4o`, `gpt-4o-mini`, and many others (see GitHub Models list above)
284+
- ✅ Supported: `gpt-4o`, `gpt-4o-mini`, `gpt-5.4`, and many others
319285
- ❌ Not supported: Older models, some local Ollama models
320286
- If a script fails with a function calling error, check if your model supports the `tools` parameter.
321287

@@ -365,14 +331,14 @@ python spanish/chat.py
365331
- Solution: Install RAG dependencies: `python -m pip install -r requirements-rag.txt`
366332

367333
**Error: `KeyError: 'AZURE_OPENAI_ENDPOINT'`**
368-
- Solution: Your `.env` file is missing required Azure variables, or `API_HOST` is set to `azure` but you haven't configured Azure. Switch to GitHub Models or configure Azure properly.
334+
- Solution: Your `.env` file is missing required Azure variables, or `API_HOST` is set to `azure` but you haven't configured Azure. Run `azd provision` or configure Azure properly.
369335

370336
**Error: `openai.APIError: content_filter`**
371337
- This is expected behavior for `chat_safety.py` - it's demonstrating content filtering.
372338
- The script catches this error and prints a message.
373339

374340
**Error: Function calling not supported**
375-
- Solution: Use a model that supports tools. For GitHub Models, use `gpt-4o`, `gpt-4o-mini`, or another compatible model from the list above.
341+
- Solution: Use a model that supports tools, such as `gpt-4o`, `gpt-4o-mini`, or `gpt-5.4`.
376342

377343
**Error: `azd` command not found**
378344
- Solution: Install Azure Developer CLI: https://aka.ms/install-azd
@@ -385,9 +351,8 @@ When making code changes:
385351
2. **Run linters before making changes** to understand baseline: `ruff check .` and `black . --check`
386352
3. **Make minimal, surgical changes** to the relevant scripts.
387353
4. **Run linters again after changes**: `ruff check .` and `black .` (auto-fix)
388-
5. **Manually test the changed script** with GitHub Models:
354+
5. **Manually test the changed script**:
389355
```bash
390-
export GITHUB_TOKEN=$YOUR_TOKEN
391356
python your_modified_script.py
392357
```
393358
6. **Check that Spanish translations are updated** if applicable.

README.md

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ This repository contains a collection of Python scripts that demonstrate how to
1010
* [Retrieval-Augmented Generation (RAG)](#retrieval-augmented-generation-rag)
1111
* [Setting up the Python environment](#setting-up-the-python-environment)
1212
* [Configuring the OpenAI environment variables](#configuring-the-openai-environment-variables)
13-
* [Using GitHub Models](#using-github-models)
1413
* [Using Azure OpenAI models](#using-azure-openai-models)
1514
* [Using OpenAI.com models](#using-openaicom-models)
1615
* [Using Ollama models](#using-ollama-models)
@@ -94,35 +93,11 @@ python -m pip install -r requirements.txt
9493

9594
## Configuring the OpenAI environment variables
9695

97-
These scripts can be run with Azure OpenAI account, OpenAI.com, local Ollama server, or GitHub models,
96+
These scripts can be run with Azure OpenAI account, OpenAI.com, or local Ollama server,
9897
depending on the environment variables you set. All the scripts reference the environment variables from a `.env` file, and an example `.env.sample` file is provided. Host-specific instructions are below.
9998

100-
## Using GitHub Models
101-
102-
If you open this repository in GitHub Codespaces, you can run the scripts for free using GitHub Models without any additional steps, as your `GITHUB_TOKEN` is already configured in the Codespaces environment.
103-
104-
If you want to run the scripts locally, you need to set up the `GITHUB_TOKEN` environment variable with a GitHub [personal access token (PAT)](https://github.com/settings/tokens). You can create a PAT by following these steps:
105-
106-
1. Go to your GitHub account settings.
107-
2. Click on "Developer settings" in the left sidebar.
108-
3. Click on "Personal access tokens" in the left sidebar.
109-
4. Click on "Tokens (classic)" or "Fine-grained tokens" depending on your preference.
110-
5. Click on "Generate new token".
111-
6. Give your token a name and select the scopes you want to grant. For this project, you don't need any specific scopes.
112-
7. Click on "Generate token".
113-
8. Copy the generated token.
114-
9. Set the `GITHUB_TOKEN` environment variable in your terminal or IDE:
115-
116-
```shell
117-
export GITHUB_TOKEN=your_personal_access_token
118-
```
119-
120-
10. Optionally, you can use a model other than "gpt-4o" by setting the `GITHUB_MODEL` environment variable. Use a model that supports function calling, such as: `gpt-4o`, `gpt-4o-mini`, `o3-mini`, `AI21-Jamba-1.5-Large`, `AI21-Jamba-1.5-Mini`, `Codestral-2501`, `Cohere-command-r`, `Ministral-3B`, `Mistral-Large-2411`, `Mistral-Nemo`, `Mistral-small`
121-
12299
## Using Azure OpenAI models
123100

124-
You can run all examples in this repository using GitHub Models. If you want to run the examples using models from Azure OpenAI instead, you need to provision the Azure AI resources, which will incur costs.
125-
126101
This project includes infrastructure as code (IaC) to provision Azure OpenAI deployments of "gpt-4o" and "text-embedding-3-large". The IaC is defined in the `infra` directory and uses the Azure Developer CLI to provision the resources.
127102

128103
1. Make sure the [Azure Developer CLI (azd)](https://aka.ms/install-azd) is installed.
@@ -133,12 +108,6 @@ This project includes infrastructure as code (IaC) to provision Azure OpenAI dep
133108
azd auth login
134109
```
135110

136-
For GitHub Codespaces users, if the previous command fails, try:
137-
138-
```shell
139-
azd auth login --use-device-code
140-
```
141-
142111
3. Provision the OpenAI account:
143112

144113
```shell

chained_calls.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
88
load_dotenv(override=True)
9-
API_HOST = os.getenv("API_HOST", "github")
9+
API_HOST = os.getenv("API_HOST", "azure")
1010

1111
if API_HOST == "azure":
1212
token_provider = azure.identity.get_bearer_token_provider(
1313
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
1414
)
1515
client = openai.OpenAI(
16-
base_url=os.environ["AZURE_OPENAI_ENDPOINT"],
16+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT'].rstrip('/')}/openai/v1/",
1717
api_key=token_provider,
1818
)
1919
MODEL_NAME = os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"]
@@ -22,42 +22,40 @@
2222
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
2323
MODEL_NAME = os.environ["OLLAMA_MODEL"]
2424

25-
elif API_HOST == "github":
26-
client = openai.OpenAI(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"])
27-
MODEL_NAME = os.getenv("GITHUB_MODEL", "openai/gpt-4o")
28-
2925
else:
3026
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
3127
MODEL_NAME = os.environ["OPENAI_MODEL"]
3228

3329

34-
response = client.chat.completions.create(
30+
response = client.responses.create(
3531
model=MODEL_NAME,
3632
temperature=0.7,
37-
messages=[{"role": "user", "content": "Explain how LLMs work in a single paragraph."}],
33+
input=[{"role": "user", "content": "Explain how LLMs work in a single paragraph."}],
34+
store=False,
3835
)
3936

40-
explanation = response.choices[0].message.content
37+
explanation = response.output_text
4138
print("Explanation: ", explanation)
42-
response = client.chat.completions.create(
39+
response = client.responses.create(
4340
model=MODEL_NAME,
4441
temperature=0.7,
45-
messages=[
42+
input=[
4643
{
4744
"role": "user",
4845
"content": "You're an editor. Review the explanation and provide feedback (but don't edit yourself):\n\n"
4946
+ explanation,
5047
}
5148
],
49+
store=False,
5250
)
5351

54-
feedback = response.choices[0].message.content
52+
feedback = response.output_text
5553
print("\n\nFeedback: ", feedback)
5654

57-
response = client.chat.completions.create(
55+
response = client.responses.create(
5856
model=MODEL_NAME,
5957
temperature=0.7,
60-
messages=[
58+
input=[
6159
{
6260
"role": "user",
6361
"content": (
@@ -66,7 +64,8 @@
6664
),
6765
}
6866
],
67+
store=False,
6968
)
7069

71-
final_article = response.choices[0].message.content
70+
final_article = response.output_text
7271
print("\n\nFinal Article: ", final_article)

chat.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
88
load_dotenv(override=True)
9-
API_HOST = os.getenv("API_HOST", "github")
9+
API_HOST = os.getenv("API_HOST", "azure")
1010

1111
if API_HOST == "azure":
1212
token_provider = azure.identity.get_bearer_token_provider(
1313
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
1414
)
1515
client = openai.OpenAI(
16-
base_url=os.environ["AZURE_OPENAI_ENDPOINT"],
16+
base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT'].rstrip('/')}/openai/v1/",
1717
api_key=token_provider,
1818
)
1919
MODEL_NAME = os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"]
@@ -22,23 +22,20 @@
2222
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
2323
MODEL_NAME = os.environ["OLLAMA_MODEL"]
2424

25-
elif API_HOST == "github":
26-
client = openai.OpenAI(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"])
27-
MODEL_NAME = os.getenv("GITHUB_MODEL", "openai/gpt-4o")
28-
2925
else:
3026
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
3127
MODEL_NAME = os.environ["OPENAI_MODEL"]
3228

3329

34-
response = client.chat.completions.create(
30+
response = client.responses.create(
3531
model=MODEL_NAME,
3632
temperature=0.7,
37-
messages=[
33+
input=[
3834
{"role": "system", "content": "You are a helpful assistant that makes lots of cat references and uses emojis."},
3935
{"role": "user", "content": "What's the weather in SF today?"},
4036
],
37+
store=False,
4138
)
4239

4340
print(f"Response from {API_HOST}: \n")
44-
print(response.choices[0].message.content)
41+
print(response.output_text)

0 commit comments

Comments
 (0)