简介
GitHub Models 是 GitHub 提供的 AI 推理 API,您只需使用 GitHub 凭据即可运行 AI 模型。您可以从众多模型中选择——包括 OpenAI、Meta 和 DeepSeek 的模型——并在脚本、应用或 GitHub Actions 中使用它们,无需单独的身份验证流程。
本指南帮助您在 Playground 中快速尝试模型,然后展示如何通过 API 或工作流运行您的第一个模型。
步骤 1:在 Playground 中尝试模型
-
在 Playground 中,从下拉菜单中选择至少一个模型。
-
使用“Chat”视图测试不同提示,并比较不同模型的响应。
-
使用“Parameters”视图自定义您正在测试的模型参数,然后观察它们对响应的影响。
注意
只要登录 GitHub,Playground 即可直接使用。它使用您的 GitHub 账户进行访问——无需设置或 API 密钥。
步骤 2:进行 API 调用
有关可用字段、标头和请求格式的完整细节,请参阅 GitHub Models API 参考文档。
要以编程方式调用模型,您需要
- 一个 GitHub 账户。
- 一个具有
models权限范围的个人访问令牌(PAT),您可以在 设置 中创建。
-
运行以下
curl命令,将YOUR_GITHUB_PAT替换为您的令牌。Bash curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer YOUR_GITHUB_PAT" \ -H "X-GitHub-Api-Version: 2022-11-28" \ -H "Content-Type: application/json" \ https://models.github.ai/inference/chat/completions \ -d '{"model":"openai/gpt-4.1","messages":[{"role":"user","content":"What is the capital of France?"}]}'curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer YOUR_GITHUB_PAT" \ -H "X-GitHub-Api-Version: 2022-11-28" \ -H "Content-Type: application/json" \ https://models.github.ai/inference/chat/completions \ -d '{"model":"openai/gpt-4.1","messages":[{"role":"user","content":"What is the capital of France?"}]}' -
您将收到如下响应
{ "choices": [ { "message": { "role": "assistant", "content": "The capital of France is **Paris**." } } ], ...other fields omitted } -
要尝试其他模型,请将 JSON 负载中的
model字段的值更改为 marketplace 中的某个模型。
步骤 3:在 GitHub Actions 中运行模型
-
在您的仓库中,在
.github/workflows/models-demo.yml创建工作流文件。 -
将以下工作流粘贴到您刚创建的文件中。
YAML name: Use GitHub Models on: [push] permissions: models: read jobs: call-model: runs-on: ubuntu-latest steps: - name: Call AI model env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | curl "https://models.github.ai/inference/chat/completions" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $GITHUB_TOKEN" \ -d '{ "messages": [ { "role": "user", "content": "Explain the concept of recursion." } ], "model": "openai/gpt-4o" }'name: Use GitHub Models on: [push] permissions: models: read jobs: call-model: runs-on: ubuntu-latest steps: - name: Call AI model env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | curl "https://models.github.ai/inference/chat/completions" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $GITHUB_TOKEN" \ -d '{ "messages": [ { "role": "user", "content": "Explain the concept of recursion." } ], "model": "openai/gpt-4o" }'注意
调用 GitHub Models 的工作流必须在 permissions 区块中包含
models: read。GitHub 托管的运行器会自动提供GITHUB_TOKEN。 -
提交并推送以触发工作流。
此示例展示了如何向模型发送提示并在持续集成(CI)工作流中使用其响应。有关更高级的用例(例如对 issue 进行摘要、检测 bug 报告中缺失的复现步骤,或对 pull request 作出响应),请参阅 在 GitHub Copilot 中配置 AI 模型访问。
步骤 4:保存您的第一个提示文件
GitHub Models 支持在 .prompt.yml 文件中定义可重用的提示。将此文件添加到仓库后,它会出现在仓库的 Models 页面中,并可直接在 Prompt Editor 和评估工具中运行。了解更多关于 在 GitHub 仓库中存储提示 的信息。
-
在您的仓库中,创建名为
summarize.prompt.yml的文件。您可以将其保存在任意目录下。 -
将以下示例提示粘贴到您刚创建的文件中。
YAML name: Text Summarizer description: Summarizes input text concisely model: openai/gpt-4o-mini modelParameters: temperature: 0.5 messages: - role: system content: You are a text summarizer. Your only job is to summarize text given to you. - role: user content: | Summarize the given text, beginning with "Summary -": <text> {{input}} </text>name: Text Summarizer description: Summarizes input text concisely model: openai/gpt-4o-mini modelParameters: temperature: 0.5 messages: - role: system content: You are a text summarizer. Your only job is to summarize text given to you. - role: user content: | Summarize the given text, beginning with "Summary -": <text> {{input}} </text> -
提交并推送该文件到您的仓库。
-
前往您仓库中的 Models 标签页。
-
在导航菜单中,点击 提示,然后点击该提示文件。
-
提示将在 Prompt Editor 中打开。点击 Run。右侧边栏会出现,要求您输入文本。输入任意文本后,再次点击右下角的 Run 进行测试。
注意
Prompt Editor 不会自动将仓库内容传递给提示。您需要手动提供输入。
步骤 5:设置您的第一个评估
评估帮助您衡量不同模型在相同输入下的响应,从而选择最适合您使用场景的模型。
-
返回到您在上一步创建的
summarize.prompt.yml文件。 -
将文件更新为以下示例。
YAML name: Text Summarizer description: Summarizes input text concisely model: openai/gpt-4o-mini modelParameters: temperature: 0.5 messages: - role: system content: You are a text summarizer. Your only job is to summarize text given to you. - role: user content: | Summarize the given text, beginning with "Summary -": <text> {{input}} </text> testData: - input: | The quick brown fox jumped over the lazy dog. The dog was too tired to react. expected: Summary - A fox jumped over a lazy, unresponsive dog. - input: | The museum opened a new dinosaur exhibit this weekend. Families from all over the city came to see the life-sized fossils and interactive displays. expected: Summary - The museum's new dinosaur exhibit attracted many families with its fossils and interactive displays. evaluators: - name: Output should start with 'Summary -' string: startsWith: 'Summary -' - name: Similarity uses: github/similarityname: Text Summarizer description: Summarizes input text concisely model: openai/gpt-4o-mini modelParameters: temperature: 0.5 messages: - role: system content: You are a text summarizer. Your only job is to summarize text given to you. - role: user content: | Summarize the given text, beginning with "Summary -": <text> {{input}} </text> testData: - input: | The quick brown fox jumped over the lazy dog. The dog was too tired to react. expected: Summary - A fox jumped over a lazy, unresponsive dog. - input: | The museum opened a new dinosaur exhibit this weekend. Families from all over the city came to see the life-sized fossils and interactive displays. expected: Summary - The museum's new dinosaur exhibit attracted many families with its fossils and interactive displays. evaluators: - name: Output should start with 'Summary -' string: startsWith: 'Summary -' - name: Similarity uses: github/similarity -
提交并推送该文件到您的仓库。
-
在您的仓库中,点击 Models 标签页。然后点击 提示,并在提示编辑器中重新打开同一提示。
-
在左上角,您可以在 Edit 与 Compare 视图之间切换。点击 Compare。
-
评估将自动设置。点击 Run 查看结果。
提示
点击 Add prompt,您可以使用不同模型运行同一提示,或修改提示文字,以一次性获得多种变体的推理响应、查看评估结果,并并排比较,以做出数据驱动的模型选择。