This is a sample project demonstrating how to use Azure OpenAI API to generate structured outputs that conform to a specific pydantic
model. The project includes a Python script that connects to Azure OpenAI, sends a prompt, and then parses the response into a structured format using pydantic
.
- Integration with Azure OpenAI API
- Use of
pydantic
models to structure API output - Simple API response handling
Before you can run this project, you will need:
- Python 3.8 or later
- An Azure OpenAI resource and API key
setuptools
andwheel
for packaging and dependency management
-
Clone this repository:
https://github.com/wattai/pg-azure-openai-structured-output.git cd pg-azure-openai-structured-output
-
Set up your environment:
Make sure you have a Python virtual environment set up. If you don’t have one, create it as follows:
pyenv install 3.12.1 pyenv virtualenv 3.12.1 pg-azure-openai pyenv shell pg-azure-openai
-
Install dependencies:
Once your environment is active, install the necessary dependencies from the pyproject.toml file:
pip install .
- Set up your Azure OpenAI API credentials:
You'll need to provide your Azure OpenAI API credentials in the script (api_key, api_base, etc.) before running the project. You can find these details in the Azure portal under your OpenAI resource.
To run the example and see the structured output:
-
Modify the script main.py and replace
<AZURE_OPENAI_GPT4O_V2024_05_13_ENDPOINT>
,<AZURE_OPENAI_GPT4O_V2024_05_13_KEY>
,<AZURE_OPENAI_GPT4O_V2024_08_06_ENDPOINT>
, and<AZURE_OPENAI_GPT4O_V2024_08_06_KEY>
with your actual Azure OpenAI credentials. -
Run the script:
python scripts/001-run.py
The script will make a call to the Azure OpenAI API, request a structured JSON response, and parse the response into a pydantic model.