aibow/gemini.go

50 lines
1.4 KiB
Go

package main
import (
"context"
"fmt"
"google.golang.org/genai"
)
type chatTurn struct {
Role string `json:"role"` // "user" or "model"
Text string `json:"text"`
}
const (
modelGemma = "gemma-4-26b-a4b-it"
modelGemini = "gemini-flash-latest"
)
func selectModel(unlockCode, unlockSecret string) string {
if unlockSecret != "" && unlockCode == unlockSecret {
return modelGemini
}
return modelGemma
}
func buildSystemInstruction(videoTitle, videoDescription string) *genai.Content {
prompt := fmt.Sprintf(
"あなたはユーザーと一緒に動画を見ている友達です。タメ口で短く、実況や相槌のように反応してください。\n"+
"今見ている動画:\nタイトル: %s\n概要: %s",
videoTitle, videoDescription,
)
return genai.NewContentFromText(prompt, genai.RoleUser)
}
func chatReply(ctx context.Context, client *genai.Client, model, videoTitle, videoDescription string, history []chatTurn, message string) (string, error) {
contents := make([]*genai.Content, 0, len(history)+1)
for _, h := range history {
contents = append(contents, genai.NewContentFromText(h.Text, genai.Role(h.Role)))
}
contents = append(contents, genai.NewContentFromText(message, genai.RoleUser))
resp, err := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{
SystemInstruction: buildSystemInstruction(videoTitle, videoDescription),
})
if err != nil {
return "", err
}
return resp.Text(), nil
}