45 lines
895 B
Go
45 lines
895 B
Go
|
|
package llm
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"sync"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ReloadableClient struct {
|
||
|
|
mu sync.RWMutex
|
||
|
|
current Client
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewReloadableClient(current Client) *ReloadableClient {
|
||
|
|
return &ReloadableClient{current: current}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Update(next Client) {
|
||
|
|
if c == nil || next == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.mu.Lock()
|
||
|
|
defer c.mu.Unlock()
|
||
|
|
c.current = next
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Validate() error {
|
||
|
|
return c.snapshot().Validate()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) Generate(ctx context.Context, req GenerateRequest, onDelta func(string)) (*GenerateResult, error) {
|
||
|
|
return c.snapshot().Generate(ctx, req, onDelta)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *ReloadableClient) snapshot() Client {
|
||
|
|
if c == nil {
|
||
|
|
return disabledClient{reason: "llm client is nil"}
|
||
|
|
}
|
||
|
|
c.mu.RLock()
|
||
|
|
defer c.mu.RUnlock()
|
||
|
|
if c.current == nil {
|
||
|
|
return disabledClient{reason: "llm client is not initialized"}
|
||
|
|
}
|
||
|
|
return c.current
|
||
|
|
}
|