Skip to main content
本笔记本介绍如何使用 Jira 工具包。 Jira 工具包允许代理与给定的 Jira 实例交互,执行诸如搜索问题和创建问题等操作。该工具封装了 atlassian-python-api 库,更多信息请参见:atlassian-python-api.readthedocs.io/jira.html

安装与设置

要使用此工具,必须首先设置以下环境变量: JIRA_INSTANCE_URL, JIRA_CLOUD 您可以选择两种认证方法:
  • API 令牌认证:设置 JIRA_API_TOKEN(如果需要,还需设置 JIRA_USERNAME)环境变量
  • OAuth2.0 认证:设置 JIRA_OAUTH2 环境变量为一个字典,包含字段 “client_id” 和 “token”,其中 “token” 是一个至少包含 “access_token” 和 “token_type” 的字典
pip install -qU  atlassian-python-api
pip install -qU langchain-community langchain-openai
import os

from langchain.agents import create_agent
from langchain_community.agent_toolkits.jira.toolkit import JiraToolkit
from langchain_community.utilities.jira import JiraAPIWrapper
from langchain_openai import OpenAI
使用 API 令牌进行认证
os.environ["JIRA_API_TOKEN"] = "abc"
os.environ["JIRA_USERNAME"] = "123"
os.environ["JIRA_INSTANCE_URL"] = "https://jira.atlassian.com"
os.environ["OPENAI_API_KEY"] = "xyz"
os.environ["JIRA_CLOUD"] = "True"
使用 Oauth2.0 进行认证
os.environ["JIRA_OAUTH2"] = (
    '{"client_id": "123", "token": {"access_token": "abc", "token_type": "bearer"}}'
)
os.environ["JIRA_INSTANCE_URL"] = "https://jira.atlassian.com"
os.environ["OPENAI_API_KEY"] = "xyz"
os.environ["JIRA_CLOUD"] = "True"
llm = OpenAI(temperature=0)
jira = JiraAPIWrapper()
toolkit = JiraToolkit.from_jira_api_wrapper(jira)

工具使用

让我们看看 Jira 工具包中有哪些独立工具:
[(tool.name, tool.description) for tool in toolkit.get_tools()]
[('JQL Query',
  '\n    此工具是 atlassian-python-api 的 Jira jql API 的包装器,当您需要搜索 Jira 问题时非常有用。\n    此工具的输入是一个 JQL 查询字符串,将传递给 atlassian-python-api 的 Jira `jql` 函数。\n    例如,要查找项目 "Test" 中分配给当前用户的所有问题,您可以传入以下字符串:\n    project = Test AND assignee = currentUser()\n    或者要查找摘要中包含单词 "test" 的问题,您可以传入以下字符串:\n    summary ~ \'test\'\n    '),
 ('Get Projects',
  "\n    此工具是 atlassian-python-api 的 Jira project API 的包装器,\n    当您需要获取用户有权访问的所有项目、了解有多少项目,或作为涉及按项目搜索的中间步骤时非常有用。\n    此工具没有输入。\n    "),
 ('Create Issue',
  '\n    此工具是 atlassian-python-api 的 Jira issue_create API 的包装器,当您需要创建 Jira 问题时非常有用。\n    此工具的输入是一个指定 Jira 问题字段的字典,将传递给 atlassian-python-api 的 Jira `issue_create` 函数。\n    例如,要创建一个优先级为低、名为 "test issue" 且描述为 "test description" 的任务,您可以传入以下字典:\n    {{"summary": "test issue", "description": "test description", "issuetype": {{"name": "Task"}}, "priority": {{"name": "Low"}}}}\n    '),
 ('Catch all Jira API call',
  '\n    此工具是 atlassian-python-api 的 Jira API 的包装器。\n    还有其他专用工具用于获取所有项目、创建和搜索问题,\n    如果您需要执行 atlassian-python-api Jira API 允许的任何其他操作,请使用此工具。\n    此工具的输入是一个指定 atlassian-python-api Jira API 函数的字典,\n    以及要传递给该函数的参数列表和关键字参数字典。\n    例如,要获取组中的所有用户,同时将最大结果数增加到 100,您可以\n    传入以下字典:{{"function": "get_all_users_from_group", "args": ["group"], "kwargs": {{"limit":100}} }}\n    或者要了解 Jira 实例中有多少项目,您可以传入以下字符串:\n    {{"function": "projects"}}\n    有关 Jira API 的更多信息,请参阅 https://atlassian-python-api.readthedocs.io/jira.html\n    '),
 ('Create confluence page',
  '此工具是 atlassian-python-api 的 Confluence atlassian-python-api API 的包装器,当您需要创建 Confluence 页面时非常有用。此工具的输入是一个指定 Confluence 页面字段的字典,将传递给 atlassian-python-api 的 Confluence `create_page` 函数。例如,要在 DEMO 空间中创建一个标题为 "This is the title"、正文为 "This is the body. You can use <strong>HTML tags</strong>!" 的页面,您可以传入以下字典:{{"space": "DEMO", "title":"This is the title","body":"This is the body. You can use <strong>HTML tags</strong>!"}} ')]
agent = create_agent(
    model=llm,
    tools=toolkit.get_tools(),
    verbose=True,
)
agent.invoke(
    {
        "input": "在项目 PW 中创建一个新问题,提醒我做更多炒饭"
    }
)
> 进入新的 AgentExecutor 链...
 我需要在项目 PW 中创建一个问题
操作:创建问题
操作输入:{"summary": "做更多炒饭", "description": "提醒我做更多炒饭", "issuetype": {"name": "Task"}, "priority": {"name": "Low"}, "project": {"key": "PW"}}
观察:无
思考:我现在知道最终答案了
最终答案:已在项目 PW 中创建了一个新问题,摘要为 "做更多炒饭",描述为 "提醒我做更多炒饭"。

> 链结束。
'已在项目 PW 中创建了一个新问题,摘要为 "做更多炒饭",描述为 "提醒我做更多炒饭"。'