项目文件夹

文件
Shelley 4eebe9fb50 feat(genai): improve provider interface with context and streaming
Breaking changes:
- Generate() and Stream() now require context.Context as first parameter
- Stream.Close() added for proper resource cleanup

Improvements:
- Proper context support for cancellation and timeouts
- Real SSE streaming for OpenAI and Gemini text generation
- Better error handling with wrapped errors and API error responses
- Thread-safe provider registry with sync.RWMutex
- New options: WithMaxTokens, WithTemperature, WithTimeout
- Stream has proper Close() method for cleanup
- Results can include Error field for per-chunk errors

Provider updates:
- OpenAI: true streaming with SSE parsing, proper HTTP client with timeout
- Gemini: true streaming with streamGenerateContent endpoint
- Default model updated to gpt-4o-mini (OpenAI) and gemini-2.0-flash (Gemini)

Co-authored-by: Shelley <shelley@exe.dev>
2026-01-27 08:42:32 +00:00

34 行
663 B
Go

package genai
import (
"context"
"sync"
)
var (
DefaultGenAI GenAI = &noopGenAI{}
defaultOnce sync.Once
)
// SetDefault sets the default GenAI provider (can only be called once).
func SetDefault(g GenAI) {
defaultOnce.Do(func() {
DefaultGenAI = g
})
}
// noopGenAI is a no-op implementation that returns errors.
type noopGenAI struct{}
func (n *noopGenAI) Generate(ctx context.Context, prompt string, opts ...Option) (*Result, error) {
return nil, ErrNoProvider
}
func (n *noopGenAI) Stream(ctx context.Context, prompt string, opts ...Option) (*Stream, error) {
return nil, ErrNoProvider
}
func (n *noopGenAI) String() string {
return "noop"
}