GitHub Models 是 GitHub 提供的 AI 模型平台,让你可以直接在 GitHub 上使用和测试各种 AI 模型。
| 模型 | 提供商 | 用途 |
|---|---|---|
| GPT-4o | OpenAI | 通用对话、代码生成 |
| GPT-4o-mini | OpenAI | 轻量级任务 |
| Claude 3.5 Sonnet | Anthropic | 代码分析、对话 |
| Llama 3.1 | Meta | 开源通用模型 |
| Mistral | Mistral AI | 欧洲开源模型 |
| Phi-3 | Microsoft | 轻量级模型 |
| GitLab Foundation | GitLab | 代码相关任务 |
# 设置 API Key
export GITHUB_TOKEN="your-github-token"
# 调用 GPT-4o
curl -X POST "https://models.inference.ai.azure.com/chat/completions" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Hello, who are you?"}
]
}'from openai import OpenAI
client = OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a Python function to sort a list"}
]
)
print(response.choices[0].message.content)# .github/workflows/ai-review.yml
name: AI Code Review
on:
pull_request:
permissions:
contents: read
pull-requests: write
jobs:
ai-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get PR diff
id: diff
run: |
DIFF=$(gh pr diff ${{ github.event.pull_request.number }})
echo "diff<<EOF" >> $GITHUB_OUTPUT
echo "$DIFF" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: AI Review
run: |
curl -X POST "https://models.inference.ai.azure.com/chat/completions" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "Review this code change and provide feedback on bugs, improvements, and best practices."},
{"role": "user", "content": "${{ steps.diff.outputs.diff }}"}
]
}'def ai_code_review(diff: str) -> str:
"""使用 AI 审查代码变更"""
prompt = f"""
审查以下代码变更,提供:
1. 潜在的 bug
2. 性能问题
3. 安全漏洞
4. 代码风格建议
代码变更:
{diff}
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.contentdef generate_docs(code: str) -> str:
"""为代码生成文档"""
prompt = f"""
为以下代码生成详细的 API 文档,包括:
1. 函数描述
2. 参数说明
3. 返回值
4. 使用示例
代码:
{code}
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.contentdef generate_tests(code: str) -> str:
"""为代码生成测试用例"""
prompt = f"""
为以下代码生成完整的测试用例,包括:
1. 正常情况测试
2. 边界情况测试
3. 错误情况测试
使用 pytest 框架。
代码:
{code}
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.contentdef nl_to_sql(question: str, schema: str) -> str:
"""将自然语言转换为 SQL"""
prompt = f"""
根据以下数据库 schema,将自然语言问题转换为 SQL 查询。
Schema:
{schema}
问题:{question}
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content| 模型 | 每 1000 输入 token | 每 1000 输出 token |
|---|---|---|
| GPT-4o | $0.005 | $0.015 |
| GPT-4o-mini | $0.00015 | $0.0006 |
| Claude 3.5 Sonnet | $0.003 | $0.015 |
| Llama 3.1 | 免费 | 免费 |
- 选择合适的模型:根据任务复杂度选择模型
- 优化提示词:清晰的提示词获得更好的结果
- 控制成本:监控 API 使用量
- 错误处理:处理 API 调用失败的情况
- 缓存结果:缓存相同的请求以减少调用
A: GitHub Models 是 API 平台,可以直接调用模型;Copilot 是集成在 IDE 中的 AI 助手。
A: 使用流式响应、批量请求、缓存结果。
A: 模型生成的内容可能不准确,建议人工审查,特别是对于关键任务。
上一篇:GitHub Advanced Security | 下一篇:Monorepo 管理