mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-04-30 23:32:19 +00:00
Design
This commit is contained in:
+103
-14
@@ -2,44 +2,133 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/liushuangls/go-anthropic/v2"
|
||||
)
|
||||
|
||||
func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Message, isNewChat, isAdminOrOwner bool) (string, error) {
|
||||
// ErrModelNotFound is returned when the configured Anthropic model is no longer available
|
||||
// (deprecated or removed). Callers can use errors.Is to detect this and surface an
|
||||
// actionable message to admins/owners while keeping the response vague for regular users.
|
||||
var ErrModelNotFound = errors.New("model not found or deprecated")
|
||||
|
||||
func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Message, isNewChat, isOwner, isEmojiOnly bool, username string, firstName string, lastName string, isPremium bool, languageCode string, messageTime int) (string, error) {
|
||||
// Use prompts from config
|
||||
var systemMessage string
|
||||
if isNewChat {
|
||||
systemMessage = "You are a helpful AI assistant."
|
||||
systemMessage = b.config.SystemPrompts["new_chat"]
|
||||
} else {
|
||||
systemMessage = "Continue the conversation."
|
||||
systemMessage = b.config.SystemPrompts["continue_conversation"]
|
||||
}
|
||||
|
||||
if !isAdminOrOwner {
|
||||
systemMessage += " Avoid discussing sensitive topics or providing harmful information."
|
||||
// Combine default prompt with custom instructions
|
||||
systemMessage = b.config.SystemPrompts["default"] + " " + b.config.SystemPrompts["custom_instructions"] + " " + systemMessage
|
||||
|
||||
// Handle username placeholder
|
||||
usernameValue := username
|
||||
if username == "" {
|
||||
usernameValue = "unknown" // Use "unknown" when username is not available
|
||||
}
|
||||
systemMessage = strings.ReplaceAll(systemMessage, "{username}", usernameValue)
|
||||
|
||||
// Handle firstname placeholder
|
||||
firstnameValue := firstName
|
||||
if firstName == "" {
|
||||
firstnameValue = "unknown" // Use "unknown" when first name is not available
|
||||
}
|
||||
systemMessage = strings.ReplaceAll(systemMessage, "{firstname}", firstnameValue)
|
||||
|
||||
// Handle lastname placeholder
|
||||
lastnameValue := lastName
|
||||
if lastName == "" {
|
||||
lastnameValue = "" // Empty string when last name is not available
|
||||
}
|
||||
systemMessage = strings.ReplaceAll(systemMessage, "{lastname}", lastnameValue)
|
||||
|
||||
// Handle language code placeholder
|
||||
langValue := languageCode
|
||||
if languageCode == "" {
|
||||
langValue = "en" // Default to English when language code is not available
|
||||
}
|
||||
systemMessage = strings.ReplaceAll(systemMessage, "{language}", langValue)
|
||||
|
||||
// Handle premium status
|
||||
premiumStatus := "regular user"
|
||||
if isPremium {
|
||||
premiumStatus = "premium user"
|
||||
}
|
||||
systemMessage = strings.ReplaceAll(systemMessage, "{premium_status}", premiumStatus)
|
||||
|
||||
// Handle time awareness
|
||||
timeObj := time.Unix(int64(messageTime), 0)
|
||||
hour := timeObj.Hour()
|
||||
var timeContext string
|
||||
if hour >= 5 && hour < 12 {
|
||||
timeContext = "morning"
|
||||
} else if hour >= 12 && hour < 18 {
|
||||
timeContext = "afternoon"
|
||||
} else if hour >= 18 && hour < 22 {
|
||||
timeContext = "evening"
|
||||
} else {
|
||||
timeContext = "night"
|
||||
}
|
||||
systemMessage = strings.ReplaceAll(systemMessage, "{time_context}", timeContext)
|
||||
|
||||
if !isOwner {
|
||||
systemMessage += " " + b.config.SystemPrompts["avoid_sensitive"]
|
||||
}
|
||||
|
||||
if isEmojiOnly {
|
||||
systemMessage += " " + b.config.SystemPrompts["respond_with_emojis"]
|
||||
}
|
||||
|
||||
// Debug logging
|
||||
InfoLogger.Printf("Sending %d messages to Anthropic", len(messages))
|
||||
for i, msg := range messages {
|
||||
for _, content := range msg.Content {
|
||||
if content.Type == anthropic.MessagesContentTypeText {
|
||||
InfoLogger.Printf("Message %d: Role=%v, Text=%v", i, msg.Role, content.Text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the roles are correct
|
||||
for i := range messages {
|
||||
if messages[i].Role == "user" {
|
||||
switch messages[i].Role {
|
||||
case anthropic.RoleUser:
|
||||
messages[i].Role = anthropic.RoleUser
|
||||
} else if messages[i].Role == "assistant" {
|
||||
case anthropic.RoleAssistant:
|
||||
messages[i].Role = anthropic.RoleAssistant
|
||||
default:
|
||||
// Default to 'user' if role is unrecognized
|
||||
messages[i].Role = anthropic.RoleUser
|
||||
}
|
||||
}
|
||||
|
||||
model := anthropic.ModelClaude3Dot5Sonnet20240620
|
||||
if !isAdminOrOwner {
|
||||
model = anthropic.ModelClaudeInstant1Dot2
|
||||
}
|
||||
model := anthropic.Model(b.config.Model)
|
||||
|
||||
resp, err := b.anthropicClient.CreateMessages(ctx, anthropic.MessagesRequest{
|
||||
Model: model,
|
||||
// Create the request
|
||||
request := anthropic.MessagesRequest{
|
||||
Model: model, // Now `model` is of type anthropic.Model
|
||||
Messages: messages,
|
||||
System: systemMessage,
|
||||
MaxTokens: 1000,
|
||||
})
|
||||
}
|
||||
|
||||
// Apply temperature if set in config
|
||||
if b.config.Temperature != nil {
|
||||
request.Temperature = b.config.Temperature
|
||||
}
|
||||
|
||||
resp, err := b.anthropicClient.CreateMessages(ctx, request)
|
||||
if err != nil {
|
||||
var apiErr *anthropic.APIError
|
||||
if errors.As(err, &apiErr) && apiErr.IsNotFoundErr() {
|
||||
return "", fmt.Errorf("%w: %s", ErrModelNotFound, b.config.Model)
|
||||
}
|
||||
return "", fmt.Errorf("error creating Anthropic message: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user