Skip to content

Commit

Permalink
Merge pull request #1 from Azure-Samples/githubmodels
Browse files Browse the repository at this point in the history
Add support for GitHub models
  • Loading branch information
pamelafox authored Nov 27, 2024
2 parents b59400b + e1ed4d0 commit 80b1ee5
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 82 deletions.
7 changes: 4 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"charliermarsh.ruff",
"ms-python.vscode-pylance",
"charliermarsh.ruff",
"ms-azuretools.azure-dev",
"ms-azuretools.vscode-bicep"
"ms-azuretools.vscode-bicep",
"humao.rest-client"
],
"python.defaultInterpreterPath": "/usr/local/bin/python",
"[python]": {
Expand Down
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
OPENAI_HOST=azure # Options are azure or github
AZURE_OPENAI_GPT_DEPLOYMENT=
AZURE_OPENAI_SERVICE=
AZURE_TENANT_ID=
GITHUB_TOKEN=
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,36 @@ A related option is VS Code Dev Containers, which will open the project in your

## Running the Python examples

1. If you're not already running in a Codespace or Dev Container, create a Python virtual environment.
To run the samples, you'll either need to have already [deployed the Azure OpenAI account](#deployment) or use GitHub models.
2. Install the requirements:
1. Check that the `.env` file exists in the root of the project. If you [deployed an Azure OpenAI account](#deployment), it should have been created for you, and look like this:
```shell
OPENAI_HOST=azure
AZURE_OPENAI_GPT_DEPLOYMENT=gpt-4o
AZURE_OPENAI_SERVICE=your-service-name
AZURE_TENANT_ID=your-tenant-id-1234
```
If you're using GitHub models, create a `.env` file with the following content:

```shell
OPENAI_HOST=github
GITHUB_TOKEN=
```

You can create a GitHub token by following the [GitHub documentation](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token),
or open this project inside GitHub Codespaces where the token is already exposed as an environment variable.

2. If you're not already running in a Codespace or Dev Container, create a Python virtual environment.
3. Install the requirements:
```shell
python -m pip install -r requirements.txt
```
3. Run an example by running either `python example_file.py` or selecting the `Run` button on the opened file. Available examples:
4. Run an example by running either `python example_file.py` or selecting the `Run` button on the opened file. Available examples:
| Script filename | Description |
|---------------------------|-----------------------------------------------------------------------------|
Expand Down
35 changes: 23 additions & 12 deletions extract_github_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@
logging.basicConfig(level=logging.WARNING)
load_dotenv()

# Configure Azure OpenAI
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT environment variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")

if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"})
model_name = "gpt-4o"


# Define models for Structured Outputs
Expand Down
35 changes: 23 additions & 12 deletions extract_github_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@
logging.basicConfig(level=logging.WARNING)
load_dotenv()

# Configure Azure OpenAI
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT environment variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"})
model_name = "gpt-4o"


# Define models for Structured Outputs
Expand Down Expand Up @@ -69,7 +80,7 @@ class RepoOverview(BaseModel):

# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
model=model_name,
messages=[
{
"role": "system",
Expand Down
36 changes: 23 additions & 13 deletions extract_image_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@
logging.basicConfig(level=logging.WARNING)
load_dotenv()

# Configure Azure OpenAI
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT environment variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"})
model_name = "gpt-4o"


# Define models for Structured Outputs
Expand All @@ -46,7 +56,7 @@ def open_image_as_base64(filename):

# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
model=model_name,
messages=[
{"role": "system", "content": "Extract the information from the graph"},
{
Expand Down
35 changes: 23 additions & 12 deletions extract_image_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,28 @@
logging.basicConfig(level=logging.WARNING)
load_dotenv()

# Configure Azure OpenAI
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT environment variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"})
model_name = "gpt-4o"


# Define models for Structured Outputs
Expand Down Expand Up @@ -53,7 +64,7 @@ def open_image_as_base64(filename):

# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
model=model_name,
messages=[
{"role": "system", "content": "Extract the information from the table"},
{
Expand Down
36 changes: 23 additions & 13 deletions extract_pdf_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@
logging.basicConfig(level=logging.WARNING)
load_dotenv()

# Configure Azure OpenAI
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT environment variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"})
model_name = "gpt-4o"


# Define models for Structured Outputs
Expand All @@ -45,7 +55,7 @@ class Receipt(BaseModel):

# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
model=model_name,
messages=[
{"role": "system", "content": "Extract the information from the blog post"},
{"role": "user", "content": md_text},
Expand Down
37 changes: 23 additions & 14 deletions extract_webpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@
logging.basicConfig(level=logging.WARNING)
load_dotenv()

# Configure Azure OpenAI
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT environment variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")

if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"})
model_name = "gpt-4o"

# Define models for Structured Outputs
class BlogPost(BaseModel):
Expand All @@ -46,7 +55,7 @@ class BlogPost(BaseModel):

# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
model=model_name,
messages=[
{"role": "system", "content": "Extract the information from the blog post"},
{"role": "user", "content": f"{post_title}\n{post_contents}"},
Expand Down
3 changes: 3 additions & 0 deletions http/.env.azure
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AZURE_OPENAI_SERVICE=
AZURE_OPENAI_GPT_DEPLOYMENT=
AZURE_OPENAI_TOKEN=
2 changes: 2 additions & 0 deletions http/.env.github
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# In Codespaces, you can find this value in the env variables. Otherwise, generate a new Personal Access Token in GitHub.
GITHUB_TOKEN=
Loading

0 comments on commit 80b1ee5

Please sign in to comment.