github.com/weaviate/weaviate@v1.24.6/modules/qna-openai/additional/answer/answer_result.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package answer 13 14 import ( 15 "context" 16 "errors" 17 "strings" 18 19 "github.com/weaviate/weaviate/entities/models" 20 "github.com/weaviate/weaviate/entities/moduletools" 21 "github.com/weaviate/weaviate/entities/search" 22 qnamodels "github.com/weaviate/weaviate/modules/qna-openai/additional/models" 23 ) 24 25 func (p *AnswerProvider) findAnswer(ctx context.Context, in []search.Result, params *Params, limit *int, argumentModuleParams map[string]interface{}, cfg moduletools.ClassConfig) ([]search.Result, error) { 26 if len(in) == 0 { 27 return in, nil 28 } 29 question := p.paramsHelper.GetQuestion(argumentModuleParams["ask"]) 30 if question == "" { 31 return in, errors.New("empty question") 32 } 33 properties := p.paramsHelper.GetProperties(argumentModuleParams["ask"]) 34 35 for i := range in { 36 textProperties := map[string]string{} 37 schema := in[i].Object().Properties.(map[string]interface{}) 38 for property, value := range schema { 39 if p.containsProperty(property, properties) { 40 if valueString, ok := value.(string); ok && len(valueString) > 0 { 41 textProperties[property] = valueString 42 } 43 } 44 } 45 46 var texts []string 47 for _, value := range textProperties { 48 texts = append(texts, value) 49 } 50 text := strings.Join(texts, " ") 51 if len(text) == 0 { 52 return in, errors.New("empty content") 53 } 54 55 answer, err := p.qna.Answer(ctx, text, question, cfg) 56 if err != nil { 57 return in, err 58 } 59 60 ap := in[i].AdditionalProperties 61 if ap == nil { 62 ap = models.AdditionalProperties{} 63 } 64 propertyName, startPos, endPos := p.findProperty(answer.Answer, textProperties) 65 ap["answer"] = &qnamodels.Answer{ 66 Result: answer.Answer, 67 Property: propertyName, 68 StartPosition: startPos, 69 EndPosition: endPos, 70 HasAnswer: answer.Answer != nil, 71 } 72 73 in[i].AdditionalProperties = ap 74 } 75 76 return in, nil 77 } 78 79 func (p *AnswerProvider) containsProperty(property string, properties []string) bool { 80 if len(properties) == 0 { 81 return true 82 } 83 for i := range properties { 84 if properties[i] == property { 85 return true 86 } 87 } 88 return false 89 } 90 91 func (p *AnswerProvider) findProperty(answer *string, textProperties map[string]string) (*string, int, int) { 92 if answer == nil { 93 return nil, 0, 0 94 } 95 lowercaseAnswer := strings.ToLower(*answer) 96 if len(lowercaseAnswer) > 0 { 97 for property, value := range textProperties { 98 lowercaseValue := strings.ToLower(strings.ReplaceAll(value, "\n", " ")) 99 if strings.Contains(lowercaseValue, lowercaseAnswer) { 100 startIndex := strings.Index(lowercaseValue, lowercaseAnswer) 101 return &property, startIndex, startIndex + len(lowercaseAnswer) 102 } 103 } 104 } 105 propertyNotFound := "" 106 return &propertyNotFound, 0, 0 107 }