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

GitHubIssueViewer

此组件会抓取并解析 GitHub issue 到 Haystack 文档中。

pipeline 中的最常见位置在 pipeline 的最开始,并放在一个需要 GitHub issue 内容作为输入的 ChatPromptBuilder 之前
强制运行变量"url": 一个 GitHub issue 的 URL
输出变量"documents": 一个文档列表,包含主 issue 及其评论
API 参考GitHub
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github

概述

GitHubIssueViewer 接受一个 GitHub issue 的 URL,并返回一个文档列表,其中

  • 第一个文档包含主 issue 的内容
  • 随后的文档包含 issue 的评论(如果存在)

每个文档都包含丰富的元数据,例如 issue 的标题、编号、状态、创建日期、作者等。

授权

对于公开仓库,此组件无需认证即可工作;但对于私有仓库或为避免速率限制,您可以提供一个 GitHub 个人访问令牌。

您可以使用GITHUB_API_KEY 环境变量来设置令牌,或者在初始化期间通过github_token 参数直接传递。

要创建个人访问令牌,请访问 GitHub 的令牌设置页面

安装

使用 pip 安装 GitHub 集成

pip install github-haystack

用法

📘

仓库占位符

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

单独使用

基本用法,无需认证

from haystack_integrations.components.connectors.github import GitHubIssueViewer

viewer = GitHubIssueViewer()
result = viewer.run(url="https://github.com/deepset-ai/haystack/issues/123")

print(result)
{'documents': [Document(id=3989459bbd8c2a8420a9ba7f3cd3cf79bb41d78bd0738882e57d509e1293c67a, content: 'sentence-transformers = 0.2.6.1
haystack = latest
farm = 0.4.3 latest branch

In the call to Emb...', meta: {'type': 'issue', 'title': 'SentenceTransformer no longer accepts \'gpu" as argument', 'number': 123, 'state': 'closed', 'created_at': '2020-05-28T04:49:31Z', 'updated_at': '2020-05-28T07:11:43Z', 'author': 'predoctech', 'url': 'https://github.com/deepset-ai/haystack/issues/123'}), Document(id=a8a56b9ad119244678804d5873b13da0784587773d8f839e07f644c4d02c167a, content: 'Thanks for reporting!
Fixed with #124 ', meta: {'type': 'comment', 'issue_number': 123, 'created_at': '2020-05-28T07:11:42Z', 'updated_at': '2020-05-28T07:11:42Z', 'author': 'tholor', 'url': 'https://github.com/deepset-ai/haystack/issues/123#issuecomment-635153940'})]}

在 pipeline 中

以下 pipeline 会抓取一个 GitHub issue,提取相关信息,并生成一个摘要

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

# Initialize components
issue_viewer = GitHubIssueViewer()

prompt_template = [
    ChatMessage.from_system("You are a helpful assistant that analyzes GitHub issues."),
    ChatMessage.from_user(
        "Based on the following GitHub issue and comments:\n"
        "{% for document in documents %}"
        "{% if document.meta.type == 'issue' %}"
        "**Issue Title:** {{ document.meta.title }}\n"
        "**Issue Description:** {{ document.content }}\n"
        "{% else %}"
        "**Comment by {{ document.meta.author }}:** {{ document.content }}\n"
        "{% endif %}"
        "{% endfor %}\n"
        "Please provide a summary of the issue and suggest potential solutions."
    )
]

prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*")
llm = OpenAIChatGenerator(model="gpt-4o-mini")

# Create pipeline
pipeline = Pipeline()
pipeline.add_component("issue_viewer", issue_viewer)
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("llm", llm)

# Connect components
pipeline.connect("issue_viewer.documents", "prompt_builder.documents")
pipeline.connect("prompt_builder.prompt", "llm.messages")

# Run pipeline
issue_url = "https://github.com/deepset-ai/haystack/issues/123"
result = pipeline.run(data={"issue_viewer": {"url": issue_url}})

print(result["llm"]["replies"][0])