mirror of
https://github.com/mudler/cogito.git
synced 2026-07-24 02:45:53 -04:00
2f20fcce01
* Split e2e and unit tests Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Abstract away LLM to an interface Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Update README Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Fixups Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Enhance content improve generation Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Add unit test for reviewer Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Better handling of result status of tool calls Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Fixups Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Add tools test file Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package cogito
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/mudler/cogito/prompt"
|
|
"github.com/mudler/cogito/structures"
|
|
|
|
"github.com/mudler/cogito/pkg/xlog"
|
|
)
|
|
|
|
// ExtractBoolean extracts a boolean from a conversation
|
|
func ExtractBoolean(llm LLM, f Fragment, opts ...Option) (*structures.Boolean, error) {
|
|
o := defaultOptions()
|
|
o.Apply(opts...)
|
|
|
|
prompter := o.Prompts.GetPrompt(prompt.PromptBooleanType)
|
|
|
|
structure, boolean := structures.StructureBoolean()
|
|
|
|
booleanExtractor := struct {
|
|
Context string
|
|
}{
|
|
Context: f.Messages[len(f.Messages)-1].Content,
|
|
}
|
|
|
|
prompt, err := prompter.Render(booleanExtractor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to render tool reasoner prompt: %w", err)
|
|
}
|
|
|
|
booleanConv := NewEmptyFragment().AddMessage("user", prompt)
|
|
|
|
err = booleanConv.ExtractStructure(o.Context, llm, structure)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to extract boolean structure: %w", err)
|
|
}
|
|
|
|
return boolean, nil
|
|
}
|
|
|
|
func ExtractKnowledgeGaps(llm LLM, f Fragment, opts ...Option) ([]string, error) {
|
|
o := defaultOptions()
|
|
o.Apply(opts...)
|
|
|
|
prompter := o.Prompts.GetPrompt(prompt.GapAnalysisType)
|
|
|
|
renderOptions := struct {
|
|
Text string
|
|
Context string
|
|
}{
|
|
Text: f.String(),
|
|
}
|
|
|
|
if f.ParentFragment != nil {
|
|
if o.DeepContext {
|
|
renderOptions.Context = f.ParentFragment.AllFragmentsStrings()
|
|
} else {
|
|
renderOptions.Context = f.ParentFragment.String()
|
|
}
|
|
}
|
|
|
|
prompt, err := prompter.Render(renderOptions)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to render gap analysis prompt: %w", err)
|
|
}
|
|
|
|
xlog.Debug("Analyzing knowledge gaps", "prompt", prompt)
|
|
newFragment := NewEmptyFragment().AddMessage("system", prompt)
|
|
|
|
f, err = llm.Ask(o.Context, newFragment)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
xlog.Debug("LLM response for gap analysis", "response", f.String())
|
|
o.StatusCallback(f.LastMessage().Content)
|
|
|
|
structure, gaps := structures.StructureGaps()
|
|
err = f.ExtractStructure(o.Context, llm, structure)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return gaps.Gaps, nil
|
|
}
|