简介
GitHub Models 是来自 GitHub 的 AI 推理 API,使用它,只需要使用 GitHub 凭据即可运行 AI 模型。 可以从许多不同的模型(包括 OpenAI、Meta 和 DeepSeek)中进行选择,并在脚本、应用甚至 GitHub Actions 中使用这些模型,而无需设置单独的身份验证过程。
本指南可帮助你在操场中快速试用模型,并演示如何通过 API 或工作流运行第一个模型。
步骤 1:在操场中试用模型
-
在操场中,从下拉菜单中选择至少一个模型。
-
** **使用“Chat”视图测试不同的提示,并比较不同模型的响应。 -
** **使用“Parameters”视图自定义要测试的模型的参数,看看它们会如何影响响应。注意
如果已登录 GitHub,可直接开始使用操场。 它使用你的 GitHub 帐户进行访问 - 无需进行设置或 API 密钥。
步骤 2:进行 API 调用
有关可用字段、标头和请求格式的完整详细信息,请参阅 GitHub Models 的 API 参考。
若要以编程方式调用模型,需要:
- GitHub 帐户。
- 具有
models作用域的 personal access token (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字段的值更改为市场中的一个值。
步骤 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 的工作流必须在权限块中包含
models: read。 GitHub 承载的运行器自动提供GITHUB_TOKEN。 -
提交并推送以触发工作流。
此示例演示如何向模型发送提示并在持续集成 (CI) 工作流中使用响应。 有关更高级的用例,例如汇总问题、检测 bug 报告缺少的重现步骤或响应拉取请求,请参阅 在 GitHub Copilot 中配置对 AI 模型的访问权限。
步骤 4:保存第一个提示文件
GitHub Models 支持 .prompt.yml 文件中定义的可重用提示。 将此文件添加到存储库后,它将显示在存储库的“Models”页中,并且可以直接在提示编辑器和评估工具中运行。 详细了解 在 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> -
提交文件并将其推送到存储库。
-
转到存储库中的模型选项���。
-
** **在导航菜单中,单击“<svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-note" aria-label="none" role="img"><path d="M0 3.75C0 2.784.784 2 1.75 2h12.5c.966 0 1.75.784 1.75 1.75v8.5A1.75 1.75 0 0 1 14.25 14H1.75A1.75 1.75 0 0 1 0 12.25Zm1.75-.25a.25.25 0 0 0-.25.25v8.5c0 .138.112.25.25.25h12.5a.25.25 0 0 0 .25-.25v-8.5a.25.25 0 0 0-.25-.25ZM3.5 6.25a.75.75 0 0 1 .75-.75h7a.75.75 0 0 1 0 1.5h-7a.75.75 0 0 1-.75-.75Zm.75 2.25h4a.75.75 0 0 1 0 1.5h-4a.75.75 0 0 1 0-1.5Z"></path></svg> Prompts”,然后单击提示文件。 -
提示将在提示编辑器中打开。 单击 “运行” 。 将显示一个右侧边栏,并提示输入文本。 ** **输入任何文本,然后再次单击右下角的“Run”进行测试。
注意
提示编辑器不会自动将存储库内容传递到提示中。 手动提供输入。
步骤 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 -
提交文件并将其推送到存储库。
-
在您的存储库中,单击模型选项卡。然后单击 提示,并在提示编辑器中重新打开相同的提示。
-
在左上角,可以将视图从 "Edit" 切换为 "Compare"。 单击“比较”。
-
将自动设置评估流程。 ** **单击“Run”查看结果。
提示
通过单击“Add prompt”,可以**** 使用不同的模型运行相同的提示,或更改提示词,一次性获得多种变体的推理响应,还能查看评估情况,并将它们并排比较,从而做出基于数据的模型选择。
后续步骤
-
[AUTOTITLE](/github-models/about-github-models)。 -
[浏览模型目录](https://github.com/marketplace?type=models) -
[AUTOTITLE](/github-models/use-github-models/storing-prompts-in-github-repositories) -
[AUTOTITLE](/github-models/use-github-models/evaluating-ai-models) -
[AUTOTITLE](/github-models/use-github-models/integrating-ai-models-into-your-development-workflow#using-ai-models-with-github-actions)