Files
2026-07-13 12:59:43 +08:00

101 lines
2.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"\n",
"from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",
"from openai import OpenAI\n",
"\n",
"# This sample uses the Azure OpenAI Responses API via the stable /openai/v1/ endpoint.\n",
"# GitHub Models is deprecated (retiring July 2026) and does not support the Responses API,\n",
"# so we call Azure OpenAI directly instead.\n",
"endpoint = os.environ[\"AZURE_OPENAI_ENDPOINT\"]\n",
"deployment = os.environ.get(\"AZURE_OPENAI_DEPLOYMENT\", \"gpt-4o-mini\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Authenticate with Entra ID (run `az login` first). No API version is needed with the v1 endpoint.\n",
"token_provider = get_bearer_token_provider(\n",
" DefaultAzureCredential(),\n",
" \"https://cognitiveservices.azure.com/.default\",\n",
")\n",
"\n",
"client = OpenAI(\n",
" base_url=f\"{endpoint.rstrip('/')}/openai/v1/\",\n",
" api_key=token_provider,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"role = \"travel agent\"\n",
"company = \"contoso travel\"\n",
"responsibility = \"booking flights\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = client.responses.create(\n",
" model=deployment,\n",
" input=[\n",
" {\"role\": \"system\", \"content\": \"\"\"You are an expert at creating AI agent assistants. \n",
"You will be provided a company name, role, responsibilities and other\n",
"information that you will use to provide a system prompt for.\n",
"To create the system prompt, be descriptive as possible and provide a structure that a system using an LLM can better understand the role and responsibilities of the AI assistant.\"\"\"},\n",
" {\"role\": \"user\", \"content\": f\"You are {role} at {company} that is responsible for {responsibility}.\"},\n",
" ],\n",
" # Optional parameters\n",
" temperature=1.0,\n",
" max_output_tokens=1000,\n",
" top_p=1.0,\n",
" store=False,\n",
")\n",
"\n",
"print(response.output_text)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}