
API接口重试的8种方法
在人工智能技术快速发展的今天,企业级AI服务正在重塑各行各业的创新范式。Azure OpenAI作为微软推出的企业级AI服务平台,将OpenAI领先的生成式AI能力与Azure云的安全合规特性完美结合,为开发者提供了开箱即用的大模型服务。本教程将带您从零开始,逐步掌握Azure OpenAI API的核心使用方法。
注意:部分区域需要单独申请OpenAI服务访问权限,可在Azure门户提交申请表单
# 使用Azure CLI快速创建资源
az cognitiveservices account create \
--name my-openai-resource \
--resource-group my-resource-group \
--kind OpenAI \
--sku S0 \
--location eastus2 \
--yes
在Azure门户导航至创建的OpenAI资源
pip install python-dotenv openai
创建.env文件:
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_KEY=your-api-key
AZURE_OPENAI_DEPLOYMENT=your-deployment-name
import os
from dotenv import load_dotenv
from openai import AzureOpenAI
load_dotenv()
client = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_KEY"),
api_version="2024-02-01",
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
)
response = client.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
prompt="人工智能将改变",
temperature=0.7,
max_tokens=100
)
print(response.choices[0].text)
参数说明表:
参数 | 类型 | 说明 | 推荐值 |
---|---|---|---|
temperature | float | 生成随机性 | 0.5-1.0 |
max_tokens | int | 最大输出长度 | 根据需求调整 |
top_p | float | 核采样阈值 | 0.9-1.0 |
frequency_penalty | float | 重复惩罚系数 | 0.0-2.0 |
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=[
{"role": "system", "content": "你是一位资深技术专家"},
{"role": "user", "content": "请用通俗语言解释量子计算"}
],
temperature=0.5
)
print(response.choices[0].message.content)
消息角色说明:
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=[{"role": "user", "content": "讲述一个科技创业故事"}],
stream=True
)
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
}
}
]
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=[{"role": "user", "content": "北京现在天气如何?"}],
tools=tools
)
print(response.choices[0].message.tool_calls)
# 启用内容过滤
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=[...],
filter_params={
"hate": "block",
"self_harm": "alert"
}
)
az monitor metrics list \
--resource my-openai-resource \
--metric "TotalTokens"
def generate_response(query):
history = load_chat_history()
messages = [
{"role": "system", "content": "你是一个专业的客服助理"},
*history,
{"role": "user", "content": query}
]
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=messages,
temperature=0.3
)
return response.choices[0].message.content
def summarize_document(text):
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=[{
"role": "user",
"content": f"请用三点总结以下文档内容:\n{text}"
}],
temperature=0.1
)
return response.choices[0].message.content
def generate_python_code(requirement):
messages = [
{"role": "system", "content": "你是一个专业的Python开发助手"},
{"role": "user", "content": f"请生成满足以下需求的Python代码:{requirement}"}
]
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=messages,
temperature=0.2
)
return extract_code(response.choices[0].message.content)
from tenacity import retry, wait_exponential
@retry(wait=wait_exponential(multiplier=1, max=10))
def safe_api_call():
return client.chat.completions.create(...)
通过本教程,您已经掌握了Azure OpenAI API的核心使用方法和最佳实践。建议从以下方向深入探索:
Azure OpenAI正在持续更新模型版本和功能特性,建议定期查阅官方文档获取最新信息。期待您利用这项强大的技术创造出改变世界的应用!