-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadapter.go
More file actions
120 lines (101 loc) · 3.73 KB
/
adapter.go
File metadata and controls
120 lines (101 loc) · 3.73 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
118
119
120
package llmberjack
import (
"context"
"net/http"
"reflect"
"github.com/checkmarble/llmberjack/internal"
"github.com/cockroachdb/errors"
)
const (
defaultProvider = "__DEFAULT__"
)
// Llm defines the interface that all LLM providers must implement.
// It provides a contract for initializing, managing context,
// and performing chat completions with different language models.
type Llm interface {
// Init initializes the LLM provider with the given adapter configuration.
// It is called once when the provider is added to the adapter.
Init(llm internal.Adapter) error
// ResetContext clears the conversation history for the specific LLM provider.
// This allows starting a new conversation without re-initializing the provider.
ResetThread(*ThreadId)
// CopyThread copies all history from the provided thread into a new, discrete one.
CopyThread(*ThreadId) *ThreadId
// CloseThread deletes a thread and associated resources.
CloseThread(*ThreadId)
// ChatCompletion sends a chat completion request to the LLM provider.
// It takes a context, the adapter's internal configuration, and a Requester
// to retrieve the request.
ChatCompletion(context.Context, internal.Adapter, Requester) (*InnerResponse, error)
// RequestOptionsType returns the reflect.Type of the provider-specific
// request options struct. This is used for type checking and reflection
// when processing custom request options.
RequestOptionsType() reflect.Type
}
// Llmberjack is the main entrypoint for interacting with different LLM providers.
// It provides a unified interface to send requests and receive responses.
type Llmberjack struct {
providers map[string]Llm
defaultProvider Llm
httpClient *http.Client
defaultModel string
}
// New creates a new Llmberjack with the given options.
// It initializes the specified LLM provider and returns a configured adapter.
//
// Example usage:
//
// adapter, err := llmberjack.New(
// llmberjack.WithDefaultProvider(provider),
// llmberjack.WithDefaultModel("gpt-4"),
// llmberjack.WithApiKey("...")
// )
func New(opts ...llmOption) (*Llmberjack, error) {
llm := Llmberjack{
providers: make(map[string]Llm),
}
for _, opt := range opts {
opt(&llm)
}
for name, provider := range llm.providers {
if err := provider.Init(llm); err != nil {
return nil, errors.Wrapf(err, "could not initialize LLM provider '%s'", name)
}
}
return &llm, nil
}
// ResetContext clears the conversation history maintained by the adapter.
// This is useful when you want to start a new conversation without creating a
// new adapter instance. This also clears the systems instructions.
// If called without arguments, will clear the history of the default provider,
// otherwise, it accepts variadic provider names for which to clear the history.
// func (llm *Llmberjack) ResetThreads(threadIds ...*ThreadId) {
// for _, thread := range threadIds {
// thread.provider.ResetThread(thread)
// }
// }
// GetProvider retrieves an LLM provider based on the given provider name.
// It accepts the provider requested in a specific request, which will override
// the default provider. If the provider argument is nil, it will return the
// configured default provider.
func (llm *Llmberjack) GetProvider(requestProvider *string) (Llm, error) {
if llm.defaultProvider == nil {
return nil, errors.New("no provider was configured")
}
provider := llm.defaultProvider
if requestProvider != nil {
p, ok := llm.providers[*requestProvider]
if !ok {
return nil, errors.Newf("unknown provider '%s'", *requestProvider)
}
provider = p
}
return provider, nil
}
// Llmberjack implementation of Adapter
func (llm Llmberjack) DefaultModel() string {
return llm.defaultModel
}
func (llm Llmberjack) HttpClient() *http.Client {
return llm.httpClient
}