From 99564aca0e560936ffe27f9cb6c6992210d83fc3 Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:42:44 +0100 Subject: [PATCH 01/10] improve the prompt --- action.yml | 2 +- src/prompts.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 8e08b17..dcfa071 100644 --- a/action.yml +++ b/action.yml @@ -1,4 +1,4 @@ -name: "Ai PR summarizer" +name: "pull request summarizer" description: "Ai enabled summarizer for PRs" author: "bemijonathan" branding: diff --git a/src/prompts.ts b/src/prompts.ts index 07a072f..3d2fbd9 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -32,6 +32,11 @@ e.g. ## Testing Scope - Scenarios to be tested based on the changes of this PR e.g table was updated so all CRUD operations should be tested. + + ## Notable Changes + + - if the new changes has a significant change in the logic please add a short table of the old and new logic and ask + if the new logic is correct or not. ` -export const generatePrompt = () => {} +export const generatePrompt = () => { } From bc0fbbdd069180c35698296d0d4742aaf7b84601 Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:05:20 +0100 Subject: [PATCH 02/10] added env variables for test ci --- .github/workflows/ci.yml | 3 +++ action.yml | 9 +++++++++ src/steps/get-changes.ts | 4 ++-- src/steps/summarize-changes.ts | 7 ++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8e5893..d56b1b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,6 +59,9 @@ jobs: - name: Test Local Action id: test-action uses: ./ + with: + gitHubToken: ${{ secrets.GIT_HUB_TOKEN }} + openAIKey: ${{ secrets.OPENAI_KEY }} - name: Print Output id: output diff --git a/action.yml b/action.yml index dcfa071..c7e27ab 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,15 @@ name: "pull request summarizer" description: "Ai enabled summarizer for PRs" author: "bemijonathan" + +inputs: + gitHubToken: + description: "GitHub token" + required: true + openAIKey: + description: "OpenAI key" + required: true + branding: color: "blue" icon: "align-left" diff --git a/src/steps/get-changes.ts b/src/steps/get-changes.ts index 5d0a39e..c228f0c 100644 --- a/src/steps/get-changes.ts +++ b/src/steps/get-changes.ts @@ -8,7 +8,7 @@ export async function getChanges( ): Promise { try { Logger.log('getting changes', pullRequestNumber) - const githubToken = core.getInput('token') + const githubToken = core.getInput('gitHubToken') const octokit = github.getOctokit(githubToken) const repo = github.context.repo @@ -28,6 +28,6 @@ export async function getChanges( return response.data } catch (error) { - Logger.error('error getting changes') + Logger.error('error getting changes', JSON.stringify(error)) } } diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index 73bc36e..c10756b 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -4,13 +4,18 @@ const { RecursiveCharacterTextSplitter } = require('langchain/text_splitter') const { PromptTemplate } = require('langchain/prompts') import { prompt } from 'src/prompts.js' import { Logger } from 'src/utils.js' +import * as core from '@actions/core' export async function summarizeChanges( diff: string ): Promise { try { Logger.log('summarizing changes') - const model = new OpenAI({ temperature: 0 }) + const openAiKey = core.getInput('openAIKey') + const model = new OpenAI( + { temperature: 0 }, + { apiKey: openAiKey } + ) Logger.log('created model') const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000, From 9e4b9175afb1d91a3cea983e32af992ba7ecacf1 Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:09:52 +0100 Subject: [PATCH 03/10] added diff --- src/steps/get-changes.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/steps/get-changes.ts b/src/steps/get-changes.ts index c228f0c..e3e1085 100644 --- a/src/steps/get-changes.ts +++ b/src/steps/get-changes.ts @@ -22,11 +22,13 @@ export async function getChanges( Logger.log('got changes diff', files) - const response = await axios.get(files.diff_url) + // const response = await axios.get(files.diff_url) - Logger.log('diff', response.data) + // Logger.log('diff', response.data) - return response.data + return files as unknown as string + + // return response.data } catch (error) { Logger.error('error getting changes', JSON.stringify(error)) } From 846022aca3632aa1f96d7c6255bb67eeb04092b0 Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:15:37 +0100 Subject: [PATCH 04/10] added diff --- src/steps/summarize-changes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index c10756b..7a9a650 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -42,6 +42,6 @@ export async function summarizeChanges( return res.output.join('\n') } catch (e) { Logger.log('error summarizing changes') - Logger.error(JSON.stringify(e as unknown as string)) + Logger.log(JSON.stringify(e as unknown as string)) } } From 14ddcf7d6ed2e326209118c29465ff529b2c90ed Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:24:51 +0100 Subject: [PATCH 05/10] added diff --- src/steps/summarize-changes.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index 7a9a650..39c29e3 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -13,8 +13,7 @@ export async function summarizeChanges( Logger.log('summarizing changes') const openAiKey = core.getInput('openAIKey') const model = new OpenAI( - { temperature: 0 }, - { apiKey: openAiKey } + { temperature: 0, openAIApiKey: openAiKey }, ) Logger.log('created model') const textSplitter = new RecursiveCharacterTextSplitter({ From c1236d622ac8bae263825dfa899c31432c5483bb Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Sat, 7 Oct 2023 07:36:42 +0100 Subject: [PATCH 06/10] added diff --- src/steps/summarize-changes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index 39c29e3..42d94f6 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -41,6 +41,6 @@ export async function summarizeChanges( return res.output.join('\n') } catch (e) { Logger.log('error summarizing changes') - Logger.log(JSON.stringify(e as unknown as string)) + Logger.log(e) } } From ce37b9d69a9e87b872f3d1c81f574938b21b8770 Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:00:56 +0100 Subject: [PATCH 07/10] fix summary --- src/index.ts | 2 +- src/prompts.ts | 29 ++--------------------------- src/steps/summarize-changes.ts | 31 +++++++++++++++++++++---------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/index.ts b/src/index.ts index 40aa32c..b876eb9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,7 +34,7 @@ export async function run(): Promise { const summary = await summarizeChanges(changes) if (!summary) { - Logger.warn('Could not summarize changes, exiting') + Logger.warn('Summary is empty, exiting') return } diff --git a/src/prompts.ts b/src/prompts.ts index 3d2fbd9..eaab260 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -10,33 +10,8 @@ Be specific and descriptive, accurately identifying the affected files and lines Present your summary in a clear and concise manner, ensuring readability and comprehension for all stakeholders involved in the code review process. Length: Aim for a summary of around 3-5 sentences. - -Style: Maintain a professional and objective tone in your review, emphasizing the technical aspects and impact of the changes rather than personal opinions. - -Formatting: Use Markdown to format your summary. -e.g. - ## Title - - # Issue Reference - - - This PR fixes/closes/relates to issue #[issue number] - - ## Description - - - Detailed explanation of what this PR does and why it's needed. - - ## Why? - - - Explanation of the reasoning behind the changes. - - ## Testing Scope - - - Scenarios to be tested based on the changes of this PR e.g table was updated so all CRUD operations should be tested. - - ## Notable Changes - - - if the new changes has a significant change in the logic please add a short table of the old and new logic and ask - if the new logic is correct or not. +----------------------------- +{diff} ` export const generatePrompt = () => { } diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index 42d94f6..c28747d 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -10,37 +10,48 @@ export async function summarizeChanges( diff: string ): Promise { try { - Logger.log('summarizing changes') + const openAiKey = core.getInput('openAIKey') + const model = new OpenAI( - { temperature: 0, openAIApiKey: openAiKey }, + { temperature: 0.7, openAIApiKey: openAiKey, "model": "davinci" }, ) - Logger.log('created model') + const textSplitter = new RecursiveCharacterTextSplitter({ - chunkSize: 1000, separators: ['diff --git'], chunkOverlap: 0, keepSeparator: true }) + Logger.log('created text splitter') + const docs = await textSplitter.createDocuments([diff]) - const basePromptTemplate = PromptTemplate.fromTemplate(prompt) + + const basePromptTemplate = new PromptTemplate({ + template: prompt, + inputVariables: ["diff"] + }) + Logger.log('created prompt template') const chain = loadSummarizationChain(model, { - prompt: basePromptTemplate, + type: "refine", verbose: true, - type: 'stuff' + refinePrompt: basePromptTemplate }) + Logger.log('loaded summarization chain') + const res = await chain.call({ - input_documents: docs + input_documents: docs, + diff: diff }) + Logger.log('summarized changes') console.log({ res }) - - return res.output.join('\n') + return res.output_text } catch (e) { Logger.log('error summarizing changes') + console.log(e) Logger.log(e) } } From d5b1232e462033cdd5bcd4cc046f6674a3fca222 Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:02:26 +0100 Subject: [PATCH 08/10] added check for open ai key --- src/steps/summarize-changes.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index c28747d..e144385 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -13,6 +13,8 @@ export async function summarizeChanges( const openAiKey = core.getInput('openAIKey') + Logger.log('creating openai model', openAiKey.length ? 'with key' : 'without key') + const model = new OpenAI( { temperature: 0.7, openAIApiKey: openAiKey, "model": "davinci" }, ) From 5f9ee3d0e94ebb1c9f0dd1dcd7bc2d803133008c Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:07:49 +0100 Subject: [PATCH 09/10] added check for open ai key --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d56b1b8..e2fcaa0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,7 +61,7 @@ jobs: uses: ./ with: gitHubToken: ${{ secrets.GIT_HUB_TOKEN }} - openAIKey: ${{ secrets.OPENAI_KEY }} + openAIKey: ${{ secrets.OPENAI_API_KEY }} - name: Print Output id: output From 7f0ebe3ce9fa323ab7eebd5ff1f35466fc24de3e Mon Sep 17 00:00:00 2001 From: Jonathan Atiene <34762800+bemijonathan@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:16:09 +0100 Subject: [PATCH 10/10] fix sumarisation concept --- src/prompts.ts | 6 ------ src/steps/post-comment.ts | 2 +- src/steps/summarize-changes.ts | 5 +++-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/prompts.ts b/src/prompts.ts index eaab260..08e5ab4 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -1,15 +1,9 @@ export const prompt = ` As an expert PR reviewer with extensive knowledge of version control systems, provide a concise summary of the review for the git diff. - ### Instructions - Your summary should include an analysis of the changes made in the git diff, highlighting significant modifications, additions, and deletions. - Be specific and descriptive, accurately identifying the affected files and lines of code. - Present your summary in a clear and concise manner, ensuring readability and comprehension for all stakeholders involved in the code review process. - -Length: Aim for a summary of around 3-5 sentences. ----------------------------- {diff} ` diff --git a/src/steps/post-comment.ts b/src/steps/post-comment.ts index 6ee5b14..a78c37a 100644 --- a/src/steps/post-comment.ts +++ b/src/steps/post-comment.ts @@ -3,7 +3,7 @@ import * as github from '@actions/github' import { Logger } from 'src/utils.js' export async function postComment(pullRequestNumber: number, summary: string) { - const githubToken = core.getInput('token') + const githubToken = core.getInput('gitHubToken') const octokit = github.getOctokit(githubToken) const repo = github.context.repo Logger.log('posted comment', github.context) diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index e144385..b054292 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -20,9 +20,9 @@ export async function summarizeChanges( ) const textSplitter = new RecursiveCharacterTextSplitter({ - separators: ['diff --git'], chunkOverlap: 0, - keepSeparator: true + keepSeparator: true, + chunkSize: 5000 }) Logger.log('created text splitter') @@ -35,6 +35,7 @@ export async function summarizeChanges( }) Logger.log('created prompt template') + const chain = loadSummarizationChain(model, { type: "refine", verbose: true,