-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
117 lines (98 loc) · 2.89 KB
/
client.go
File metadata and controls
117 lines (98 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package goai
import (
"context"
"fmt"
"sync"
)
// Client is the main entry point for interacting with LLM providers
type Client struct {
provider Provider
options Options
}
// Response represents a response from an LLM provider
type Response struct {
Content string // The response text
TokensUsed int // Number of tokens used (if available)
LatencyMs int64 // Response latency in milliseconds
Model string // Model identifier
Provider string // Provider name
Metadata map[string]interface{} // Additional metadata
}
// Provider is the internal interface that all providers must implement
type Provider interface {
// chat sends a prompt to the LLM and returns the response
chat(ctx context.Context, prompt string, opts Options) (*Response, error)
// name returns the provider name
name() string
// model returns the model identifier
model() string
}
// ProviderFactory creates a new provider instance
type ProviderFactory func(model string, opts Options) (Provider, error)
var (
mu sync.RWMutex
providers = make(map[string]ProviderFactory)
)
// register registers a provider factory
func register(name string, factory ProviderFactory) {
mu.Lock()
defer mu.Unlock()
providers[name] = factory
}
// New creates a new LLM client
//
// Example:
// client := goai.New("openai", "gpt-4o",
// goai.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
// )
func New(providerName, model string, opts ...Option) (*Client, error) {
mu.RLock()
factory, exists := providers[providerName]
mu.RUnlock()
if !exists {
return nil, fmt.Errorf("%w: %s", ErrProviderNotFound, providerName)
}
// Build options
options := defaultOptions()
for _, opt := range opts {
opt(&options)
}
// Create provider
provider, err := factory(model, options)
if err != nil {
return nil, err
}
return &Client{
provider: provider,
options: options,
}, nil
}
// Chat sends a prompt to the LLM and returns the response
//
// The context can be used to set timeouts or cancel the request.
//
// Example:
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// defer cancel()
//
// response, err := client.Chat(ctx, "Hello, how are you?")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(response.Content)
func (c *Client) Chat(ctx context.Context, prompt string) (*Response, error) {
if prompt == "" {
return nil, fmt.Errorf("prompt cannot be empty")
}
return c.provider.chat(ctx, prompt, c.options)
}
// ListProviders returns a list of all registered provider names
func ListProviders() []string {
mu.RLock()
defer mu.RUnlock()
names := make([]string, 0, len(providers))
for name := range providers {
names = append(names, name)
}
return names
}