Spaces:
Running
Running
File size: 5,100 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package modqnaopenai
import (
"context"
"net/http"
"os"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/weaviate/weaviate/entities/modulecapabilities"
"github.com/weaviate/weaviate/entities/moduletools"
qnaadditional "github.com/weaviate/weaviate/modules/qna-openai/additional"
qnaadditionalanswer "github.com/weaviate/weaviate/modules/qna-openai/additional/answer"
qnaask "github.com/weaviate/weaviate/modules/qna-openai/ask"
"github.com/weaviate/weaviate/modules/qna-openai/clients"
qnaadependency "github.com/weaviate/weaviate/modules/qna-openai/dependency"
"github.com/weaviate/weaviate/modules/qna-openai/ent"
)
const Name = "qna-openai"
func New() *QnAModule {
return &QnAModule{}
}
type QnAModule struct {
qna qnaClient
graphqlProvider modulecapabilities.GraphQLArguments
searcher modulecapabilities.DependencySearcher
additionalPropertiesProvider modulecapabilities.AdditionalProperties
nearTextDependencies []modulecapabilities.Dependency
askTextTransformer modulecapabilities.TextTransform
}
type qnaClient interface {
Answer(ctx context.Context, text, question string, cfg moduletools.ClassConfig) (*ent.AnswerResult, error)
MetaInfo() (map[string]interface{}, error)
}
func (m *QnAModule) Name() string {
return Name
}
func (m *QnAModule) Type() modulecapabilities.ModuleType {
return modulecapabilities.Text2TextQnA
}
func (m *QnAModule) Init(ctx context.Context,
params moduletools.ModuleInitParams,
) error {
if err := m.initAdditional(ctx, params.GetConfig().ModuleHttpClientTimeout, params.GetLogger()); err != nil {
return errors.Wrap(err, "init q/a")
}
return nil
}
func (m *QnAModule) InitExtension(modules []modulecapabilities.Module) error {
var textTransformer modulecapabilities.TextTransform
for _, module := range modules {
if module.Name() == m.Name() {
continue
}
if arg, ok := module.(modulecapabilities.TextTransformers); ok {
if arg != nil && arg.TextTransformers() != nil {
textTransformer = arg.TextTransformers()["ask"]
}
}
}
m.askTextTransformer = textTransformer
if err := m.initAskProvider(); err != nil {
return errors.Wrap(err, "init ask provider")
}
return nil
}
func (m *QnAModule) InitDependency(modules []modulecapabilities.Module) error {
nearTextDependencies := []modulecapabilities.Dependency{}
for _, module := range modules {
if module.Name() == m.Name() {
continue
}
var argument modulecapabilities.GraphQLArgument
var searcher modulecapabilities.VectorForParams
if arg, ok := module.(modulecapabilities.GraphQLArguments); ok {
if arg != nil && arg.Arguments() != nil {
if nearTextArg, ok := arg.Arguments()["nearText"]; ok {
argument = nearTextArg
}
}
}
if arg, ok := module.(modulecapabilities.Searcher); ok {
if arg != nil && arg.VectorSearches() != nil {
if nearTextSearcher, ok := arg.VectorSearches()["nearText"]; ok {
searcher = nearTextSearcher
}
}
}
if argument.ExtractFunction != nil && searcher != nil {
nearTextDependency := qnaadependency.New(module.Name(), argument, searcher)
nearTextDependencies = append(nearTextDependencies, nearTextDependency)
}
}
if len(nearTextDependencies) == 0 {
return errors.New("nearText dependecy not present")
}
m.nearTextDependencies = nearTextDependencies
if err := m.initAskSearcher(); err != nil {
return errors.Wrap(err, "init ask searcher")
}
return nil
}
func (m *QnAModule) initAdditional(ctx context.Context, timeout time.Duration,
logger logrus.FieldLogger,
) error {
openAIApiKey := os.Getenv("OPENAI_APIKEY")
openAIOrganization := os.Getenv("OPENAI_ORGANIZATION")
azureApiKey := os.Getenv("AZURE_APIKEY")
client := clients.New(openAIApiKey, openAIOrganization, azureApiKey, timeout, logger)
m.qna = client
answerProvider := qnaadditionalanswer.New(m.qna, qnaask.NewParamsHelper())
m.additionalPropertiesProvider = qnaadditional.New(answerProvider)
return nil
}
func (m *QnAModule) RootHandler() http.Handler {
// TODO: remove once this is a capability interface
return nil
}
func (m *QnAModule) MetaInfo() (map[string]interface{}, error) {
return m.qna.MetaInfo()
}
func (m *QnAModule) AdditionalProperties() map[string]modulecapabilities.AdditionalProperty {
return m.additionalPropertiesProvider.AdditionalProperties()
}
// verify we implement the modules.Module interface
var (
_ = modulecapabilities.Module(New())
_ = modulecapabilities.AdditionalProperties(New())
_ = modulecapabilities.MetaProvider(New())
)
|