Skip to content

Commit

Permalink
feat: make the bot more interesting.
Browse files Browse the repository at this point in the history
  • Loading branch information
vgjm committed Mar 27, 2024
1 parent 0a9deca commit fe2fd64
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
53 changes: 50 additions & 3 deletions geminiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,50 @@ func New(apiKey string) (*GeminiClient, error) {

func (gc *GeminiClient) SingleQuestion(ask string) (string, error) {
model := gc.client.GenerativeModel("gemini-pro")
model.SetMaxOutputTokens(300)
resp, err := model.GenerateContent(gc.ctx, genai.Text(ask))

// Set all harm block to none
// https://ai.google.dev/docs/safety_setting_gemini?hl=zh-cn#safety-settings
model.SafetySettings = []*genai.SafetySetting{
{
Category: genai.HarmCategoryHarassment,
Threshold: genai.HarmBlockNone,
},
{
Category: genai.HarmCategoryHateSpeech,
Threshold: genai.HarmBlockNone,
},
{
Category: genai.HarmCategorySexuallyExplicit,
Threshold: genai.HarmBlockNone,
},
{
Category: genai.HarmCategoryDangerousContent,
Threshold: genai.HarmBlockNone,
},
}

// Initial a bot with specified role
cs := model.StartChat()
cs.History = []*genai.Content{
{
Parts: []genai.Part{
genai.Text("请想象你是《瑞克和莫蒂》里面的瑞克与我对话。"),
},
Role: "user",
},
{
Parts: []genai.Part{
genai.Text("你好,我是瑞克·桑切斯,全宇宙最聪明的男人。"),
},
Role: "model",
},
}

resp, err := cs.SendMessage(gc.ctx, genai.Text(ask))
if err != nil {
return "Gemini已麻。", err
}

var text string
for _, cand := range resp.Candidates {
if cand.Content != nil {
Expand All @@ -38,8 +77,16 @@ func (gc *GeminiClient) SingleQuestion(ask string) (string, error) {
}
}
if text == "" {
text = "我擦,我不好说。"
switch resp.PromptFeedback.BlockReason {
case genai.BlockReasonUnspecified:
text = "你挺让人无语的。"
case genai.BlockReasonSafety:
text = "底线!"
case genai.BlockReasonOther:
text = "我擦,我不好说。"
}
}

return text, nil
}
func (gc *GeminiClient) Close() error {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func main() {
}
}
default:
log.Info().Msgf("Unsupported message content: %T\n", e.Message)
log.Debug().Msgf("Unsupported message content: %T\n", e.Message)
}
default:
log.Info().Msgf("Unsupported message: %T\n", event)
log.Debug().Msgf("Unsupported message: %T\n", event)
}
}
})
Expand Down

0 comments on commit fe2fd64

Please sign in to comment.