Skip to main content

GitHub Models 快速入门

使用 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”视图自定义要测试的模型的参数,看看它们会如何影响响应。

    注意

    如果已登录 GitHub,可直接开始使用操场。 它使用你的 GitHub 帐户进行访问 - 无需进行设置或 API 密钥。

步骤 2:进行 API 调用

有关可用字段、标头和请求格式的完整详细信息,请参阅 GitHub Models 的 API 参考

若要以编程方式调用模型,需要:

  • GitHub 帐户。
  • 具有 models 作用域的 personal access token (PAT),可在设置中创建。
  1. 运行以下 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?"}]}'
    
  2. 会收到如下所示的响应:

    {
      "choices": [
        {
          "message": {
            "role": "assistant",
            "content": "The capital of France is **Paris**."
          }
        }
      ],
      ...other fields omitted
    }
    
  3. 若要尝试其他模型,可将 JSON 有效负载中 model 字段的值更改为市场中的一个值。

步骤 3:在 GitHub Actions 中运行模型

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

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

    name: GitHub Models Demo
    
    on: [push]
    
    permissions:
      contents: read
      models: read
    
    jobs:
      summarize-repo:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v4
    
          - name: Summarize the repository README
            run: |
              SUMMARY_INPUT=$(head -c 4000 README.md)
              PROMPT="Summarize this repository in one sentence. Here is the README:\n$SUMMARY_INPUT"
              PAYLOAD=$(jq -n --arg prompt "$PROMPT" '{
                model: "openai/gpt-4.1",
                messages: [
                  {role: "user", content: $prompt}
                ]
              }')
              RESPONSE=$(curl -sL \
                -X POST \
                -H "Accept: application/vnd.github+json" \
                -H "Authorization: Bearer $" \
                -H "X-GitHub-Api-Version: 2022-11-28" \
                -H "Content-Type: application/json" \
                https://models.github.ai/inference/chat/completions \
                -d "$PAYLOAD")
              echo "$RESPONSE" | jq -r '.choices[0].message.content'
    

    注意

    调用 GitHub Models 的工作流必须在权限块中包含 models: read。 GitHub 承载的运行器自动提供 GITHUB_TOKEN

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

此示例演示如何向模型发送提示并在持续集成 (CI) 工作流中使用响应。 有关更高级的用例,例如汇总问题、检测 bug 报告缺少的重现步骤或响应拉取请求,请参阅 将 AI 模型集成到开发工作流中

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

GitHub Models 支持 .prompt.yml 文件中定义的可重用提示。 将此文件添加到存储库后,它将显示在存储库的“Models”页中,并且可以直接在提示编辑器和评估工具中运行。 详细了解 Storing prompts in GitHub repositories

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

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

    name: One-line summary
    description: Ask the model to summarize a paragraph in one sentence.
    messages:
      - role: user
        content: 'Summarize the following text in one sentence: '
    model: openai/gpt-4o
    
  3. 提交文件并将其推送到存储库。

  4. **** 转到存储库中的“Models”选项卡。

  5. **** 在导航菜单中,单击“ Prompts”,然后单击提示文件。

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

    注意

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

步骤 5:设置第一个评估

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

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

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

      name: One-line summary
      description: Ask the model to summarize a paragraph in one sentence.
      messages:
        - role: user
          content: 'Summarize the following text in one sentence: '
      model: openai/gpt-4o
      testData:
        - 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: >-
            The museum's new dinosaur exhibit attracted many families with its fossils
            and interactive displays.
        - input: >-
            Lucy baked cookies for the school fundraiser. She spent the entire evening
            in the kitchen to make sure there were enough for everyone.
          expected: Lucy baked cookies all evening to support the school fundraiser.
      evaluators:
        - name: Similarity
          uses: github/similarity
    
  3. 提交文件并将其推送到存储库。

  4. **** 在存储库中,单击“Models”选项卡。**** 然后单击“ Prompts”,并在提示编辑器中重新打开相同的提示。

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

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

    提示

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

后续步骤