Skip to content

Commit 3aae0fd

Browse files
committed
experiment: enable retrieving full Wikipedia articles
Signed-off-by: Katharine Berry <[email protected]>
1 parent acc601a commit 3aae0fd

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

service/assistant/functions/functions.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ func CallFunction(ctx context.Context, qt *quota.Tracker, fn, args string) (stri
9191
if err != nil {
9292
return "", fmt.Errorf("unable to marshal response: %v", err)
9393
}
94-
if len(r) > MaxResponseSize {
95-
r = r[:MaxResponseSize]
96-
}
94+
//if len(r) > MaxResponseSize {
95+
// r = r[:MaxResponseSize]
96+
//}
9797
return string(r), nil
9898
}
9999

@@ -169,9 +169,9 @@ func CallAction(ctx context.Context, qt *quota.Tracker, fn, args string, ws *web
169169
if err != nil {
170170
return "", fmt.Errorf("unable to marshal response: %v", err)
171171
}
172-
if len(r) > MaxResponseSize {
173-
r = r[:MaxResponseSize]
174-
}
172+
//if len(r) > MaxResponseSize {
173+
// r = r[:MaxResponseSize]
174+
//}
175175
return string(r), nil
176176
}
177177

service/assistant/functions/wikipedia.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import (
3131
)
3232

3333
type WikipediaRequest struct {
34-
Query string `json:"article_name"`
34+
Query string `json:"article_name"`
35+
CompleteArticle bool `json:"complete_article"`
3536
}
3637

3738
type WikipediaResponse struct {
@@ -42,7 +43,7 @@ func init() {
4243
registerFunction(Registration{
4344
Definition: genai.FunctionDeclaration{
4445
Name: "wikipedia",
45-
Description: "Look up the content of a single named English Wikipedia page.",
46+
Description: "Look up the content of a single named English Wikipedia page. Never say the Wikipedia page didn't have the information needed without first trying to fetch the complete article.",
4647
Parameters: &genai.Schema{
4748
Type: genai.TypeObject,
4849
Nullable: false,
@@ -52,6 +53,11 @@ func init() {
5253
Description: "The name of the English Wikipedia page to look up",
5354
Nullable: false,
5455
},
56+
"complete_article": {
57+
Type: genai.TypeBoolean,
58+
Description: "Whether to return the complete article or just the summary. Prefer to fetch only the summary. If the summary didn't have the information you expected, you can try again with the complete article.",
59+
Nullable: false,
60+
},
5561
},
5662
Required: []string{"article_name"},
5763
},
@@ -68,7 +74,7 @@ func queryWikipediaThought(args interface{}) string {
6874

6975
func queryWikipedia(ctx context.Context, quotaTracker *quota.Tracker, args interface{}) interface{} {
7076
req := args.(*WikipediaRequest)
71-
results, err := queryWikipediaInternal(ctx, req.Query, true)
77+
results, err := queryWikipediaInternal(ctx, req.Query, req.CompleteArticle, true)
7278
if err != nil {
7379
return Error{Error: err.Error()}
7480
}
@@ -77,13 +83,17 @@ func queryWikipedia(ctx context.Context, quotaTracker *quota.Tracker, args inter
7783
}
7884
}
7985

80-
func queryWikipediaInternal(ctx context.Context, query string, allowSearch bool) (string, error) {
86+
func queryWikipediaInternal(ctx context.Context, query string, completeArticle, allowSearch bool) (string, error) {
8187
ctx, span := beeline.StartSpan(ctx, "query_wikipedia")
8288
defer span.Send()
8389
span.AddField("title", query)
84-
log.Printf("Looking up Wikipedia article: %q\n", query)
90+
log.Printf("Looking up Wikipedia article: %q (complete: %t)\n", query, completeArticle)
8591
qs := url.QueryEscape(query)
86-
request, err := http.NewRequestWithContext(ctx, "GET", "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles="+qs+"&rvsection=0&rvslots=main", nil)
92+
url := "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=" + qs + "&rvslots=main"
93+
if !completeArticle {
94+
url += "&rvsection=0"
95+
}
96+
request, err := http.NewRequestWithContext(ctx, "GET", url, nil)
8797
if err != nil {
8898
return "", err
8999
}
@@ -116,9 +126,13 @@ func queryWikipediaInternal(ctx context.Context, query string, allowSearch bool)
116126
if len(searchResult) == 0 {
117127
return "", errors.New("Wikipedia page not found. Try to answer using your general knowledge.")
118128
}
119-
return queryWikipediaInternal(ctx, searchResult[0], false)
129+
return queryWikipediaInternal(ctx, searchResult[0], completeArticle, false)
130+
}
131+
addendum := ""
132+
if !completeArticle {
133+
addendum = "\n\nThis was only the summary. If necessary, more information can be returned by repeating the query_wikipedia call with complete_article = true. You can always do this automatically, without prompting the user."
120134
}
121-
return string(content), nil
135+
return string(content) + addendum, nil
122136
}
123137

124138
func searchWikipedia(ctx context.Context, query string) ([]string, error) {

service/assistant/system_prompt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (ps *PromptSession) generateSystemPrompt(ctx context.Context) string {
9595
"Your name is Bobby, and you are running on a Pebble smartwatch. " +
9696
"The text you receive is transcribed from voice input. " +
9797
"Your knowledge cutoff is September 2024. However, you can use the wikipedia function to access the current content of specific Wikipedia pages. " +
98-
"Always follow Wikipedia redirects immediately and silently. Never ask the user whether you should check wikipedia - if you would ask, assume that you should. Don't mention looking up articles or Wikipedia to the user. " +
98+
"Always follow Wikipedia redirects immediately and silently. Never ask the user whether you should check wikipedia, or whether you should check the full article - if you would ask, assume that you should (but don't ever fetch full articles if you already have the answer to the question). Don't mention looking up articles or Wikipedia to the user. " +
9999
locationString +
100100
ps.generateTimeSentence(ctx) +
101101
"You may call multiple functions before responding to the user, if necessary. If executing a lua script fails, try hard to fix the script using the error message, and consider alternate approaches to solve the problem. " +

0 commit comments

Comments
 (0)