Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve the prompt #23

Merged
merged 10 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ jobs:
- name: Test Local Action
id: test-action
uses: ./
with:
gitHubToken: ${{ secrets.GIT_HUB_TOKEN }}
openAIKey: ${{ secrets.OPENAI_API_KEY }}

- name: Print Output
id: output
Expand Down
11 changes: 10 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
name: "Ai PR summarizer"
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"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function run(): Promise<void> {
const summary = await summarizeChanges(changes)

if (!summary) {
Logger.warn('Could not summarize changes, exiting')
Logger.warn('Summary is empty, exiting')
return
}

Expand Down
32 changes: 3 additions & 29 deletions src/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
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.

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.
-----------------------------
{diff}
`

export const generatePrompt = () => {}
export const generatePrompt = () => { }
12 changes: 7 additions & 5 deletions src/steps/get-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function getChanges(
): Promise<string | undefined> {
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

Expand All @@ -22,12 +22,14 @@ 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')
Logger.error('error getting changes', JSON.stringify(error))
}
}
2 changes: 1 addition & 1 deletion src/steps/post-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 31 additions & 13 deletions src/steps/summarize-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,57 @@ 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<string | undefined> {
try {
Logger.log('summarizing changes')
const model = new OpenAI({ temperature: 0 })
Logger.log('created model')

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" },
)

const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
separators: ['diff --git'],
chunkOverlap: 0,
keepSeparator: true
keepSeparator: true,
chunkSize: 5000
})

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')
Logger.error(JSON.stringify(e as unknown as string))
console.log(e)
Logger.log(e)
}
}
Loading