Skip to content

Commit 4f4f6d6

Browse files
committed
Updated llm
1 parent 17f8603 commit 4f4f6d6

File tree

15 files changed

+533
-403
lines changed

15 files changed

+533
-403
lines changed

cmd/llm/complete.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"os"
8+
"strings"
9+
10+
// Packages
11+
llm "github.com/mutablelogic/go-llm"
12+
agent "github.com/mutablelogic/go-llm/pkg/agent"
13+
)
14+
15+
////////////////////////////////////////////////////////////////////////////////
16+
// TYPES
17+
18+
type CompleteCmd struct {
19+
Model string `arg:"" help:"Model name"`
20+
Prompt string `arg:"" optional:"" help:"Prompt"`
21+
File []string `type:"file" help:"Files to attach"`
22+
System string `flag:"system" help:"Set the system prompt"`
23+
NoStream bool `flag:"no-stream" help:"Do not stream output"`
24+
}
25+
26+
////////////////////////////////////////////////////////////////////////////////
27+
// PUBLIC METHODS
28+
29+
func (cmd *CompleteCmd) Run(globals *Globals) error {
30+
return runagent(globals, func(ctx context.Context, client llm.Agent) error {
31+
var prompt []byte
32+
33+
// Load the model
34+
model, err := client.(*agent.Agent).GetModel(ctx, cmd.Model)
35+
if err != nil {
36+
return err
37+
}
38+
39+
// If we are pipeline content in via stdin
40+
fileInfo, err := os.Stdin.Stat()
41+
if err != nil {
42+
return llm.ErrInternalServerError.Withf("Failed to get stdin stat: %v", err)
43+
}
44+
if (fileInfo.Mode() & os.ModeCharDevice) == 0 {
45+
if data, err := io.ReadAll(os.Stdin); err != nil {
46+
return err
47+
} else if len(data) > 0 {
48+
prompt = data
49+
}
50+
}
51+
52+
// Append any further prompt
53+
if len(cmd.Prompt) > 0 {
54+
prompt = append(prompt, []byte("\n\n")...)
55+
prompt = append(prompt, []byte(cmd.Prompt)...)
56+
}
57+
58+
opts := cmd.opts()
59+
if !cmd.NoStream {
60+
// Add streaming callback
61+
var buf string
62+
opts = append(opts, llm.WithStream(func(c llm.Completion) {
63+
fmt.Print(strings.TrimPrefix(c.Text(0), buf))
64+
buf = c.Text(0)
65+
}))
66+
}
67+
68+
// Add attachments
69+
for _, file := range cmd.File {
70+
f, err := os.Open(file)
71+
if err != nil {
72+
return err
73+
}
74+
defer f.Close()
75+
opts = append(opts, llm.WithAttachment(f))
76+
}
77+
78+
// Make the completion
79+
completion, err := model.Completion(ctx, string(prompt), opts...)
80+
if err != nil {
81+
return err
82+
}
83+
84+
// Print the completion
85+
if cmd.NoStream {
86+
fmt.Println(completion.Text(0))
87+
} else {
88+
fmt.Println("")
89+
}
90+
91+
// Return success
92+
return nil
93+
})
94+
}
95+
96+
func (cmd *CompleteCmd) opts() []llm.Opt {
97+
opts := []llm.Opt{}
98+
if cmd.System != "" {
99+
opts = append(opts, llm.WithSystemPrompt(cmd.System))
100+
}
101+
return opts
102+
}

cmd/llm/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os/signal"
77
"path/filepath"
88
"syscall"
9+
"time"
910

1011
// Packages
1112
kong "github.com/alecthomas/kong"
@@ -21,8 +22,9 @@ import (
2122

2223
type Globals struct {
2324
// Debugging
24-
Debug bool `name:"debug" help:"Enable debug output"`
25-
Verbose bool `name:"verbose" help:"Enable verbose output"`
25+
Debug bool `name:"debug" help:"Enable debug output"`
26+
Verbose bool `name:"verbose" help:"Enable verbose output"`
27+
Timeout time.Duration `name:"timeout" help:"Timeout for the command"`
2628

2729
// Agents
2830
Ollama `embed:"" help:"Ollama configuration"`
@@ -71,6 +73,7 @@ type CLI struct {
7173
// Commands
7274
Download DownloadModelCmd `cmd:"" help:"Download a model"`
7375
Chat ChatCmd `cmd:"" help:"Start a chat session"`
76+
Complete CompleteCmd `cmd:"" help:"Complete a prompt"`
7477
}
7578

7679
////////////////////////////////////////////////////////////////////////////////
@@ -106,13 +109,16 @@ func main() {
106109
if cli.Debug || cli.Verbose {
107110
clientopts = append(clientopts, client.OptTrace(os.Stderr, cli.Verbose))
108111
}
112+
if cli.Timeout > 0 {
113+
clientopts = append(clientopts, client.OptTimeout(cli.Timeout))
114+
}
109115

110116
// Create an agent
111117
opts := []llm.Opt{}
118+
if cli.OllamaEndpoint != "" {
119+
opts = append(opts, agent.WithOllama(cli.OllamaEndpoint, clientopts...))
120+
}
112121
/*
113-
if cli.OllamaEndpoint != "" {
114-
opts = append(opts, agent.WithOllama(cli.OllamaEndpoint, clientopts...))
115-
}
116122
if cli.AnthropicKey != "" {
117123
opts = append(opts, agent.WithAnthropic(cli.AnthropicKey, clientopts...))
118124
}

error.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
ErrBadParameter
1414
ErrNotImplemented
1515
ErrConflict
16+
ErrInternalServerError
1617
)
1718

1819
////////////////////////////////////////////////////////////////////////////////
@@ -36,6 +37,8 @@ func (e Err) Error() string {
3637
return "not implemented"
3738
case ErrConflict:
3839
return "conflict"
40+
case ErrInternalServerError:
41+
return "internal server error"
3942
}
4043
return fmt.Sprintf("error code %d", int(e))
4144
}

pkg/agent/opt.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
// Packages
55
client "github.com/mutablelogic/go-client"
66
llm "github.com/mutablelogic/go-llm"
7+
"github.com/mutablelogic/go-llm/pkg/ollama"
78
openai "github.com/mutablelogic/go-llm/pkg/openai"
89
)
910

1011
////////////////////////////////////////////////////////////////////////////////
1112
// PUBLIC METHODS
12-
/*
13+
1314
func WithOllama(endpoint string, opts ...client.ClientOpt) llm.Opt {
1415
return func(o *llm.Opts) error {
1516
client, err := ollama.New(endpoint, opts...)
@@ -21,6 +22,7 @@ func WithOllama(endpoint string, opts ...client.ClientOpt) llm.Opt {
2122
}
2223
}
2324

25+
/*
2426
func WithAnthropic(key string, opts ...client.ClientOpt) llm.Opt {
2527
return func(o *llm.Opts) error {
2628
client, err := anthropic.New(key, opts...)

pkg/ollama/chat.go

Lines changed: 0 additions & 152 deletions
This file was deleted.

pkg/ollama/client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ollama
22

33
import (
4-
54
// Packages
65
client "github.com/mutablelogic/go-client"
76
llm "github.com/mutablelogic/go-llm"
@@ -14,7 +13,6 @@ type Client struct {
1413
*client.Client
1514
}
1615

17-
// Ensure it satisfies the agent.Agent interface
1816
var _ llm.Agent = (*Client)(nil)
1917

2018
///////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)