本文提供了一个在Python中调用微软Azure平台提供的LLM API的示例
在Azure平台注册账号之后登录,会看到一个Azure Services的选项
点击“Create a resource”来创建一个资源
在搜索栏搜索“open ai”, 点击创建资源
点击创建
之后填写详细信息
- Subscription指的是所有在该订阅下创建的资源统一共用同一个账单来计费
- Resource group指的是在该组内的资源使用统一的生命周期、权限以及策略
- 不同的Region会有不同的调用Quata限制,具体参考:learn.microsoft.com/zh-cn/azure…
- Price tier指的是不同的定价套餐
填写完毕之后,点击“Next”
之后选择可以访问该资源的网络范围
可以为该资源添加一些标签方便之后的分类管理
确认无误之后,点击创建
之后等待几分钟,资源就被创建好了,会跳转到该页面
点击“Go to resource”
之后我们点击“Explore Azure AI Foundry portal”来部署所需要的模型
我们尝试部署“gpt-4”
之后,可以在代码中调用该模型API
import os
from openai import AzureOpenAI
endpoint = "https://a-test-of-azure.openai.azure.com/"
model_name = "gpt-4"
deployment = "gpt-4"
subscription_key = "<your-api-key>"
api_version = "2024-12-01-preview"
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
api_key=subscription_key,
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": "Hello, who are you?",
}
],
max_tokens=4096,
temperature=1.0,
top_p=1.0,
model=deployment
)
print(response.choices[0].message.content)
将“subscription_key”替换为该模型页面的
直接复制即可
运行结果