Skip to content

Commit db433cd

Browse files
committed
Updated llm
1 parent 116185a commit db433cd

File tree

6 files changed

+107
-28
lines changed

6 files changed

+107
-28
lines changed

cmd/agent/chat.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (cmd *ChatCmd) Run(globals *Globals) error {
4040
opts := []llm.Opt{}
4141
if !cmd.NoStream {
4242
opts = append(opts, llm.WithStream(func(cc llm.ContextContent) {
43-
fmt.Println(cc)
43+
fmt.Println("STREAM", cc)
4444
}))
4545
}
4646
if cmd.System != "" {
@@ -73,17 +73,30 @@ func (cmd *ChatCmd) Run(globals *Globals) error {
7373
return err
7474
}
7575

76-
fmt.Println(session.Text())
77-
78-
// If there are tool calls, then do these
79-
calls := session.ToolCalls()
80-
if results, err := globals.toolkit.Run(ctx, calls...); err != nil {
81-
return err
82-
} else if err := session.FromTool(ctx, results...); err != nil {
83-
return err
84-
} else {
85-
fmt.Println(session.Text())
76+
// Repeat call tools until no more calls are made
77+
for {
78+
calls := session.ToolCalls()
79+
if len(calls) == 0 {
80+
break
81+
}
82+
if session.Text() != "" {
83+
fmt.Println("Calling", session.Text())
84+
} else {
85+
var names []string
86+
for _, call := range calls {
87+
names = append(names, call.Name())
88+
}
89+
fmt.Println("Calling", strings.Join(names, ", "))
90+
}
91+
if results, err := globals.toolkit.Run(ctx, calls...); err != nil {
92+
return err
93+
} else if err := session.FromTool(ctx, results...); err != nil {
94+
return err
95+
}
8696
}
97+
98+
// Print the response
99+
fmt.Println("SESSION", session.Text())
87100
}
88101
})
89102
}

pkg/anthropic/messages.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ func (anthropic *Client) Messages(ctx context.Context, context llm.Context, opts
150150
if err := evt.Json(&r); err != nil {
151151
return err
152152
} else if int(r.Index) != len(response.MessageMeta.Content)-1 {
153-
fmt.Println(response.MessageMeta)
154153
return fmt.Errorf("%s: unexpected index %d", r.Type, r.Index)
155154
} else if content, err := appendDelta(response.MessageMeta.Content, &r.Content); err != nil {
156155
return err

pkg/newsapi/agent.go

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package newsapi
22

33
import (
44
"context"
5+
"fmt"
6+
"slices"
57

68
// Packages
79
llm "github.com/mutablelogic/go-llm"
@@ -11,32 +13,78 @@ import (
1113
// TYPES
1214

1315
type headlines struct {
14-
*Client `json:"-"`
15-
CountryCode string `json:"country_code,omitempty" help:"The two-letter countrycode to return headlines for"`
16+
*Client `json:"-"`
17+
// CountryCode string `json:"country_code,omitempty" help:"The two-letter countrycode to return headlines for. Leave empty for worldwide headlines."`
18+
}
19+
20+
type search struct {
21+
*Client `json:"-"`
22+
Query string `json:"query" help:"A phrase used to search for news headlines." required:"true"`
23+
}
24+
25+
type category struct {
26+
*Client `json:"-"`
27+
Category string `json:"category" enum:"business, entertainment, health, science, sports, technology" help:"business, entertainment, health, science, sports, technology" required:"true"`
1628
}
1729

1830
var _ llm.Tool = (*headlines)(nil)
1931

32+
var (
33+
categories = []string{"business", "entertainment", "health", "science", "sports", "technology"}
34+
)
35+
2036
///////////////////////////////////////////////////////////////////////////////
2137
// HEADLINES
2238

2339
func (headlines) Name() string {
24-
return "current_headlines"
40+
return "news_headlines"
2541
}
2642

2743
func (headlines) Description() string {
28-
return "Return the current news headlines, optionally for a specific country"
44+
return "Return the current global news headlines"
2945
}
3046

3147
func (headlines *headlines) Run(ctx context.Context) (any, error) {
32-
response, err := headlines.Headlines(OptCategory("general"), OptLimit(5))
33-
if err != nil {
34-
return nil, err
48+
return headlines.Headlines(OptCategory("general"), OptLimit(10))
49+
}
50+
51+
///////////////////////////////////////////////////////////////////////////////
52+
// SEARCH
53+
54+
func (search) Name() string {
55+
return "news_search"
56+
}
57+
58+
func (search) Description() string {
59+
return "Search the news archive with a search query"
60+
}
61+
62+
func (search *search) Run(ctx context.Context) (any, error) {
63+
if search.Query == "" {
64+
return nil, nil
3565
}
36-
return map[string]any{
37-
"type": "text",
38-
"headlines": response,
39-
}, nil
66+
fmt.Printf("search for %q\n", search.Query)
67+
return search.Articles(OptQuery(search.Query), OptLimit(10))
68+
}
69+
70+
///////////////////////////////////////////////////////////////////////////////
71+
// CATEGORY
72+
73+
func (category) Name() string {
74+
return "news_headlines_category"
75+
}
76+
77+
func (category) Description() string {
78+
return "Return the news headlines for a specific category"
79+
}
80+
81+
func (category *category) Run(ctx context.Context) (any, error) {
82+
if !slices.Contains(categories, category.Category) {
83+
fmt.Printf("search for %q\n", category.Category)
84+
return category.Articles(OptQuery(category.Category), OptLimit(10))
85+
}
86+
fmt.Printf("category for %q\n", category.Category)
87+
return category.Headlines(OptCategory(category.Category), OptLimit(10))
4088
}
4189

4290
/*

pkg/newsapi/client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ func New(ApiKey string, opts ...client.ClientOpt) (*Client, error) {
4242

4343
func (newsapi *Client) RegisterWithToolKit(toolkit *tool.ToolKit) error {
4444
// Register tools
45-
if err := toolkit.Register(&headlines{newsapi, ""}); err != nil {
45+
if err := toolkit.Register(&headlines{newsapi}); err != nil {
46+
return err
47+
}
48+
if err := toolkit.Register(&search{newsapi, ""}); err != nil {
49+
return err
50+
}
51+
if err := toolkit.Register(&category{newsapi, ""}); err != nil {
4652
return err
4753
}
4854

pkg/ollama/chat.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,24 @@ func (ollama *Client) Chat(ctx context.Context, prompt llm.Context, opts ...llm.
6464
return nil, err
6565
}
6666

67+
// Append the system prompt at the beginning
68+
seq := make([]*MessageMeta, 0, len(prompt.(*session).seq)+1)
69+
if system := opt.SystemPrompt(); system != "" {
70+
seq = append(seq, &MessageMeta{
71+
Role: "system",
72+
Content: opt.SystemPrompt(),
73+
})
74+
}
75+
seq = append(seq, prompt.(*session).seq...)
76+
6777
// Request
6878
req, err := client.NewJSONRequest(reqChat{
6979
Model: prompt.(*session).model.Name(),
70-
Messages: prompt.(*session).seq,
80+
Messages: seq,
7181
Tools: optTools(ollama, opt),
7282
Format: optFormat(opt),
7383
Options: optOptions(opt),
7484
Stream: optStream(ollama, opt),
75-
System: optSystemPrompt(opt),
7685
KeepAlive: optKeepAlive(opt),
7786
})
7887
if err != nil {
@@ -97,8 +106,10 @@ func (ollama *Client) Chat(ctx context.Context, prompt llm.Context, opts ...llm.
97106
}
98107

99108
//Call the chat callback
100-
if fn := opt.StreamFn(); fn != nil {
101-
fn(&response)
109+
if optStream(ollama, opt) {
110+
if fn := opt.StreamFn(); fn != nil {
111+
fn(&response)
112+
}
102113
}
103114
return nil
104115
})); err != nil {

pkg/ollama/session.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ func (s *session) FromTool(ctx context.Context, results ...llm.ToolResult) error
9999
// Append the tool results
100100
for _, result := range results {
101101
if message, err := toolResult(result); err != nil {
102+
return err
103+
} else {
102104
s.seq = append(s.seq, message)
103105
}
104106
}

0 commit comments

Comments
 (0)