60 lines
3.0 KiB
Go
60 lines
3.0 KiB
Go
package agents
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
|
|
)
|
|
|
|
// ExtractKnowledge will take a knowledge object and use the gained knowledge to extract the knowledge relevant to the
|
|
// questions provided.
|
|
// sourceData is the raw text to analyze for the knowledge.
|
|
// source is the source of the information, such as a URL.
|
|
// questions are the questions that the knowledge is trying to answer.
|
|
// model is the chat completion model to use.
|
|
// contextualInformation is any contextual information that should be provided to the model.
|
|
// It will return the knowledge extracted from the sourceData along with any remaining questions.
|
|
// This agent call will not use the Agent's system prompts, but will instead form its own. The contextual information will be used.
|
|
func (a Agent) ExtractKnowledge(ctx context.Context, sourceData string, source string, questions []string) (Knowledge, error) {
|
|
|
|
var knowledge Knowledge
|
|
fnAnswer := gollm.NewFunction(
|
|
"learn",
|
|
`Use learn to pass some relevant information to the model. The model will use this information to answer the question. Use it to learn relevant information from the text. Keep these concise and relevant to the question.`,
|
|
func(ctx *gollm.Context, args struct {
|
|
Info string `description:"The information to learn from the text."`
|
|
}) (any, error) {
|
|
knowledge.Knowledge = append(knowledge.Knowledge, TidBit{Info: args.Info, Source: source})
|
|
return "", nil
|
|
})
|
|
|
|
fnNoAnswer := gollm.NewFunction(
|
|
"finished",
|
|
"Indicate that the text does not answer the question.",
|
|
func(ctx *gollm.Context, args struct {
|
|
Remaining string `description:"After all the knowledge has been learned, this is the parts of the question that are not answered, if any. Leave this blank if the text fully answers the question."`
|
|
}) (any, error) {
|
|
knowledge.RemainingQuestions = []string{args.Remaining}
|
|
return "", nil
|
|
})
|
|
|
|
// Overwrite this agent's system prompts with the ones needed for this function.
|
|
|
|
var questionPrompt = "The questions you are trying to answer using the text are:\n" + strings.Join(questions, "\n")
|
|
if len(questions) == 1 {
|
|
questionPrompt = "The question you are trying to answer using the text is: " + questions[0]
|
|
}
|
|
|
|
_, err := a.
|
|
WithSystemPrompt(`Evaluate the given text to see if you can answer any information from it relevant to the question that the user asks.
|
|
Use the "learn" function to pass relevant information to the model. You can use the "learn" function multiple times to pass multiple pieces of relevant information to the model.
|
|
If the text does not answer the question or you are done using "learn" to pass on knowledge then use the "finished" function and indicate the parts of the question that are not answered by anything learned.
|
|
You can call "learn" multiple times before calling "finished".`).
|
|
WithSystemPromptSuffix(``).
|
|
WithToolbox(gollm.NewToolBox(fnAnswer, fnNoAnswer)).
|
|
CallAndExecute(ctx, "The text for you to evaluate is: "+sourceData, questionPrompt)
|
|
|
|
return knowledge, err
|
|
}
|