📘 **TELUS Agriculture & Consumer Goods** 如何通过 **Haystack Agents** 转变促销交易
由 deepset 维护

集成:GitHub

在 Haystack 中与 GitHub 存储库、问题和拉取请求进行交互

作者
deepset

目录

概述

Haystack 的 GitHub 集成提供了一组组件和工具,用于与 GitHub 存储库、问题和拉取请求进行交互。它使您能够在 Haystack 代理和管道中查看存储库内容、管理问题、创建拉取请求等。

此集成中的某些组件和工具需要使用个人访问令牌进行 GitHub 身份验证。例如,发布评论、分叉存储库或创建拉取请求需要进行身份验证。您可以在 GitHub 上创建一个(精细的个人访问令牌)[https://github.com/settings/personal-access-tokens] 或一个经典个人访问令牌,然后通过名为 GITHUB_API_KEY 的环境变量公开它。

安装

使用 pip 安装 GitHub 集成

pip install github-haystack

使用

此集成包含多个组件和工具

组件

  • GitHubIssueViewer:查看问题及其详细信息
  • GitHubIssueCommenter:向问题添加评论
  • GitHubRepoViewer:查看存储库内容和元数据
  • GitHubRepoForker:分叉存储库
  • GitHubFileEditor:编辑存储库中的文件
  • GitHubPRCreator:创建拉取请求

工具

  • GitHubIssueViewerTool:查看问题
  • GitHubIssueCommenterTool:评论问题
  • GitHubRepoViewerTool:查看存储库内容
  • GitHubFileEditorTool:编辑存储库文件
  • GitHubPRCreatorTool:创建拉取请求

示例用法

from typing import List

from haystack import Pipeline
from haystack.components.agents import Agent
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage, Document
from haystack.tools.from_function import tool

from haystack_integrations.components.connectors.github import GitHubIssueViewer
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack_integrations.prompts.github import SYSTEM_PROMPT
from haystack_integrations.tools.github import GitHubRepoViewerTool

@tool
def create_comment(comment: str) -> str:
    """
    Use this to create a Github comment once you finished your exploration.
    """
    # A mockup tool to showcase how Agent uses tools. You should use `GitHubIssueCommenterTool` instead of this one to write comments on GitHub.
    return comment

repo_viewer_tool = GitHubRepoViewerTool()

chat_generator = AnthropicChatGenerator(model="claude-3-5-sonnet-latest", generation_kwargs={"max_tokens": 8000})

agent = Agent(
    chat_generator=chat_generator,
    system_prompt=SYSTEM_PROMPT,
    tools=[repo_viewer_tool, create_comment],
    exit_conditions=["create_comment"],
    state_schema={"documents": {"type": List[Document]}},
)

issue_template = """
Issue from: {{ url }}
{% for document in documents %}
{% if loop.index == 1 %}
**Title: {{ document.meta.title }}**
{% endif %}
<issue-comment>
{{document.content}}
</issue-comment>
{% endfor %}
"""

issue_builder = ChatPromptBuilder(template=[ChatMessage.from_user(issue_template)], required_variables="*")

issue_fetcher = GitHubIssueViewer()

pipeline = Pipeline()

pipeline.add_component("issue_fetcher", issue_fetcher)
pipeline.add_component("issue_builder", issue_builder)
pipeline.add_component("agent", agent)

pipeline.connect("issue_fetcher.documents", "issue_builder.documents")
pipeline.connect("issue_builder.prompt", "agent.messages")

issue_url = "https://github.com/<owner>/<repo>/issues/1268"

result = pipeline.run({"url": issue_url})
print(result["agent"]["last_message"].tool_call_result.result)