Skip to content

Commit

Permalink
trim ai summary when it puts it in a code block (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus authored Apr 13, 2024
1 parent b8d2dcb commit a556dce
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/aisummary/diff_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var tracer = otel.Tracer("pkg/aisummary")

// SummarizeDiff uses ChatGPT to summarize changes to a Kubernetes application.
func (c *OpenAiClient) SummarizeDiff(ctx context.Context, appName string, manifests []string, diff string) (string, error) {
func (c *OpenAiClient) SummarizeDiff(ctx context.Context, appName, diff string) (string, error) {
ctx, span := tracer.Start(ctx, "SummarizeDiff")
defer span.End()

Expand Down
27 changes: 22 additions & 5 deletions pkg/checks/diff/ai_summary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package diff

import (
"strings"

"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel"
"golang.org/x/net/context"
Expand All @@ -14,7 +16,7 @@ import (

var tracer = otel.Tracer("pkg/checks/diff")

func aiDiffSummary(ctx context.Context, mrNote *msg.Message, cfg config.ServerConfig, name string, manifests []string, diff string) {
func aiDiffSummary(ctx context.Context, mrNote *msg.Message, cfg config.ServerConfig, name, diff string) {
ctx, span := tracer.Start(ctx, "aiDiffSummary")
defer span.End()

Expand All @@ -23,7 +25,8 @@ func aiDiffSummary(ctx context.Context, mrNote *msg.Message, cfg config.ServerCo
return
}

aiSummary, err := aisummary.GetOpenAiClient(cfg.OpenAIAPIToken).SummarizeDiff(ctx, name, manifests, diff)
aiClient := aisummary.GetOpenAiClient(cfg.OpenAIAPIToken)
aiSummary, err := aiClient.SummarizeDiff(ctx, name, diff)
if err != nil {
telemetry.SetError(span, err, "OpenAI SummarizeDiff")
log.Error().Err(err).Msg("failed to summarize diff")
Expand All @@ -32,8 +35,22 @@ func aiDiffSummary(ctx context.Context, mrNote *msg.Message, cfg config.ServerCo
return
}

if aiSummary != "" {
cr := msg.Result{State: pkg.StateNone, Summary: "<b>Show AI Summary Diff</b>", Details: aiSummary}
mrNote.AddToAppMessage(ctx, name, cr)
aiSummary = cleanUpAiSummary(aiSummary)
if aiSummary == "" {
return
}

cr := msg.Result{State: pkg.StateNone, Summary: "<b>Show AI Summary Diff</b>", Details: aiSummary}
mrNote.AddToAppMessage(ctx, name, cr)
}

func cleanUpAiSummary(aiSummary string) string {
aiSummary = strings.TrimSpace(aiSummary)

// occasionally the model thinks it should wrap it in a code block.
// comments do not need this, as they are already rendered as markdown.
aiSummary = strings.TrimPrefix(aiSummary, "```markdown")
aiSummary = strings.TrimSuffix(aiSummary, "```")

return strings.TrimSpace(aiSummary)
}
33 changes: 33 additions & 0 deletions pkg/checks/diff/ai_summary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package diff

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCleanUpAiSummary(t *testing.T) {
t.Run("prefix", func(t *testing.T) {
input := "```markdown\nhello\nworld"
expected := "hello\nworld"

actual := cleanUpAiSummary(input)
assert.Equal(t, expected, actual)
})

t.Run("suffix", func(t *testing.T) {
input := "\nhello\nworld```"
expected := "hello\nworld"

actual := cleanUpAiSummary(input)
assert.Equal(t, expected, actual)
})

t.Run("prefix and suffix", func(t *testing.T) {
input := "```markdown\n\nhello\nworld```"
expected := "hello\nworld"

actual := cleanUpAiSummary(input)
assert.Equal(t, expected, actual)
})
}
2 changes: 1 addition & 1 deletion pkg/checks/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func Check(ctx context.Context, request checks.Request) (msg.Result, error) {

cr.Details = fmt.Sprintf("```diff\n%s\n```", renderedDiff)

aiDiffSummary(ctx, request.Note, request.Container.Config, request.AppName, request.JsonManifests, renderedDiff)
aiDiffSummary(ctx, request.Note, request.Container.Config, request.AppName, renderedDiff)

return cr, nil
}
Expand Down

0 comments on commit a556dce

Please sign in to comment.