refactor(rag): update loadDocumentByID and loadFAQByID to return pointers and simplify error handling
This commit is contained in:
@@ -51,7 +51,7 @@ func (s *index) IndexDocumentByID(ctx context.Context, documentID int64) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return s.IndexDocument(ctx, document)
|
return s.IndexDocument(ctx, *document)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *index) IndexDocument(ctx context.Context, document models.KnowledgeDocument) error {
|
func (s *index) IndexDocument(ctx context.Context, document models.KnowledgeDocument) error {
|
||||||
@@ -72,7 +72,7 @@ func (s *index) IndexDocument(ctx context.Context, document models.KnowledgeDocu
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
vectors, chunkCount, err := s.runDocumentIndex(ctx, document, knowledgeBase)
|
vectors, chunkCount, err := s.runDocumentIndex(ctx, document, *knowledgeBase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
@@ -105,11 +105,11 @@ func (s *index) IndexFAQByID(ctx context.Context, faqID int64) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
knowledgeBase, err := s.loadFAQKnowledgeBase(faq)
|
knowledgeBase, err := s.loadFAQKnowledgeBase(*faq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
if err := s.runFAQIndex(ctx, faq, knowledgeBase); err != nil {
|
if err := s.runFAQIndex(ctx, *faq, *knowledgeBase); err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
if err := s.markFAQIndexIndexed(faq.ID); err != nil {
|
if err := s.markFAQIndexIndexed(faq.ID); err != nil {
|
||||||
|
|||||||
@@ -10,37 +10,37 @@ import (
|
|||||||
"github.com/mlogclub/simple/sqls"
|
"github.com/mlogclub/simple/sqls"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *index) loadDocumentByID(documentID int64) (models.KnowledgeDocument, error) {
|
func (s *index) loadDocumentByID(documentID int64) (*models.KnowledgeDocument, error) {
|
||||||
document := repositories.KnowledgeDocumentRepository.Get(sqls.DB(), documentID)
|
document := repositories.KnowledgeDocumentRepository.Get(sqls.DB(), documentID)
|
||||||
if document == nil {
|
if document == nil {
|
||||||
return models.KnowledgeDocument{}, fmt.Errorf("document not found: %d", documentID)
|
return nil, fmt.Errorf("document not found: %d", documentID)
|
||||||
}
|
}
|
||||||
return *document, nil
|
return document, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *index) loadFAQByID(faqID int64) (models.KnowledgeFAQ, error) {
|
func (s *index) loadFAQByID(faqID int64) (*models.KnowledgeFAQ, error) {
|
||||||
faq := repositories.KnowledgeFAQRepository.Get(sqls.DB(), faqID)
|
faq := repositories.KnowledgeFAQRepository.Get(sqls.DB(), faqID)
|
||||||
if faq == nil {
|
if faq == nil {
|
||||||
return models.KnowledgeFAQ{}, fmt.Errorf("faq not found: %d", faqID)
|
return nil, fmt.Errorf("faq not found: %d", faqID)
|
||||||
}
|
}
|
||||||
return *faq, nil
|
return faq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *index) loadDocumentKnowledgeBase(document models.KnowledgeDocument) (models.KnowledgeBase, error) {
|
func (s *index) loadDocumentKnowledgeBase(document models.KnowledgeDocument) (*models.KnowledgeBase, error) {
|
||||||
knowledgeBase := repositories.KnowledgeBaseRepository.Get(sqls.DB(), document.KnowledgeBaseID)
|
knowledgeBase := repositories.KnowledgeBaseRepository.Get(sqls.DB(), document.KnowledgeBaseID)
|
||||||
if knowledgeBase == nil {
|
if knowledgeBase == nil {
|
||||||
return models.KnowledgeBase{}, fmt.Errorf("knowledge base not found: %d", document.KnowledgeBaseID)
|
return nil, fmt.Errorf("knowledge base not found: %d", document.KnowledgeBaseID)
|
||||||
}
|
}
|
||||||
return *knowledgeBase, nil
|
return knowledgeBase, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *index) loadFAQKnowledgeBase(faq models.KnowledgeFAQ) (models.KnowledgeBase, error) {
|
func (s *index) loadFAQKnowledgeBase(faq models.KnowledgeFAQ) (*models.KnowledgeBase, error) {
|
||||||
knowledgeBase := repositories.KnowledgeBaseRepository.Get(sqls.DB(), faq.KnowledgeBaseID)
|
knowledgeBase := repositories.KnowledgeBaseRepository.Get(sqls.DB(), faq.KnowledgeBaseID)
|
||||||
if knowledgeBase == nil {
|
if knowledgeBase == nil {
|
||||||
return models.KnowledgeBase{}, fmt.Errorf("knowledge base not found: %d", faq.KnowledgeBaseID)
|
return nil, fmt.Errorf("knowledge base not found: %d", faq.KnowledgeBaseID)
|
||||||
}
|
}
|
||||||
if knowledgeBase.KnowledgeType != string(enums.KnowledgeBaseTypeFAQ) {
|
if knowledgeBase.KnowledgeType != string(enums.KnowledgeBaseTypeFAQ) {
|
||||||
return models.KnowledgeBase{}, fmt.Errorf("knowledge base %d is not faq type", knowledgeBase.ID)
|
return nil, fmt.Errorf("knowledge base %d is not faq type", knowledgeBase.ID)
|
||||||
}
|
}
|
||||||
return *knowledgeBase, nil
|
return knowledgeBase, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user