文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

GitHubIssueCommenter

此组件使用 GitHub API 将评论发布到 GitHub issue。

pipeline 中的最常见位置在提供了要发布的评论文本的 Chat Generator 之后,或者在 pipeline 的最开始。
必需的初始化变量"github_token": GitHub 个人访问令牌。可以设置为GITHUB_TOKEN 环境变量。
强制运行变量"url": GitHub issue 的 URL

"comment": 要发布的评论文本
输出变量"success": 指示评论是否成功发布的布尔值
API 参考GitHub
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github

概述

GitHubIssueCommenter 接受一个 GitHub issue 的 URL 和评论文本,然后将评论发布到指定的 issue。

由于发布评论是需要身份验证的操作,该组件需要使用 GitHub 个人访问令牌进行身份验证。

授权

此组件需要使用个人访问令牌进行 GitHub 身份验证。您可以通过GITHUB_TOKEN 环境变量进行设置,或者在初始化时通过github_token 参数直接传递。

要创建个人访问令牌,请访问 GitHub 的令牌设置页面。请确保授予适当的仓库访问和 issue 管理权限。

安装

使用 pip 安装 GitHub 集成

pip install github-haystack

用法

📘

仓库占位符

要运行以下代码片段,您需要将owner/repo 替换为您自己的 GitHub 仓库名称。

单独使用

使用环境变量进行身份验证的基本用法

from haystack_integrations.components.connectors.github import GitHubIssueCommenter

commenter = GitHubIssueCommenter()
result = commenter.run(
    url="https://github.com/owner/repo/issues/123",
    comment="Thanks for reporting this issue! We'll look into it."
)

print(result)
{'success': True}

在 pipeline 中

以下 pipeline 分析 GitHub issue 并自动发布响应

from haystack import Pipeline
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.components.converters import OutputAdapter
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.connectors.github import GitHubIssueViewer, GitHubIssueCommenter

issue_viewer = GitHubIssueViewer()
issue_commenter = GitHubIssueCommenter()

prompt_template = [
    ChatMessage.from_system("You are a helpful assistant that analyzes GitHub issues and creates appropriate responses."),
    ChatMessage.from_user(
        "Based on the following GitHub issue:\n"
        "{% for document in documents %}"
        "{% if document.meta.type == 'issue' %}"
        "**Issue Title:** {{ document.meta.title }}\n"
        "**Issue Description:** {{ document.content }}\n"
        "{% endif %}"
        "{% endfor %}\n"
        "Generate a helpful response comment for this issue. Keep it professional and concise."
    )
]

prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*")
llm = OpenAIChatGenerator(model="gpt-4o-mini")
adapter = OutputAdapter(template="{{ replies[-1].text }}", output_type=str)

pipeline = Pipeline()
pipeline.add_component("issue_viewer", issue_viewer)
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("llm", llm)
pipeline.add_component("adapter", adapter)
pipeline.add_component("issue_commenter", issue_commenter)

pipeline.connect("issue_viewer.documents", "prompt_builder.documents")
pipeline.connect("prompt_builder.prompt", "llm.messages")
pipeline.connect("llm.replies", "adapter.replies")
pipeline.connect("adapter", "issue_commenter.comment")

issue_url = "https://github.com/owner/repo/issues/123"
result = pipeline.run(data={
    "issue_viewer": {"url": issue_url},
    "issue_commenter": {"url": issue_url}
})

print(f"Comment posted successfully: {result['issue_commenter']['success']}")
Comment posted successfully: True