From ec43b575c6d7083634d8e0ff7517cfde39a904d1 Mon Sep 17 00:00:00 2001 From: Pascal Aznar Date: Thu, 5 Sep 2024 01:05:08 +0200 Subject: [PATCH] add groq api to issues --- .github/workflows/issue_helper.yml | 111 ++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/.github/workflows/issue_helper.yml b/.github/workflows/issue_helper.yml index 0b726cf9..700ab8c5 100644 --- a/.github/workflows/issue_helper.yml +++ b/.github/workflows/issue_helper.yml @@ -1,8 +1,10 @@ -name: Issue Helper +name: Issue Management on: issues: types: [opened, edited] + issue_comment: + types: [created] jobs: check_issue_template: @@ -40,3 +42,110 @@ jobs: state: 'closed' }); } + + auto_reply: + runs-on: ubuntu-latest + needs: check_issue_template + steps: + - name: Generate Response and Post Comment + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const issue = context.payload.issue; + const comment = context.payload.comment; + const repo = context.repo.repo; + const issueNumber = issue.number; + const issueBody = issue.body; + + const groqApiKey = process.env.GROQ_API_KEY; + const issueAuthor = issue.user.login; + const commentAuthor = comment ? comment.user.login : null; + + const systemPrompt = `You are an assistant for a GitHub repository called Applio (https://github.com/IAHispano/Applio), a Python project focused on voice cloning. Your job is to assist users with any issues or bugs they report, providing clear, helpful and short guidance. You can troubleshoot various technical problems and offer solutions, code snippets, or documentation references as needed. Be concise, efficient, and to the point when responding to the following GitHub issue. Try to answer in a paragraph whenever possible and if you are not sure of the answer ask the user for more details. If relevant, refer users to the official documentation at https://docs.applio.org, just share that link, do not add any extension to it. Issue content: ${issueBody}`; + + async function getAIResponse(prompt) { + const response = await fetch('https://api.groq.com/openai/v1/chat/completions', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${groqApiKey}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + messages: [ + { role: 'system', content: prompt }, + { role: 'user', content: issueBody }, + ], + model: 'llama-3.1-70b-versatile', + temperature: 0.6, + }), + }); + + if (!response.ok) { + throw new Error('Failed to fetch response from LLM'); + } + + const data = await response.json(); + return data.choices[0].message.content; + } + + const comments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: repo, + issue_number: issueNumber, + }); + + const discordRecommendation = 'consider joining our support community on [Discord](https://discord.gg/iahispano)'; + const hasDiscordRecommendation = comments.data.some(comment => comment.body.includes(discordRecommendation)); + + if (hasDiscordRecommendation) { + console.log("Discord recommendation already posted. No further responses."); + return; + } + + if (comment) { + if (commentAuthor === issueAuthor) { + const followUpPrompt = `The user has sent another comment. Try to get him to provide more information about his error and try to help him as much as possible precisely and concisely, whenever possible in a paragraph.`; + const aiResponse = await getAIResponse(followUpPrompt); + + const commentMessage = `${aiResponse}\n\nIf you're looking for faster assistance, consider joining our support community on [Discord](https://discord.gg/iahispano), or please wait for a staff member to assist.`; + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: repo, + issue_number: issueNumber, + body: commentMessage, + }); + } + } else { + const initialResponse = await getAIResponse(systemPrompt); + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: repo, + issue_number: issueNumber, + body: initialResponse, + }); + + const followUpMessage = ` + If this issue persists or you need further assistance, please visit our support community at [discord.gg/iahispano](https://discord.gg/iahispano). + Our community is available to help with any additional questions or concerns. + `; + + const hasFollowUpComment = comments.data.some(comment => + comment.body.includes('If this issue persists or you need further assistance') + ); + + if (!hasFollowUpComment) { + setTimeout(async () => { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: repo, + issue_number: issueNumber, + body: followUpMessage, + }); + }, 72 * 60 * 60 * 1000); + } + } + env: + GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}