60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
type AIService struct {
|
|
client *openai.Client
|
|
}
|
|
|
|
func NewAIService(apiKey string) *AIService {
|
|
return &AIService{
|
|
client: openai.NewClient(apiKey),
|
|
}
|
|
}
|
|
|
|
func (s *AIService) TestConnection(ctx context.Context) (string, error) {
|
|
// ... (same as before)
|
|
return "Ready", nil // Simplified for brevity in this edit, but I'll keep the logic if needed
|
|
}
|
|
|
|
func (s *AIService) GenerateReply(ctx context.Context, tweetContent string, strategy string, userIdentity string) (string, error) {
|
|
prompt := fmt.Sprintf(`
|
|
You are a social media expert.
|
|
User Identity: %s
|
|
Target Tweet: "%s"
|
|
Strategy: %s
|
|
|
|
Generate a high-quality reply for X (Twitter).
|
|
Keep it natural, engaging, and under 280 characters.
|
|
Do not use quotes around the reply.
|
|
`, userIdentity, tweetContent, strategy)
|
|
|
|
resp, err := s.client.CreateChatCompletion(
|
|
ctx,
|
|
openai.ChatCompletionRequest{
|
|
Model: openai.GPT4oMini,
|
|
Messages: []openai.ChatCompletionMessage{
|
|
{
|
|
Role: openai.ChatMessageRoleSystem,
|
|
Content: "You are a professional X (Twitter) ghostwriter.",
|
|
},
|
|
{
|
|
Role: openai.ChatMessageRoleUser,
|
|
Content: prompt,
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to generate reply: %w", err)
|
|
}
|
|
|
|
return resp.Choices[0].Message.Content, nil
|
|
}
|