Initial commit
This commit is contained in:
59
server/internal/service/ai_service.go
Normal file
59
server/internal/service/ai_service.go
Normal file
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
27
server/internal/service/user_service.go
Normal file
27
server/internal/service/user_service.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/zs/InsightReply/internal/model"
|
||||
"github.com/zs/InsightReply/internal/repository"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
repo *repository.UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(repo *repository.UserRepository) *UserService {
|
||||
return &UserService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *UserService) Register(email string, identity string) (*model.User, error) {
|
||||
user := &model.User{
|
||||
Email: email,
|
||||
IdentityLabel: identity,
|
||||
}
|
||||
err := s.repo.Create(user)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) GetUser(email string) (*model.User, error) {
|
||||
return s.repo.GetByEmail(email)
|
||||
}
|
||||
Reference in New Issue
Block a user