Compare commits

...

6 Commits

Author SHA1 Message Date
5b338d4129 implement the wolfram function 2025-03-16 23:00:04 -04:00
bdaa3b7d96 implement the wolfram function 2025-03-16 22:59:25 -04:00
bf16b4b0cd update deps 2025-03-16 22:52:59 -04:00
568a0e99e4 update deps 2025-03-16 22:40:48 -04:00
4c106d32cb update against go-llm changes 2025-03-16 22:39:47 -04:00
1d47cf5758 Add customizable prompts for system messages in Options
Introduced `SystemPrompt` and `ExtraSystemPrompts` to allow more flexible and context-aware system message customization. Replaced static default system message with dynamic handling using these new fields, falling back to `DefaultPrompt` if needed. Updated the request construction logic to incorporate the newly added prompt options.
2025-03-16 17:08:09 -04:00
2 changed files with 126 additions and 83 deletions

58
go.mod
View File

@@ -4,25 +4,27 @@ go 1.23.2
replace github.com/rocketlaunchr/google-search => github.com/chrisjoyce911/google-search v0.0.0-20230910003754-e501aedf805a replace github.com/rocketlaunchr/google-search => github.com/chrisjoyce911/google-search v0.0.0-20230910003754-e501aedf805a
replace gitea.stevedudenhoeffer.com/steve/go-llm => ../go-llm //replace gitea.stevedudenhoeffer.com/steve/go-llm => ../go-llm
require ( require (
gitea.stevedudenhoeffer.com/steve/go-extractor v0.0.0-20250123020607-964a98a5a884 gitea.stevedudenhoeffer.com/steve/go-extractor v0.0.0-20250315044602-7c0e44a22f2c
gitea.stevedudenhoeffer.com/steve/go-llm v0.0.0-20250123045620-0d909edd44d9 gitea.stevedudenhoeffer.com/steve/go-llm v0.0.0-20250317023858-7f5e34e437a7
github.com/Edw590/go-wolfram v0.0.0-20241010091529-fb9031908c5d
github.com/advancedlogic/GoOse v0.0.0-20231203033844-ae6b36caf275 github.com/advancedlogic/GoOse v0.0.0-20231203033844-ae6b36caf275
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/playwright-community/playwright-go v0.5001.0 github.com/playwright-community/playwright-go v0.5001.0
github.com/rocketlaunchr/google-search v1.1.6 github.com/rocketlaunchr/google-search v1.1.6
github.com/urfave/cli v1.22.16 github.com/urfave/cli v1.22.16
go.starlark.net v0.0.0-20250225190231-0d3f41d403af
) )
require ( require (
cloud.google.com/go v0.118.3 // indirect cloud.google.com/go v0.119.0 // indirect
cloud.google.com/go/ai v0.10.0 // indirect cloud.google.com/go/ai v0.10.1 // indirect
cloud.google.com/go/auth v0.15.0 // indirect cloud.google.com/go/auth v0.15.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/longrunning v0.6.4 // indirect cloud.google.com/go/longrunning v0.6.6 // indirect
github.com/PuerkitoBio/goquery v1.10.2 // indirect github.com/PuerkitoBio/goquery v1.10.2 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/antchfx/htmlquery v1.3.4 // indirect github.com/antchfx/htmlquery v1.3.4 // indirect
@@ -30,11 +32,11 @@ require (
github.com/antchfx/xpath v1.3.3 // indirect github.com/antchfx/xpath v1.3.3 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
github.com/deckarep/golang-set/v2 v2.7.0 // indirect github.com/deckarep/golang-set/v2 v2.8.0 // indirect
github.com/fatih/set v0.2.1 // indirect github.com/fatih/set v0.2.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 // indirect github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect github.com/go-jose/go-jose/v3 v3.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-resty/resty/v2 v2.16.5 // indirect github.com/go-resty/resty/v2 v2.16.5 // indirect
@@ -49,38 +51,38 @@ require (
github.com/google/generative-ai-go v0.19.0 // indirect github.com/google/generative-ai-go v0.19.0 // indirect
github.com/google/s2a-go v0.1.9 // indirect github.com/google/s2a-go v0.1.9 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/liushuangls/go-anthropic/v2 v2.13.1 // indirect github.com/liushuangls/go-anthropic/v2 v2.14.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/sashabaranov/go-openai v1.37.0 // indirect github.com/sashabaranov/go-openai v1.38.0 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/temoto/robotstxt v1.1.2 // indirect github.com/temoto/robotstxt v1.1.2 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect go.opentelemetry.io/otel/trace v1.35.0 // indirect
golang.org/x/crypto v0.34.0 // indirect golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
golang.org/x/net v0.35.0 // indirect golang.org/x/net v0.37.0 // indirect
golang.org/x/oauth2 v0.26.0 // indirect golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sync v0.11.0 // indirect golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.30.0 // indirect golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.22.0 // indirect golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.10.0 // indirect golang.org/x/time v0.11.0 // indirect
google.golang.org/api v0.222.0 // indirect google.golang.org/api v0.226.0 // indirect
google.golang.org/appengine v1.6.8 // indirect google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250219182151-9fdb1cabc7b2 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
google.golang.org/grpc v1.70.0 // indirect google.golang.org/grpc v1.71.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect google.golang.org/protobuf v1.36.5 // indirect
) )

View File

@@ -4,6 +4,10 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/Edw590/go-wolfram"
"go.starlark.net/lib/math"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
"log/slog" "log/slog"
"net/url" "net/url"
"strings" "strings"
@@ -32,6 +36,8 @@ type Question struct {
// Answers is a list of answers to a question // Answers is a list of answers to a question
type Answers []string type Answers []string
const DefaultPrompt = "You are being asked to answer a question. You must respond with a function. You can answer it if you know the answer, or if some functions exist you can use those to help you find the answer."
type Options struct { type Options struct {
// MaxSearches is the maximum possible number of searches to execute for this question. If this is set to 5, the function could // MaxSearches is the maximum possible number of searches to execute for this question. If this is set to 5, the function could
// search up to 5 possible times to find an answer. // search up to 5 possible times to find an answer.
@@ -50,6 +56,19 @@ type Options struct {
// The "answer" and "no_answer" functions are not included in this callback. // The "answer" and "no_answer" functions are not included in this callback.
// Return an error to stop the function from being called. // Return an error to stop the function from being called.
OnNewFunction func(ctx context.Context, funcName string, question string, parameter string) error OnNewFunction func(ctx context.Context, funcName string, question string, parameter string) error
// SystemPrompt is the prompt to use when asking the system to answer a question.
// If this is empty, DefaultPrompt will be used.
SystemPrompt string
// ExtraSystemPrompts is a list of extra prompts to use when asking the system to answer a question. Use these for
// variety in the prompts, or passing in some useful contextually relevant information.
// All of these will be used in addition to the SystemPrompt.
ExtraSystemPrompts []string
// WolframAppID is the Wolfram Alpha App ID to use when searching Wolfram Alpha for answers. If not set, the
// wolfram function will not be available.
WolframAppID string
} }
var DefaultOptions = Options{ var DefaultOptions = Options{
@@ -63,39 +82,6 @@ type Result struct {
Error error Error error
} }
func fanExecuteToolCalls(ctx context.Context, toolBox *gollm.ToolBox, calls []gollm.ToolCall) []Result {
var results []Result
var resultsOutput = make(chan Result, len(calls))
fnCall := func(call gollm.ToolCall) Result {
str, err := toolBox.Execute(ctx, call)
if err != nil {
return Result{
Error: err,
}
}
return Result{
Result: str,
}
}
for _, call := range calls {
go func(call gollm.ToolCall) {
resultsOutput <- fnCall(call)
}(call)
}
for i := 0; i < len(calls); i++ {
result := <-resultsOutput
results = append(results, result)
}
close(resultsOutput)
return results
}
type article struct { type article struct {
URL string URL string
Title string Title string
@@ -145,11 +131,11 @@ func extractArticle(ctx context.Context, c cache.Cache, u *url.URL) (res article
}, nil }, nil
} }
func doesTextAnswerQuestion(ctx context.Context, q Question, text string) (string, error) { func doesTextAnswerQuestion(ctx *gollm.Context, q Question, text string) (string, error) {
fnAnswer := gollm.NewFunction( fnAnswer := gollm.NewFunction(
"answer", "answer",
"The answer from the given text that answers the question.", "The answer from the given text that answers the question.",
func(ctx context.Context, args struct { func(ctx *gollm.Context, args struct {
Answer string `description:"the answer to the question, the answer should come from the text"` Answer string `description:"the answer to the question, the answer should come from the text"`
}) (string, error) { }) (string, error) {
return args.Answer, nil return args.Answer, nil
@@ -158,7 +144,7 @@ func doesTextAnswerQuestion(ctx context.Context, q Question, text string) (strin
fnNoAnswer := gollm.NewFunction( fnNoAnswer := gollm.NewFunction(
"no_answer", "no_answer",
"Indicate that the text does not answer the question.", "Indicate that the text does not answer the question.",
func(ctx context.Context, args struct { func(ctx *gollm.Context, args struct {
Ignored string `description:"ignored, just here to make sure the function is called. Fill with anything."` Ignored string `description:"ignored, just here to make sure the function is called. Fill with anything."`
}) (string, error) { }) (string, error) {
return "", nil return "", nil
@@ -199,8 +185,7 @@ func doesTextAnswerQuestion(ctx context.Context, q Question, text string) (strin
return req.Toolbox.Execute(ctx, res.Choices[0].Calls[0]) return req.Toolbox.Execute(ctx, res.Choices[0].Calls[0])
} }
func functionSearch(ctx context.Context, q Question, searchTerm string) (string, error) { func functionSearch(ctx *gollm.Context, q Question, searchTerm string) (string, error) {
slog.Info("searching", "search", searchTerm, "question", q) slog.Info("searching", "search", searchTerm, "question", q)
res, err := q.Search.Search(ctx, searchTerm) res, err := q.Search.Search(ctx, searchTerm)
if err != nil { if err != nil {
@@ -249,11 +234,11 @@ func functionSearch(ctx context.Context, q Question, searchTerm string) (string,
return "", nil return "", nil
} }
func functionThink(ctx context.Context, q Question) (string, error) { func functionThink(ctx *gollm.Context, q Question) (string, error) {
fnAnswer := gollm.NewFunction( fnAnswer := gollm.NewFunction(
"answer", "answer",
"Answer the question.", "Answer the question.",
func(ctx context.Context, args struct { func(ctx *gollm.Context, args struct {
Answer string `description:"the answer to the question"` Answer string `description:"the answer to the question"`
}) (string, error) { }) (string, error) {
return args.Answer, nil return args.Answer, nil
@@ -296,7 +281,7 @@ func (o Options) Answer(ctx context.Context, q Question) (Answers, error) {
fnSearch := gollm.NewFunction( fnSearch := gollm.NewFunction(
"search", "search",
"Search the web for an answer to a question. You can call this function up to "+fmt.Sprint(o.MaxSearches)+" times.", "Search the web for an answer to a question. You can call this function up to "+fmt.Sprint(o.MaxSearches)+" times.",
func(ctx context.Context, args struct { func(ctx *gollm.Context, args struct {
SearchQuery string `description:"what to search the web for for this question"` SearchQuery string `description:"what to search the web for for this question"`
Question string `description:"what question(s) you are trying to answer with this search"` Question string `description:"what question(s) you are trying to answer with this search"`
}) (string, error) { }) (string, error) {
@@ -309,7 +294,7 @@ func (o Options) Answer(ctx context.Context, q Question) (Answers, error) {
fnThink := gollm.NewFunction( fnThink := gollm.NewFunction(
"think", "think",
"Think about a question. This is useful for breaking down complex questions into smaller parts that are easier to answer.", "Think about a question. This is useful for breaking down complex questions into smaller parts that are easier to answer.",
func(ctx context.Context, args struct { func(ctx *gollm.Context, args struct {
Question string `json:"question" description:"the question to think about"` Question string `json:"question" description:"the question to think about"`
}) (string, error) { }) (string, error) {
q2 := q q2 := q
@@ -321,13 +306,50 @@ func (o Options) Answer(ctx context.Context, q Question) (Answers, error) {
fnAnswer := gollm.NewFunction( fnAnswer := gollm.NewFunction(
"answer", "answer",
"You definitively answer a question, if you call this it means you know the answer and do not need to search for it or use any other function to find it", "You definitively answer a question, if you call this it means you know the answer and do not need to search for it or use any other function to find it",
func(ctx context.Context, args struct { func(ctx *gollm.Context, args struct {
Answer string `json:"answer" description:"the answer to the question"` Answer string `json:"answer" description:"the answer to the question"`
}) (string, error) { }) (string, error) {
return args.Answer, nil return args.Answer, nil
}) })
var funcs = []*gollm.Function{fnAnswer} var fnWolfram *gollm.Function
if o.WolframAppID != "" {
fnWolfram = gollm.NewFunction(
"wolfram",
"Search Wolfram Alpha for an answer to a question.",
func(ctx *gollm.Context, args struct {
Question string `description:"the question to search for"`
}) (string, error) {
cl := wolfram.Client{
AppID: o.WolframAppID,
}
unit := wolfram.Imperial
return cl.GetShortAnswerQuery(args.Question, unit, 10)
})
}
fnCalculate := gollm.NewFunction(
"calculate",
"Calculate a mathematical expression using starlark.",
func(ctx *gollm.Context, args struct {
Expression string `description:"the mathematical expression to calculate, in starlark format"`
}) (string, error) {
fileOpts := syntax.FileOptions{}
v, err := starlark.EvalOptions(&fileOpts, &starlark.Thread{Name: "main"}, "input", args.Expression, math.Module.Members)
if err != nil {
return "", err
}
return v.String(), nil
})
var funcs = []*gollm.Function{fnAnswer, fnCalculate}
if fnWolfram != nil {
funcs = append(funcs, fnWolfram)
}
if o.MaxSearches > 0 { if o.MaxSearches > 0 {
funcs = append(funcs, fnSearch) funcs = append(funcs, fnSearch)
@@ -339,17 +361,36 @@ func (o Options) Answer(ctx context.Context, q Question) (Answers, error) {
var temp float32 = 0.8 var temp float32 = 0.8
var messages []gollm.Message
if o.SystemPrompt != "" {
messages = append(messages, gollm.Message{
Role: gollm.RoleSystem,
Text: o.SystemPrompt,
})
} else {
messages = append(messages, gollm.Message{
Role: gollm.RoleSystem,
Text: DefaultPrompt,
})
}
for _, prompt := range o.ExtraSystemPrompts {
messages = append(messages, gollm.Message{
Role: gollm.RoleSystem,
Text: prompt,
})
}
if q.Question != "" {
messages = append(messages, gollm.Message{
Role: gollm.RoleUser,
Text: q.Question,
})
}
req := gollm.Request{ req := gollm.Request{
Messages: []gollm.Message{ Messages: messages,
{
Role: gollm.RoleSystem,
Text: "You are being asked to answer a question. You must respond with a function. You can answer it if you know the answer, or if some functions exist you can use those to help you find the answer.",
},
{
Role: gollm.RoleUser,
Text: q.Question,
},
},
Toolbox: gollm.NewToolBox(funcs...), Toolbox: gollm.NewToolBox(funcs...),
Temperature: &temp, Temperature: &temp,
} }
@@ -374,7 +415,7 @@ func (o Options) Answer(ctx context.Context, q Question) (Answers, error) {
var calls []Result var calls []Result
var callsOutput = make(chan Result, len(choice.Calls)) var callsOutput = make(chan Result, len(choice.Calls))
fnCall := func(call gollm.ToolCall) Result { fnCall := func(call gollm.ToolCall) Result {
str, err := req.Toolbox.Execute(ctx, call) str, err := req.Toolbox.Execute(gollm.NewContext(ctx, req, &choice, &call), call)
if err != nil { if err != nil {
return Result{ return Result{