Chapter 09
GitHub Models 中的 Phi 系列
GitHub Models 中的 Phi 系列
欢迎来到 GitHub Models!我们已经为您准备好了一切,随时可以探索托管在 Azure AI 上的 AI 模型。

有关 GitHub Models 上可用模型的更多信息,请查看 GitHub Model Marketplace
可用模型
每个模型都有专属的 playground 和示例代码

GitHub Model Catalog 中的 Phi 系列
快速开始
这里有一些基础示例,您可以直接运行。它们都在 samples 目录下。如果您想直接查看自己喜欢的语言示例,可以在以下语言中找到:
- Python
- JavaScript
- C#
- Java
- cURL
此外,还有专门的 Codespaces 环境用于运行示例和模型。

示例代码
下面是几个使用场景的示例代码片段。有关 Azure AI Inference SDK 的更多信息,请参阅完整文档和示例。
设置
- 创建个人访问令牌
您无需为令牌赋予任何权限。请注意,令牌会发送到 Microsoft 服务。
要使用下面的代码片段,请创建一个环境变量,将您的令牌设置为客户端代码的密钥。
如果您使用 bash:
export GITHUB_TOKEN="<your-github-token-goes-here>"如果您使用 powershell:
$Env:GITHUB_TOKEN="<your-github-token-goes-here>"如果您使用 Windows 命令提示符:
set GITHUB_TOKEN=<your-github-token-goes-here>Python 示例
安装依赖
使用 pip 安装 Azure AI Inference SDK(要求:Python >=3.8):
pip install azure-ai-inference运行基础示例代码
此示例演示了对 chat completion API 的基本调用。它使用了 GitHub AI 模型推理端点和您的 GitHub 令牌。调用是同步的。
import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential
endpoint = "https://models.inference.ai.azure.com"
model_name = "Phi-4"
token = os.environ["GITHUB_TOKEN"]
client = ChatCompletionsClient(
endpoint=endpoint,
credential=AzureKeyCredential(token),
)
response = client.complete(
messages=[
UserMessage(content="I have $20,000 in my savings account, where I receive a 4% profit per year and payments twice a year. Can you please tell me how long it will take for me to become a millionaire? Also, can you please explain the math step by step as if you were explaining it to an uneducated person?"),
],
temperature=0.4,
top_p=1.0,
max_tokens=2048,
model=model_name
)
print(response.choices[0].message.content)运行多轮对话
此示例展示了如何使用 chat completion API 进行多轮对话。当您用模型做聊天应用时,需要管理对话历史,并将最新消息发送给模型。
import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import AssistantMessage, SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential
token = os.environ["GITHUB_TOKEN"]
endpoint = "https://models.inference.ai.azure.com"
# Replace Model_Name
model_name = "Phi-4"
client = ChatCompletionsClient(
endpoint=endpoint,
credential=AzureKeyCredential(token),
)
messages = [
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="What is the capital of France?"),
AssistantMessage(content="The capital of France is Paris."),
UserMessage(content="What about Spain?"),
]
response = client.complete(messages=messages, model=model_name)
print(response.choices[0].message.content)流式输出
为了更好的用户体验,您可以流式传输模型的响应,这样第一个 token 会更早出现,避免等待较长的响应时间。
import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential
token = os.environ["GITHUB_TOKEN"]
endpoint = "https://models.inference.ai.azure.com"
# Replace Model_Name
model_name = "Phi-4"
client = ChatCompletionsClient(
endpoint=endpoint,
credential=AzureKeyCredential(token),
)
response = client.complete(
stream=True,
messages=[
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="Give me 5 good reasons why I should exercise every day."),
],
model=model_name,
)
for update in response:
if update.choices:
print(update.choices[0].delta.content or "", end="")
client.close()GitHub Models 的免费使用和速率限制

playground 和免费 API 使用的速率限制 旨在帮助您试验模型和快速原型您的 AI 应用。超出这些限制后,若要将应用扩展到生产环境,您必须从 Azure 账户中配置资源,并从那里进行身份验证,而不是使用 GitHub 个人访问令牌。您无需更改代码中的其他内容。请使用此链接了解如何突破 Azure AI 免费层限制。
免责声明
请记住,与模型交互时,您是在试验 AI,因此内容可能存在错误。
该功能受多种限制(包括每分钟请求数、每日请求数、每次请求的 token 数和并发请求数),不适合生产环境使用。
GitHub Models 使用 Azure AI 内容安全过滤。这些过滤器无法关闭,作为 GitHub Models 体验的一部分。如果您选择通过付费服务使用模型,请根据需求配置内容过滤器。
此服务受 GitHub 预发布条款约束。
免责声明:
本文件使用 AI 翻译服务 Co-op Translator 进行翻译。虽然我们力求准确,但请注意自动翻译可能包含错误或不准确之处。原始文件的母语版本应被视为权威来源。对于重要信息,建议使用专业人工翻译。对于因使用本翻译而产生的任何误解或误释,我们不承担任何责任。
