mirror of
https://github.com/mudler/cogito.git
synced 2026-07-24 10:55:21 -04:00
23c7064141
* 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>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package cogito
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
type OpenAIClient struct {
|
|
model string
|
|
client *openai.Client
|
|
}
|
|
|
|
func NewOpenAILLM(model, apiKey, baseURL string) *OpenAIClient {
|
|
client := openaiClient(apiKey, baseURL)
|
|
|
|
return &OpenAIClient{
|
|
model: model,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// Ask prompts to the LLM with the provided messages
|
|
// and returns a Fragment containing the response.
|
|
// The Fragment.GetMessages() method automatically handles force-text-reply
|
|
// when tool calls are present in the conversation history.
|
|
func (llm *OpenAIClient) Ask(ctx context.Context, f Fragment) (Fragment, error) {
|
|
// Use Fragment.GetMessages() which automatically adds force-text-reply
|
|
// system message when tool calls are detected in the conversation
|
|
messages := f.GetMessages()
|
|
|
|
resp, err := llm.client.CreateChatCompletion(
|
|
ctx,
|
|
openai.ChatCompletionRequest{
|
|
Model: llm.model,
|
|
Messages: messages,
|
|
},
|
|
)
|
|
|
|
if err == nil && len(resp.Choices) > 0 {
|
|
return Fragment{
|
|
Messages: append(f.Messages, resp.Choices[0].Message),
|
|
ParentFragment: &f,
|
|
Status: &Status{},
|
|
}, nil
|
|
}
|
|
|
|
return Fragment{}, err
|
|
}
|
|
|
|
func (llm *OpenAIClient) CreateChatCompletion(ctx context.Context, request openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error) {
|
|
request.Model = llm.model
|
|
return llm.client.CreateChatCompletion(ctx, request)
|
|
}
|
|
|
|
// NewOpenAIService creates a new OpenAI service instance
|
|
func openaiClient(apiKey string, baseURL string) *openai.Client {
|
|
config := openai.DefaultConfig(apiKey)
|
|
if baseURL != "" {
|
|
config.BaseURL = baseURL
|
|
}
|
|
|
|
return openai.NewClientWithConfig(config)
|
|
}
|