2023-08-29 03:37:45 +08:00
|
|
|
import json
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
from flask import current_app
|
2023-08-29 03:37:45 +08:00
|
|
|
from langchain.embeddings import OpenAIEmbeddings
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
from core.embedding.cached_embedding import CacheEmbedding
|
|
|
|
from core.index.keyword_table_index.keyword_table_index import KeywordTableIndex, KeywordTableConfig
|
|
|
|
from core.index.vector_index.vector_index import VectorIndex
|
2023-08-12 00:57:00 +08:00
|
|
|
from core.model_providers.model_factory import ModelFactory
|
2023-08-29 03:37:45 +08:00
|
|
|
from core.model_providers.models.embedding.openai_embedding import OpenAIEmbedding
|
|
|
|
from core.model_providers.models.entity.model_params import ModelKwargs
|
|
|
|
from core.model_providers.models.llm.openai_model import OpenAIModel
|
|
|
|
from core.model_providers.providers.openai_provider import OpenAIProvider
|
2023-06-25 16:49:14 +08:00
|
|
|
from models.dataset import Dataset
|
2023-08-29 03:37:45 +08:00
|
|
|
from models.provider import Provider, ProviderType
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class IndexBuilder:
|
|
|
|
@classmethod
|
|
|
|
def get_index(cls, dataset: Dataset, indexing_technique: str, ignore_high_quality_check: bool = False):
|
|
|
|
if indexing_technique == "high_quality":
|
|
|
|
if not ignore_high_quality_check and dataset.indexing_technique != 'high_quality':
|
|
|
|
return None
|
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
embedding_model = ModelFactory.get_embedding_model(
|
2023-08-18 17:37:31 +08:00
|
|
|
tenant_id=dataset.tenant_id,
|
|
|
|
model_provider_name=dataset.embedding_model_provider,
|
|
|
|
model_name=dataset.embedding_model
|
2023-06-25 16:49:14 +08:00
|
|
|
)
|
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
embeddings = CacheEmbedding(embedding_model)
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
return VectorIndex(
|
|
|
|
dataset=dataset,
|
|
|
|
config=current_app.config,
|
|
|
|
embeddings=embeddings
|
|
|
|
)
|
|
|
|
elif indexing_technique == "economy":
|
|
|
|
return KeywordTableIndex(
|
|
|
|
dataset=dataset,
|
|
|
|
config=KeywordTableConfig(
|
|
|
|
max_keywords_per_chunk=10
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2023-08-29 03:37:45 +08:00
|
|
|
raise ValueError('Unknown indexing technique')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_default_high_quality_index(cls, dataset: Dataset):
|
|
|
|
embeddings = OpenAIEmbeddings(openai_api_key=' ')
|
|
|
|
return VectorIndex(
|
|
|
|
dataset=dataset,
|
|
|
|
config=current_app.config,
|
|
|
|
embeddings=embeddings
|
|
|
|
)
|