Skip to content

Commit 40ee5b3

Browse files
committed
Updated
1 parent c8d9dba commit 40ee5b3

File tree

7 files changed

+51
-49
lines changed

7 files changed

+51
-49
lines changed

cmd/agent/generate.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
// TYPES
1717

1818
type GenerateCmd struct {
19-
Model string `arg:"" help:"Model name"`
19+
Model string `arg:"" help:"Model name"`
20+
NoStream bool `flag:"nostream" help:"Disable streaming"`
2021
}
2122

2223
////////////////////////////////////////////////////////////////////////////////
@@ -35,7 +36,10 @@ func (cmd *GenerateCmd) Run(globals *Globals) error {
3536
}
3637

3738
// Create a session
38-
session := model.Context()
39+
session, err := model.Context(agent.WithStream(!cmd.NoStream))
40+
if err != nil {
41+
return err
42+
}
3943

4044
// Continue looping until end of input
4145
for {
@@ -58,14 +62,7 @@ func (cmd *GenerateCmd) Run(globals *Globals) error {
5862
if err != nil {
5963
return err
6064
}
61-
fmt.Println("RESPONSE=", response.Text())
65+
fmt.Println(response.Text())
6266
}
63-
/*
64-
// Generate the content
65-
66-
67-
// Print the response
68-
return nil
69-
*/
7067
})
7168
}

model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ type Model interface {
66
Name() string
77

88
// Return a context object, and set options
9-
Context(...Opt) Context
9+
Context(...Opt) (Context, error)
1010
}

pkg/agent/generate.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ func (a *Agent) Generate(ctx context.Context, m llm.Model, context llm.Context,
2323
agent = agent_
2424
}
2525

26+
// Get the options
27+
2628
// Apply the options
27-
opts, err := translate(agent, opts...)
28-
if err != nil {
29-
return nil, err
30-
}
29+
//opts, err := translate(agent, opts...)
30+
//if err != nil {
31+
// return nil, err
32+
//}
3133

32-
log.Print("agent.Generate =>", context, opts)
34+
log.Print("agent.Generate =>", context)
3335

3436
// Call Generate for the agent
3537
return agent.Generate(ctx, m, context, opts...)

pkg/agent/opt.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package agent
22

33
import (
44
// Packages
5+
"fmt"
6+
57
client "github.com/mutablelogic/go-client"
68
llm "github.com/mutablelogic/go-llm"
79
anthropic "github.com/mutablelogic/go-llm/pkg/anthropic"
@@ -15,9 +17,9 @@ type opt struct {
1517
agents map[string]llm.Agent
1618
tools map[string]llm.Tool
1719

18-
// Selected agent
19-
agent llm.Agent
20-
opts []llm.Opt
20+
// Translated options for each agent implementation
21+
ollama []llm.Opt
22+
anthropic []llm.Opt
2123
}
2224

2325
////////////////////////////////////////////////////////////////////////////////
@@ -35,28 +37,6 @@ func apply(opts ...llm.Opt) (*opt, error) {
3537
return o, nil
3638
}
3739

38-
// Translate options from general to agent-specific
39-
func translate(agent llm.Agent, opts ...llm.Opt) ([]llm.Opt, error) {
40-
o := new(opt)
41-
42-
// Set agent
43-
if agent == nil {
44-
return nil, llm.ErrBadParameter.With("agent")
45-
} else {
46-
o.agent = agent
47-
}
48-
49-
// Apply options
50-
for _, opt := range opts {
51-
if err := opt(o); err != nil {
52-
return nil, err
53-
}
54-
}
55-
56-
// Return translated options
57-
return o.opts, nil
58-
}
59-
6040
////////////////////////////////////////////////////////////////////////////////
6141
// PUBLIC METHODS
6242

@@ -82,6 +62,7 @@ func WithAnthropic(key string, opts ...client.ClientOpt) llm.Opt {
8262
}
8363
}
8464

65+
// Append tools
8566
func WithTools(tools ...llm.Tool) llm.Opt {
8667
return func(o any) error {
8768
for _, tool := range tools {
@@ -96,6 +77,19 @@ func WithTools(tools ...llm.Tool) llm.Opt {
9677
}
9778
}
9879

80+
// Set streaming function
81+
func WithStream(v bool) llm.Opt {
82+
return func(o any) error {
83+
o.(*opt).ollama = append(o.(*opt).ollama, ollama.WithStream(func(r *ollama.Response) {
84+
fmt.Println(r)
85+
}))
86+
o.(*opt).anthropic = append(o.(*opt).anthropic, anthropic.WithStream(func(r *anthropic.Response) {
87+
fmt.Println(r)
88+
}))
89+
return nil
90+
}
91+
}
92+
9993
////////////////////////////////////////////////////////////////////////////////
10094
// PRIVATE METHODS
10195

pkg/anthropic/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ var _ llm.Context = (*session)(nil)
1818
///////////////////////////////////////////////////////////////////////////////
1919
// LIFECYCLE
2020

21-
func (*model) Context(...llm.Opt) llm.Context {
21+
func (*model) Context(...llm.Opt) (llm.Context, error) {
2222
// TODO: Currently ignoring options
23-
return &session{}
23+
return &session{}, nil
2424
}
2525

2626
///////////////////////////////////////////////////////////////////////////////

pkg/ollama/context.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
// Implementation of a message session, which is a sequence of messages
1414
type session struct {
15+
*opt
1516
seq []*MessageMeta
1617
}
1718

@@ -20,9 +21,17 @@ var _ llm.Context = (*session)(nil)
2021
///////////////////////////////////////////////////////////////////////////////
2122
// LIFECYCLE
2223

23-
func (*model) Context(...llm.Opt) llm.Context {
24-
// TODO: Currently ignoring options
25-
return &session{}
24+
func (*model) Context(opts ...llm.Opt) (llm.Context, error) {
25+
// Apply options
26+
opt, err := apply(opts...)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
// Return an empty session
32+
return &session{
33+
opt: opt,
34+
}, nil
2635
}
2736

2837
///////////////////////////////////////////////////////////////////////////////

pkg/ollama/opt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ func WithPullStatus(fn func(*PullStatus)) llm.Opt {
8080
}
8181

8282
// Chat: Stream the response as it is received.
83-
func WithChatStream(fn func(*Response)) llm.Opt {
83+
func WithStream(fn func(*Response)) llm.Opt {
8484
return func(o any) error {
8585
if fn == nil {
8686
return llm.ErrBadParameter.With("callback required")
8787
}
8888
if len(o.(*opt).tools) > 0 {
89-
return llm.ErrBadParameter.With("tools not supported with streaming")
89+
return llm.ErrBadParameter.With("streaming not supported with tools")
9090
}
9191
o.(*opt).stream = true
9292
o.(*opt).chatcallback = fn
@@ -99,7 +99,7 @@ func WithTool(v *Tool) llm.Opt {
9999
return func(o any) error {
100100
// We can't use streaming when tools are included
101101
if o.(*opt).stream {
102-
return llm.ErrBadParameter.With("streaming not supported with tools")
102+
return llm.ErrBadParameter.With("tools not supported with streaming")
103103
}
104104
if v != nil {
105105
o.(*opt).tools = append(o.(*opt).tools, v)

0 commit comments

Comments
 (0)