Files
Ettore Di Giacinto 23c7064141 feat: enhance tool selection (#13)
* feat: enhance tool selection

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* try to adapt tests

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Make tests more stable

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Add autoplan test

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Don't load MCP prompts if we don't have any

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Reorder things

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* TEST

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Revert "TEST"

This reverts commit b08bbc56d5.

* TEST

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* TEST2

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Try to fix tests

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Add back guidelines

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* fixups

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Improve next action logic

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* tests

* tests2

* forcereasoning

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* maxiterations

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Fix tests

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Better error handling

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* test

* error handling

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Force text reply

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Two-step param generation approach

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Code cleanup

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Add a small note/comment

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Improve tests

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Small refinements

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
2025-10-26 21:37:24 +01:00

48 lines
1.3 KiB
Go

package cogito
import (
"context"
"fmt"
"time"
"github.com/mudler/cogito/pkg/xlog"
"github.com/sashabaranov/go-openai"
)
type LLM interface {
Ask(ctx context.Context, f Fragment) (Fragment, error)
CreateChatCompletion(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error)
}
// askLLMWithRetry asks the LLM with retries.
// It retries on errors and ensures a valid response
func askLLMWithRetry(ctx context.Context, llm LLM, conversation []openai.ChatCompletionMessage, maxRetries int) (openai.ChatCompletionMessage, error) {
var resp openai.ChatCompletionResponse
var err error
for attempt := 0; attempt <= maxRetries; attempt++ {
resp, err = llm.CreateChatCompletion(ctx,
openai.ChatCompletionRequest{
Messages: conversation,
},
)
if err == nil && len(resp.Choices) == 1 && resp.Choices[0].Message.Content != "" {
break
}
xlog.Warn("Error asking LLM, retrying", "attempt", attempt+1, "error", err)
if attempt < maxRetries {
time.Sleep(2 * time.Second) // Add a delay between retries
}
}
if err != nil {
return openai.ChatCompletionMessage{}, err
}
if len(resp.Choices) != 1 {
return openai.ChatCompletionMessage{}, fmt.Errorf("not enough choices: %w", err)
}
return resp.Choices[0].Message, nil
}