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

refactor #24

Merged
merged 1 commit into from
May 21, 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
96 changes: 4 additions & 92 deletions cmd/description/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/google/go-github/v51/github"
"github.com/jessevdk/go-flags"
"github.com/sashabaranov/go-openai"

"github.com/ravilushqa/gpt-pullrequest-updater/description"
ghClient "github.com/ravilushqa/gpt-pullrequest-updater/github"
"github.com/ravilushqa/gpt-pullrequest-updater/jira"
oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
Expand All @@ -37,10 +37,6 @@ func main() {
os.Exit(0)
}

if opts.Test {
fmt.Println("Test mode")
}

if err := run(ctx); err != nil {
panic(err)
}
Expand All @@ -60,25 +56,9 @@ func run(ctx context.Context) error {
return fmt.Errorf("error getting commits: %w", err)
}

var sumDiffs int
for _, file := range diff.Files {
if file.Patch == nil {
continue
}
sumDiffs += len(*file.Patch)
}

var completion string
if sumDiffs < 4000 {
completion, err = genCompletionOnce(ctx, openAIClient, diff)
if err != nil {
return fmt.Errorf("error generating completition once: %w", err)
}
} else {
completion, err = genCompletionPerFile(ctx, openAIClient, diff, pr)
if err != nil {
return fmt.Errorf("error generating completition twice: %w", err)
}
completion, err := description.GenerateCompletion(ctx, openAIClient, diff, pr)
if err != nil {
return fmt.Errorf("error generating completion: %w", err)
}

if opts.JiraURL != "" {
Expand All @@ -105,71 +85,3 @@ func run(ctx context.Context) error {

return nil
}

func genCompletionOnce(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison) (string, error) {
fmt.Println("Generating completion once")
messages := make([]openai.ChatCompletionMessage, 0, len(diff.Files))
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: oAIClient.PromptDescribeChanges,
})
for _, file := range diff.Files {
if file.Patch == nil {
continue
}

messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: *file.Patch,
})
}

fmt.Println("Sending prompt to OpenAI")
completion, err := client.ChatCompletion(ctx, messages)
if err != nil {
return "", fmt.Errorf("error completing prompt: %w", err)
}

return completion, nil
}

func genCompletionPerFile(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
fmt.Println("Generating completion per file")
OverallDescribeCompletion := fmt.Sprintf("Pull request title: %s, body: %s\n\n", pr.GetTitle(), pr.GetBody())

for i, file := range diff.Files {
if file.Patch == nil {
continue
}
prompt := fmt.Sprintf(oAIClient.PromptDescribeChanges, *file.Patch)

if len(prompt) > 4096 {
prompt = fmt.Sprintf("%s...", prompt[:4093])
}

fmt.Printf("Sending prompt to OpenAI for file %d/%d\n", i+1, len(diff.Files))
completion, err := client.ChatCompletion(ctx, []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
})
if err != nil {
return "", fmt.Errorf("error getting review: %w", err)
}
OverallDescribeCompletion += fmt.Sprintf("File: %s \nDescription: %s \n\n", file.GetFilename(), completion)
}

fmt.Println("Sending final prompt to OpenAI")
overallCompletion, err := client.ChatCompletion(ctx, []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf(oAIClient.PromptOverallDescribe, OverallDescribeCompletion),
},
})
if err != nil {
return "", fmt.Errorf("error getting overall review: %w", err)
}

return overallCompletion, nil
}
104 changes: 104 additions & 0 deletions description/description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package description

import (
"context"
"fmt"

"github.com/google/go-github/v51/github"
"github.com/sashabaranov/go-openai"

oAIClient "github.com/ravilushqa/gpt-pullrequest-updater/openai"
)

func GenerateCompletion(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
sumDiffs := calculateSumDiffs(diff)

var completion string
var err error
if sumDiffs < 4000 {
completion, err = genCompletionOnce(ctx, client, diff)
} else {
completion, err = genCompletionPerFile(ctx, client, diff, pr)
}

return completion, err
}

func calculateSumDiffs(diff *github.CommitsComparison) int {
sumDiffs := 0
for _, file := range diff.Files {
if file.Patch == nil {
continue
}
sumDiffs += len(*file.Patch)
}
return sumDiffs
}

func genCompletionOnce(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison) (string, error) {
fmt.Println("Generating completion once")
messages := make([]openai.ChatCompletionMessage, 0, len(diff.Files))
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: oAIClient.PromptDescribeChanges,
})
for _, file := range diff.Files {
if file.Patch == nil {
continue
}

messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: *file.Patch,
})
}

fmt.Println("Sending prompt to OpenAI")
completion, err := client.ChatCompletion(ctx, messages)
if err != nil {
return "", fmt.Errorf("error completing prompt: %w", err)
}

return completion, nil
}

func genCompletionPerFile(ctx context.Context, client *oAIClient.Client, diff *github.CommitsComparison, pr *github.PullRequest) (string, error) {
fmt.Println("Generating completion per file")
OverallDescribeCompletion := fmt.Sprintf("Pull request title: %s, body: %s\n\n", pr.GetTitle(), pr.GetBody())

for i, file := range diff.Files {
if file.Patch == nil {
continue
}
prompt := fmt.Sprintf(oAIClient.PromptDescribeChanges, *file.Patch)

if len(prompt) > 4096 {
prompt = fmt.Sprintf("%s...", prompt[:4093])
}

fmt.Printf("Sending prompt to OpenAI for file %d/%d\n", i+1, len(diff.Files))
completion, err := client.ChatCompletion(ctx, []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
})
if err != nil {
return "", fmt.Errorf("error getting review: %w", err)
}
OverallDescribeCompletion += fmt.Sprintf("File: %s \nDescription: %s \n\n", file.GetFilename(), completion)
}

fmt.Println("Sending final prompt to OpenAI")
overallCompletion, err := client.ChatCompletion(ctx, []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf(oAIClient.PromptDescribeOverall, OverallDescribeCompletion),
},
})
if err != nil {
return "", fmt.Errorf("error completing final prompt: %w", err)
}

return overallCompletion, nil
}
9 changes: 5 additions & 4 deletions openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
//go:embed prompts/review
var PromptReview string

const (
PromptDescribeChanges = "Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix\n%s\n"
PromptOverallDescribe = "Below comments are generated by AI, Generate a GitHub pull request description based on the following comments without basic prefix in markdown format with ### Description and ### Changes blocks:\n%s\n"
)
//go:embed prompts/describe_changes
var PromptDescribeChanges string

//go:embed prompts/describe_overall
var PromptDescribeOverall string

type Client struct {
client *openai.Client
Expand Down
2 changes: 2 additions & 0 deletions openai/prompts/describe_changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix
%s
6 changes: 6 additions & 0 deletions openai/prompts/describe_overall
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Below comments are generated by AI
Generate a GitHub pull request description based on the following comments without basic prefix in markdown format.
Response should contain ### Description and ### Changes blocks

PR comments:
%s