Refactor Knowledge struct into shared package

Moved the Knowledge struct and related types to the shared package, updating all references across the codebase. This improves modularity and enables better reuse of the Knowledge type across different components.
This commit is contained in:
2025-05-03 22:09:02 -04:00
parent d2b9eb350e
commit 1c3ea7d1f1
11 changed files with 63 additions and 51 deletions

View File

@@ -14,6 +14,8 @@ import (
"gitea.stevedudenhoeffer.com/steve/go-extractor"
"gitea.stevedudenhoeffer.com/steve/go-extractor/sites/duckduckgo"
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
"gitea.stevedudenhoeffer.com/steve/answer/pkg/agents/shared"
)
func deferClose(c io.Closer) {
@@ -26,7 +28,7 @@ func deferClose(c io.Closer) {
type SearchTool struct {
Name string
Description string
Function func(ctx context.Context, src *url.URL, questions []string) (Knowledge, error)
Function func(ctx context.Context, src *url.URL, questions []string) (shared.Knowledge, error)
}
// SearchAndUseTools will search duckduckgo for the given question, and then ask the LLM to select a search result to
@@ -41,8 +43,8 @@ type SearchTool struct {
// will be combined and returned.
// messages will be appended to all search results. The types of messages that can be appended are both string and
// gollm.Message.
func (a Agent) SearchAndUseTools(ctx context.Context, searchQuery string, questions []string, loops int, allowConcurrent bool, maxReads int, tools []SearchTool, messages ...any) (Knowledge, error) {
var knowledge = Knowledge{
func (a Agent) SearchAndUseTools(ctx context.Context, searchQuery string, questions []string, loops int, allowConcurrent bool, maxReads int, tools []SearchTool, messages ...any) (shared.Knowledge, error) {
var knowledge = shared.Knowledge{
OriginalQuestions: questions,
RemainingQuestions: questions,
}
@@ -184,13 +186,13 @@ Use appropriate tools to analyze the search results and determine if they answer
}
slog.Info("search results called and executed", "error", err, "results text", results.Text, "results", results.CallResults)
var learned []Knowledge
var learned []shared.Knowledge
for _, r := range results.CallResults {
if r.Error != nil {
continue
}
if k, ok := r.Result.(Knowledge); ok {
if k, ok := r.Result.(shared.Knowledge); ok {
learned = append(learned, k)
} else {
slog.Error("result is not knowledge", "result", r.Result)
@@ -210,7 +212,7 @@ Use appropriate tools to analyze the search results and determine if they answer
return knowledge, nil
}
func (a Agent) SearchAndRead(ctx context.Context, searchQuery string, questions []string, allowConcurrent bool, maxReads int) (Knowledge, error) {
func (a Agent) SearchAndRead(ctx context.Context, searchQuery string, questions []string, allowConcurrent bool, maxReads int) (shared.Knowledge, error) {
return a.SearchAndUseTools(ctx, searchQuery, questions, 2, allowConcurrent, maxReads, []SearchTool{
{
Name: "readpage",