48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package backend
|
|
|
|
import "context"
|
|
|
|
// Noop is a programmable test backend that satisfies the Backend interface
|
|
// without making any network calls. By default it returns an empty response
|
|
// immediately. Set ChatStreamFunc to override behavior.
|
|
type Noop struct {
|
|
name string
|
|
model string
|
|
|
|
// ChatStreamFunc, if non-nil, replaces the default ChatStream behavior.
|
|
// Use this to simulate blocking, errors, or custom responses.
|
|
ChatStreamFunc func(ctx context.Context, messages []Message, tools []Tool, params GenerationParams) (<-chan StreamEvent, error)
|
|
}
|
|
|
|
// NewNoop creates a Noop backend with the given name and model.
|
|
func NewNoop(name, model string) *Noop {
|
|
if name == "" {
|
|
name = "noop"
|
|
}
|
|
if model == "" {
|
|
model = "noop"
|
|
}
|
|
return &Noop{name: name, model: model}
|
|
}
|
|
|
|
func (n *Noop) Name() string { return n.name }
|
|
func (n *Noop) DefaultModel() string { return n.model }
|
|
func (n *Noop) Model() string { return n.model }
|
|
func (n *Noop) SetModel(model string) { n.model = model }
|
|
|
|
func (n *Noop) ContextLength(_ context.Context) int { return 128000 }
|
|
|
|
func (n *Noop) Models(_ context.Context) []string {
|
|
return []string{n.model}
|
|
}
|
|
|
|
func (n *Noop) ChatStream(ctx context.Context, messages []Message, tools []Tool, params GenerationParams) (<-chan StreamEvent, error) {
|
|
if n.ChatStreamFunc != nil {
|
|
return n.ChatStreamFunc(ctx, messages, tools, params)
|
|
}
|
|
ch := make(chan StreamEvent, 1)
|
|
ch <- StreamEvent{Done: true, StopReason: "end_turn"}
|
|
close(ch)
|
|
return ch, nil
|
|
}
|