Documentation
¶
Overview ¶
Package google provides ChatModel adapter for Google Gemini API.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChatModel ¶
type ChatModel struct {
// contains filtered or unexported fields
}
ChatModel implements model.ChatModel for Google's Gemini API.
Provides access to Gemini models (gemini-pro, gemini-pro-vision) with:
- Safety filter handling
- Tool/function calling support
- Context cancellation
- User-friendly error messages for blocked content
Example usage:
apiKey := os.Getenv("GOOGLE_API_KEY")
m := google.NewChatModel(apiKey, "gemini-1.5-flash")
messages := []model.Message{
{Role: model.RoleUser, Content: "What is the capital of France?"},
}
out, err := m.Chat(ctx, messages, nil)
if err != nil {
var safetyErr *SafetyFilterError
if errors.As(err, &safetyErr) {
log.Printf("Content blocked: %s", safetyErr.Category)
return
}
log.Fatal(err)
}
fmt.Println(out.Text)
func NewChatModel ¶
NewChatModel creates a new Google ChatModel.
Parameters:
- apiKey: Google API key (get from https://makersuite.google.com/app/apikey)
- modelName: Model to use (e.g., "gemini-1.5-flash"). Empty string uses default.
Returns a ChatModel configured for Gemini API.
Example:
model := google.NewChatModel(apiKey, "gemini-1.5-flash")
func (*ChatModel) Chat ¶
func (m *ChatModel) Chat(ctx context.Context, messages []model.Message, tools []model.ToolSpec) (model.ChatOut, error)
Chat implements the model.ChatModel interface.
Sends messages to Google's Gemini API and returns the response. Handles safety filter blocks with descriptive errors.
Returns:
- ChatOut with Text and/or ToolCalls
- Error for authentication failures, safety blocks, or API errors
type SafetyFilterError ¶
type SafetyFilterError struct {
// contains filtered or unexported fields
}
SafetyFilterError represents a Google safety filter block.
Provides information about why content was blocked:
- Reason: Why the block occurred (e.g., "SAFETY")
- Category: Which safety category was triggered
Use errors.As to check for this error type:
var safetyErr *google.SafetyFilterError
if errors.As(err, &safetyErr) {
log.Printf("Content blocked: %s", safetyErr.Category())
}
func (*SafetyFilterError) Category ¶
func (e *SafetyFilterError) Category() string
Category returns the safety category that triggered the block.
func (*SafetyFilterError) Error ¶
func (e *SafetyFilterError) Error() string
Error implements the error interface.
func (*SafetyFilterError) Reason ¶
func (e *SafetyFilterError) Reason() string
Reason returns why the content was blocked.