Consolidated a bunch of reused code to agents
This commit is contained in:
@@ -7,18 +7,11 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/answer/pkg/agents/shared"
|
||||
|
||||
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
|
||||
)
|
||||
|
||||
type KnowledgeProcessor struct {
|
||||
Model gollm.ChatCompletion
|
||||
ContextualInformation []string
|
||||
}
|
||||
|
||||
// Process takes a knowledge object and processes it into a response string.
|
||||
func (a KnowledgeProcessor) Process(ctx context.Context, knowledge shared.Knowledge) (string, error) {
|
||||
// AnswerQuestionWithKnowledge will take a knowledge object and use the gained knowledge to answer a question.
|
||||
func (a Agent) AnswerQuestionWithKnowledge(ctx context.Context, knowledge Knowledge) (string, error) {
|
||||
originalQuestions := strings.Join(knowledge.OriginalQuestions, "\n")
|
||||
infoGained := ""
|
||||
|
||||
@@ -60,25 +53,12 @@ Here is the knowledge I have gathered from ` + fmt.Sprint(len(sources)) + ` sour
|
||||
|
||||
systemPrompt += "\n\nUsing the sources, write an answer to the original question. Note any information that wasn't able to be answered."
|
||||
|
||||
req := gollm.Request{
|
||||
Messages: []gollm.Message{
|
||||
{
|
||||
Role: gollm.RoleSystem,
|
||||
Text: systemPrompt,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if len(a.ContextualInformation) > 0 {
|
||||
req.Messages = append(req.Messages, gollm.Message{
|
||||
Role: gollm.RoleSystem,
|
||||
Text: "Some contextual information you should be aware of: " + strings.Join(a.ContextualInformation, "\n"),
|
||||
})
|
||||
}
|
||||
|
||||
resp, err := a.Model.ChatComplete(ctx, req)
|
||||
res, err := a.WithSystemPrompt(systemPrompt).
|
||||
WithSystemPromptSuffix(``).
|
||||
WithToolbox(nil).
|
||||
CallAndExecute(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to chat complete: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
systemPrompt = `I am trying to source an analysis of information I have gathered.
|
||||
@@ -118,27 +98,15 @@ The moon is 4.5 billion years old [1,2], 238,855 miles away from the Earth [3],
|
||||
}
|
||||
|
||||
summarizedData := `Here is the I need you to source with citations:
|
||||
` + resp.Choices[0].Content
|
||||
req = gollm.Request{
|
||||
Messages: []gollm.Message{
|
||||
{
|
||||
Role: gollm.RoleSystem,
|
||||
Text: systemPrompt,
|
||||
},
|
||||
{
|
||||
Role: gollm.RoleSystem,
|
||||
Text: providedIntel,
|
||||
},
|
||||
{
|
||||
Role: gollm.RoleUser,
|
||||
Text: summarizedData,
|
||||
},
|
||||
},
|
||||
}
|
||||
` + res.Text
|
||||
|
||||
res, err = a.WithSystemPrompt(systemPrompt).
|
||||
WithSystemPromptSuffix(``).
|
||||
WithToolbox(nil).
|
||||
CallAndExecute(ctx, gollm.Message{Role: gollm.RoleSystem, Text: providedIntel}, summarizedData)
|
||||
|
||||
resp, err = a.Model.ChatComplete(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to chat complete: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
// now go through the response and find all citations
|
||||
@@ -148,7 +116,7 @@ The moon is 4.5 billion years old [1,2], 238,855 miles away from the Earth [3],
|
||||
re := regexp.MustCompile(`\[([\d,\s]+)]`)
|
||||
|
||||
// find all the citations
|
||||
citations := re.FindAllString(resp.Choices[0].Content, -1)
|
||||
citations := re.FindAllString(res.Text, -1)
|
||||
|
||||
// now we need to find the sources
|
||||
lookup := map[int][]string{}
|
||||
@@ -168,19 +136,19 @@ The moon is 4.5 billion years old [1,2], 238,855 miles away from the Earth [3],
|
||||
}
|
||||
}
|
||||
|
||||
res := resp.Choices[0].Content
|
||||
text := res.Text
|
||||
|
||||
if len(lookup) > 0 {
|
||||
res += "\n\nHere are the sources for the information provided:\n"
|
||||
text += "\n\nHere are the sources for the information provided:\n"
|
||||
|
||||
for i := 1; i <= len(sources); i++ {
|
||||
if _, ok := lookup[i]; !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
res += "[" + fmt.Sprint(i) + "] <" + lookup[i][0] + ">\n"
|
||||
text += "[" + fmt.Sprint(i) + "] <" + lookup[i][0] + ">\n"
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return text, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user