首次提交:初始化项目代码

This commit is contained in:
sunct
2026-07-31 14:44:48 +08:00
commit a4fe393571
119 changed files with 29105 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
package chat
import (
"context"
"fmt"
"resume-platform/internal/model"
"resume-platform/internal/prompt"
"resume-platform/internal/repository"
"resume-platform/internal/service/ai"
"github.com/google/uuid"
)
type ChatService interface {
Chat(ctx context.Context, userID string, question string) (string, error)
GetChatHistory(ctx context.Context, userID string) ([]*model.ChatHistory, error)
DeleteChatHistory(ctx context.Context, userID string) error
}
type chatService struct {
repo repository.ResumeRepository
ai *ai.AIService
}
func NewChatService(repo repository.ResumeRepository, ai *ai.AIService) ChatService {
return &chatService{repo: repo, ai: ai}
}
func (s *chatService) Chat(ctx context.Context, userID string, question string) (string, error) {
chunks, err := s.repo.GetDocumentChunksByUserID(userID)
if err != nil {
return "", err
}
if len(chunks) == 0 {
return s.ai.Generate(ctx, question)
}
var chunkList []model.ChunkWithScore
for _, chunk := range chunks {
chunkList = append(chunkList, model.ChunkWithScore{
Content: chunk.Content,
Metadata: chunk.Embedding,
})
}
var contextStr string
for i, chunk := range chunkList[:min(5, len(chunkList))] {
contextStr += fmt.Sprintf("[文档片段%d]\n%s\n\n", i+1, chunk.Content)
}
promptText := prompt.BuildDocumentAnalysisPrompt(contextStr, question)
answer, err := s.ai.Generate(ctx, promptText)
if err != nil {
return "", err
}
history := &model.ChatHistory{
ID: uuid.New().String(),
UserID: userID,
Question: question,
Answer: answer,
Context: contextStr,
}
s.repo.CreateChatHistory(history)
return answer, nil
}
func (s *chatService) GetChatHistory(ctx context.Context, userID string) ([]*model.ChatHistory, error) {
return s.repo.GetChatHistoryByUserID(userID)
}
func (s *chatService) DeleteChatHistory(ctx context.Context, userID string) error {
return nil
}
+34
View File
@@ -0,0 +1,34 @@
package chat
import (
"context"
"resume-platform/internal/agent"
"resume-platform/internal/ai"
)
type IMService struct {
aiProvider ai.Provider
resumeAgent *agent.ResumeAgent
}
func NewIMService(provider ai.Provider, resumeAgent *agent.ResumeAgent) *IMService {
return &IMService{aiProvider: provider, resumeAgent: resumeAgent}
}
func (s *IMService) GetAIProvider() ai.Provider {
return s.aiProvider
}
func (s *IMService) GetResumeAgent() *agent.ResumeAgent {
return s.resumeAgent
}
func (s *IMService) GenerateWithAgent(ctx context.Context, query string) (string, error) {
if s.resumeAgent != nil {
return s.resumeAgent.Run(ctx, query)
}
if s.aiProvider != nil {
return s.aiProvider.Generate(ctx, query)
}
return "", nil
}