# GitHub模型的快速入门

使用 GitHub Models 分钟数运行第一个模型。

## 简介

GitHub Models 是来自GitHub的 AI 推理 API，允许你仅通过GitHub凭据来运行 AI 模型。 可以从许多不同的模型（包括 OpenAI、Meta 和 DeepSeek）中进行选择，并在脚本、应用甚至 GitHub Actions不使用单独的身份验证过程的情况下使用它们。

本指南可帮助你在操场中快速试用模型，并演示如何通过 API 或工作流运行第一个模型。

## 步骤 1：在操场中试用模型

1. 转到 **<https://github.com/marketplace/models>**。
2. 在操场中，从下拉菜单中选择至少一个模型。
3. \*\*
   \*\*使用“Chat”视图测试不同的提示，并比较不同模型的响应。
4. \*\*
   \*\*使用“Parameters”视图自定义要测试的模型的参数，看看它们会如何影响响应。

   > \[!NOTE]
   > 如果已登录 GitHub，则操场已开箱即用。 它使用 GitHub 帐户进行访问 - 无需设置或 API 密钥。

## 步骤 2：进行 API 调用

有关可用字段、标头和请求格式的完整详细信息，请参阅 [API GitHub Models参考](/zh/rest/models/inference?apiVersion=2022-11-28)。

若要以编程方式调用模型，需要：

* 帐户 GitHub 。
* 可以在[设置](https://github.com/settings/tokens)中创建一个具有`models`的personal access token。

1. 运行以下 `curl` 命令，将 `YOUR_GITHUB_PAT` 替换为自己的令牌。

   ```bash copy
     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?"}]}'
   ```

2. 会收到如下所示的响应：

   ```json
   {
     "choices": [
       {
         "message": {
           "role": "assistant",
           "content": "The capital of France is **Paris**."
         }
       }
     ],
     ...other fields omitted
   }
   ```

3. 若要尝试其他模型，请将 JSON 有效负载中的 `model` 字段的值从 [marketplace](https://github.com/marketplace/models) 更改为一个。

## 步骤 3：在 GitHub Actions 中运行模型

1. 在存储库中，在 `.github/workflows/models-demo.yml` 创建工作流文件。

2. 将以下工作流粘贴到刚刚创建的文件中。

   ```yaml copy
   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"
               }'
   ```

   > \[!NOTE]
   > 调用 GitHub Models 的工作流必须包含在 `models: read` 权限块中。
   > GitHub-hosted runners 自动提供 `GITHUB_TOKEN`。

3. 提交并推送以触发工作流。

此示例演示如何向模型发送提示并在持续集成 (CI) 工作流中使用响应。 有关更高级的用例，例如汇总问题、检测 bug 报告缺少的重现步骤或响应拉取请求，请参阅 [在 GitHub Copilot 中配置对 AI 模型的访问](/zh/github-models/use-github-models/integrating-ai-models-into-your-development-workflow)。

## 步骤 4：保存第一个提示文件

GitHub Models 支持文件中 `.prompt.yml` 定义的可重用提示。 将此文件添加到存储库后，它将显示在存储库的“Models”页中，并且可以直接在提示编辑器和评估工具中运行。 详细了解 [在 GitHub 存储库中存储提示](/zh/github-models/use-github-models/storing-prompts-in-github-repositories)。

1. 在存储库中，创建名为 `summarize.prompt.yml` 的文件。 可以将其保存在任何目录中。

2. 将以下示例提示粘贴到刚刚创建的文件中。

   ```yaml copy
   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>
   ```

3. 提交文件并将其推送到存储库。

4. 转到存储库中的**模型**选项卡。

5. 在导航菜单中，单击 **“提示”<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>**，然后单击提示文件。

6. 提示将在提示编辑器中打开。 单击 **“运行”** 。 将显示一个右侧边栏，并提示输入文本。
   \*\*
   \*\*输入任何文本，然后再次单击右下角的“Run”进行测试。

   > \[!NOTE]
   > 提示编辑器不会自动将存储库内容传递到提示中。 手动提供输入。

## 步骤 5：设置第一个评估

通过评估，可测量不同模型对相同输入的响应方式，从而为用例选择最优模型。

1. 请返回您在上一步中创建的 `summarize.prompt.yml` 文件。

2. 更新文件以匹配以下示例。

   ```yaml copy
   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/similarity
   ```

3. 提交文件并将其推送到存储库。

4. 在存储库中，单击“**模型**”选项卡。然后单击 **“提示”<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>**，然后在提示编辑器中重新打开同一提示。

5. 在左上角，可以将视图从 **"Edit"** 切换为 **"Compare"**。 单击“比较”。

6. 将自动设置评估。
   \*\*
   \*\*单击“Run”查看结果。

   > \[!TIP]
   > 通过单击“Add prompt”，可以\*\*\*\* 使用不同的模型运行相同的提示，或更改提示词，一次性获得多种变体的推理响应，还能查看评估情况，并将它们并排比较，从而做出基于数据的模型选择。

## 后续步骤

* [关于GitHub模型](/zh/github-models/about-github-models)。
* [浏览模型目录](https://github.com/marketplace?type=models)
* [在 GitHub 存储库中存储提示](/zh/github-models/use-github-models/storing-prompts-in-github-repositories)
* [评估 AI 模型](/zh/github-models/use-github-models/evaluating-ai-models)
* [在 GitHub Copilot 中配置对 AI 模型的访问](/zh/github-models/use-github-models/integrating-ai-models-into-your-development-workflow#using-ai-models-with-github-actions)