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

fix: add context timeout to LLM query and limit the return length to less than 10000 characters. #504

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
28 changes: 26 additions & 2 deletions internal/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/qiniu/reviewbot/internal/util"
"github.com/tmc/langchaingo/llms"
)

Expand All @@ -30,11 +32,21 @@ func New(ctx context.Context, config Config) (llms.Model, error) {
}

func Query(ctx context.Context, model llms.Model, query string, extraContext string) (string, error) {
log := util.FromContext(ctx)
ragQuery := fmt.Sprintf(ragTemplateStr, query, extraContext)
respText, err := llms.GenerateFromSinglePrompt(ctx, model, ragQuery)

timeout := 5 * time.Minute
ctxWithTimeout, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

respText, err := llms.GenerateFromSinglePrompt(ctxWithTimeout, model, ragQuery)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Warnf("LLM query operation timed out after %s", timeout)
}
return "", err
}
log.Debugf("length of LLM respText: %d", len(respText))
return respText, nil
}

Expand All @@ -60,20 +72,32 @@ Context:
`

func QueryForReference(ctx context.Context, model llms.Model, linterOutput string) (string, error) {
log := util.FromContext(ctx)
if model == nil {
return "", ErrModelIsNil
}
ragQuery := fmt.Sprintf(referenceTemplateStr, linterOutput)
respText, err := llms.GenerateFromSinglePrompt(ctx, model, ragQuery)

timeout := 5 * time.Minute
ctxWithTimeout, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

respText, err := llms.GenerateFromSinglePrompt(ctxWithTimeout, model, ragQuery)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Warnf("LLM query operation timed out after %s", timeout)
}
return "", err
}
log.Debugf("length of LLM respText: %d", len(respText))

return respText, nil
}

const referenceTemplateStr = `
You are a lint expert, you can explain in detail the meaning of the lint result according to the content of the given context.
You will follow the format of the example in <format> to answer in Chinese, firstly, you will explain the lint, secondly, you will give the incorrect usage (it can be a code example or text description), and finally, you will give the correct usage (it can be a code example or a text description), and finally add a blank line before the result.
Please make sure that the output must not exceed 10,000 characters.
<format>
Expand Down
Loading