Skip to content

Commit 6adb74e

Browse files
authored
Merge pull request #122 from kc3hack/BE-48
[BE-48] Geminiの出力ランダム化
2 parents 16b30e7 + 4bbe7f8 commit 6adb74e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

backend/repositories/hint_repository.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type IHintRepository interface {
1515
BookmarkRound(id uint) error
1616
GetRandomBookmarkedRound() (*models.Round, error)
1717
GetAllBookmarkedRounds() ([]models.Round, error)
18+
GetRecentAnswers(limit int) ([]string, error)
1819
}
1920

2021
type HintRepository struct {
@@ -79,3 +80,17 @@ func (r *HintRepository) GetAllBookmarkedRounds() ([]models.Round, error) {
7980
}
8081
return rounds, nil
8182
}
83+
84+
func (r *HintRepository) GetRecentAnswers(limit int) ([]string, error) {
85+
var rounds []models.Round
86+
if err := r.db.Order("created_at desc").Limit(limit).Find(&rounds).Error; err != nil {
87+
return nil, err
88+
}
89+
answers := make([]string, 0, len(rounds))
90+
for _, round := range rounds {
91+
if len(round.Answers) > 0 {
92+
answers = append(answers, round.Answers[0])
93+
}
94+
}
95+
return answers, nil
96+
}

backend/services/hint_service.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515
"google.golang.org/genai"
1616
)
1717

18-
const MinRoundAnswerRevealDuration = 90 * time.Second
18+
const (
19+
MinRoundAnswerRevealDuration = 90 * time.Second
20+
RecentAnswersLimit = 30
21+
)
1922

2023
var (
2124
ErrRoundNotFound = errors.New("round not found")
@@ -54,7 +57,23 @@ func (s *HintService) StartGame() (*dto.StartGameResult, error) {
5457
return nil, fmt.Errorf("failed to create Gemini client: %w", err)
5558
}
5659

57-
const prompt = `
60+
// 過去の出題済みお題を取得(直近30件)
61+
pastAnswers, err := s.repository.GetRecentAnswers(RecentAnswersLimit)
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to get recent answers: %w", err)
64+
}
65+
66+
excludeSection := ""
67+
if len(pastAnswers) > 0 {
68+
excludeSection = fmt.Sprintf(`
69+
70+
# 禁止お題
71+
以下のお題は過去に出題済みなので、絶対に使わないでください:
72+
%s
73+
`, strings.Join(pastAnswers, "、"))
74+
}
75+
76+
const basePrompt = `
5877
# Role
5978
あなたは京都(上品・皮肉)と大阪(効率・本音)の個性を完璧に描き分ける脚本家であり、厳密なJSONデータを出力するシステムです。
6079
@@ -170,10 +189,12 @@ func (s *HintService) StartGame() (*dto.StartGameResult, error) {
170189
},
171190
}
172191

192+
fullPrompt := basePrompt + excludeSection
193+
173194
res, err := client.Models.GenerateContent(
174195
ctx,
175196
"gemini-2.5-flash",
176-
genai.Text(prompt),
197+
genai.Text(fullPrompt),
177198
config,
178199
)
179200
if err != nil {

0 commit comments

Comments
 (0)