Skip to content

Commit 84fbaf8

Browse files
committed
fix: explanation order
1 parent 732091c commit 84fbaf8

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

llm/binding.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"net/url"
7+
"strconv"
8+
"strings"
79

810
"github.com/rs/zerolog"
911

@@ -45,7 +47,7 @@ type SnykLLMBindings interface {
4547
// output - a channel that can be used to stream the results
4648
Explain(ctx context.Context, input AIRequest, format OutputFormat, output chan<- string) error
4749
}
48-
type ExplainResult map[string]string
50+
type ExplainResult map[int]string
4951

5052
type DeepCodeLLMBinding interface {
5153
SnykLLMBindings
@@ -64,17 +66,21 @@ type DeepCodeLLMBindingImpl struct {
6466
func (d *DeepCodeLLMBindingImpl) ExplainWithOptions(ctx context.Context, options ExplainOptions) (ExplainResult, error) {
6567
s := d.instrumentor.StartSpan(ctx, "code.ExplainWithOptions")
6668
defer d.instrumentor.Finish(s)
69+
logger := d.logger.With().Str("method", "code.ExplainWithOptions").Logger()
6770
response, err := d.runExplain(s.Context(), options)
6871
explainResult := ExplainResult{}
6972
if err != nil {
7073
return explainResult, err
7174
}
72-
index := 0
73-
for _, explanation := range response {
74-
if index < len(options.Diffs) {
75-
explainResult[options.Diffs[index]] = explanation
75+
for k, v := range response {
76+
// response key is "explanation1", "explanation2", etc
77+
explanationNumber, parseErr := strconv.Atoi(strings.ReplaceAll(strings.ToLower(k), "explanation", ""))
78+
if parseErr != nil || (explanationNumber-1) < 0 {
79+
logger.Err(parseErr).Msgf("error parsing explanationNumber%s", k)
80+
return explainResult, parseErr
7681
}
77-
index++
82+
index := explanationNumber - 1
83+
explainResult[index] = v
7884
}
7985

8086
return explainResult, nil

0 commit comments

Comments
 (0)