From 02cff62df6df424b877ef13a36b35fa1935a3ae6 Mon Sep 17 00:00:00 2001 From: Shuyang Xin Date: Mon, 4 Dec 2023 14:18:22 +0800 Subject: [PATCH] Add issue action Signed-off-by: Shuyang Xin --- .github/workflows/issue_answer.yml | 38 +++++++++++++++++++++++ .github/workflows/issue_label.yml | 39 +++++++++++++++++++++++ ai/issue_answer.py | 50 ++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 .github/workflows/issue_answer.yml create mode 100644 .github/workflows/issue_label.yml create mode 100644 ai/issue_answer.py diff --git a/.github/workflows/issue_answer.yml b/.github/workflows/issue_answer.yml new file mode 100644 index 00000000000..195929ea2c6 --- /dev/null +++ b/.github/workflows/issue_answer.yml @@ -0,0 +1,38 @@ +name: LangChain Issue Processor + +on: + issues: + types: [opened, reopened] + +jobs: + process-issue: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + pip install langchain openai + - name: Run process.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + run: | + SCRIPT_OUTPUT=$(python ai/issue_answer.py --github-token $GITHUB_TOKEN --openai-token $OPENAI_API_KEY) + echo "$SCRIPT_OUTPUT" + echo "SCRIPT_OUTPUT=$SCRIPT_OUTPUT" >> $GITHUB_ENV + echo "${{env.SCRIPT_OUTPUT}}" + - name: comment result + uses: actions/github-script@v5 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `${{env.SCRIPT_OUTPUT}}`, + }); \ No newline at end of file diff --git a/.github/workflows/issue_label.yml b/.github/workflows/issue_label.yml new file mode 100644 index 00000000000..6bc784235a9 --- /dev/null +++ b/.github/workflows/issue_label.yml @@ -0,0 +1,39 @@ +name: LangChain Label Processor + +on: + issues: + types: [opened, reopened] + +jobs: + process-issue: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + pip install langchain openai + - name: Run process.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + run: | + LABEL_OUTPUT=bug,document + echo "$LABEL_OUTPUT" + echo "LABEL_OUTPUT=$LABEL_OUTPUT" >> $GITHUB_ENV + echo "${{env.LABEL_OUTPUT}}" + - name: comment result + uses: actions/github-script@v5 + with: + script: | + const labelsToAdd = ['${{ env.LABEL_OUTPUT }}'.split(',')]; + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: labelsToAdd, + }); \ No newline at end of file diff --git a/ai/issue_answer.py b/ai/issue_answer.py new file mode 100644 index 00000000000..33e6e7d6032 --- /dev/null +++ b/ai/issue_answer.py @@ -0,0 +1,50 @@ +from langchain.document_loaders.github import GitHubIssuesLoader +from langchain.chat_models import ChatOpenAI +from langchain.prompts import ChatPromptTemplate +from langchain.schema.output_parser import StrOutputParser +from langchain.schema.runnable import RunnableParallel, RunnablePassthrough + +import argparse + +def parse_arguments(): + parser = argparse.ArgumentParser(description="Your program description.") + parser.add_argument("--github-token", required=True, help="GitHub access token") + parser.add_argument("--openai-token", required=True, help="OpenAI API key") + return parser.parse_args() + +# Parse command line arguments +args = parse_arguments() + +# Create an instance of the GitHubIssuesLoader class +loader = GitHubIssuesLoader( + repo="XinShuYang/antrea", + owner="XinShuYang", + access_token=args.github_token, + include_prs=False, +) + +# Load the issues of the repository +issues = loader.load() + +issue_data = [] +# Print the title and body of each issue +for issue in issues: + if issue.metadata["number"] == 9 : + issue_data.append({'title': issue.metadata['title'], 'body': issue.page_content}) + +template = """Answer the question based only on the following context: + {context} + + """ +prompt = ChatPromptTemplate.from_template(template) +model = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=args.openai_token) +output_parser = StrOutputParser() +setup_and_retrieval = RunnableParallel( + {"context": RunnablePassthrough()} +) + +chain = setup_and_retrieval | prompt | model | output_parser + +result=chain.invoke(issue_data) + +print(result) \ No newline at end of file