Update types of base config models for pydantic 2.0

This commit is contained in:
sabaimran 2023-11-17 23:08:52 -08:00
parent f688529150
commit 3328a41f08
3 changed files with 25 additions and 22 deletions

View file

@ -63,7 +63,7 @@ async def update(
request: Request,
files: list[UploadFile],
force: bool = False,
t: Optional[Union[state.SearchType, str]] = None,
t: Optional[Union[state.SearchType, str]] = state.SearchType.All,
client: Optional[str] = None,
user_agent: Optional[str] = Header(None),
referer: Optional[str] = Header(None),
@ -182,13 +182,16 @@ def configure_content(
files: Optional[dict[str, dict[str, str]]],
search_models: SearchModels,
regenerate: bool = False,
t: Optional[state.SearchType] = None,
t: Optional[state.SearchType] = state.SearchType.All,
full_corpus: bool = True,
user: KhojUser = None,
) -> tuple[Optional[ContentIndex], bool]:
content_index = ContentIndex()
success = True
if t is not None and t in [type.value for type in state.SearchType]:
t = state.SearchType(t)
if t is not None and not t.value in [type.value for type in state.SearchType]:
logger.warning(f"🚨 Invalid search type: {t}")
return None, False

View file

@ -229,7 +229,7 @@ def collate_results(hits, image_names, output_directory, image_files_url, count=
# Add the image metadata to the results
results += [
SearchResponse.parse_obj(
SearchResponse.model_validate(
{
"entry": f"{image_files_url}/{target_image_name}",
"score": f"{hit['score']:.9f}",
@ -237,7 +237,7 @@ def collate_results(hits, image_names, output_directory, image_files_url, count=
"image_score": f"{hit['image_score']:.9f}",
"metadata_score": f"{hit['metadata_score']:.9f}",
},
"corpus_id": hit["corpus_id"],
"corpus_id": str(hit["corpus_id"]),
}
)
]

View file

@ -14,7 +14,7 @@ from khoj.utils.helpers import to_snake_case_from_dash
class ConfigBase(BaseModel):
class Config:
alias_generator = to_snake_case_from_dash
allow_population_by_field_name = True
populate_by_name = True
def __getitem__(self, item):
return getattr(self, item)
@ -29,8 +29,8 @@ class TextConfigBase(ConfigBase):
class TextContentConfig(ConfigBase):
input_files: Optional[List[Path]]
input_filter: Optional[List[str]]
input_files: Optional[List[Path]] = None
input_filter: Optional[List[str]] = None
index_heading_entries: Optional[bool] = False
@ -50,31 +50,31 @@ class NotionContentConfig(ConfigBase):
class ImageContentConfig(ConfigBase):
input_directories: Optional[List[Path]]
input_filter: Optional[List[str]]
input_directories: Optional[List[Path]] = None
input_filter: Optional[List[str]] = None
embeddings_file: Path
use_xmp_metadata: bool
batch_size: int
class ContentConfig(ConfigBase):
org: Optional[TextContentConfig]
image: Optional[ImageContentConfig]
markdown: Optional[TextContentConfig]
pdf: Optional[TextContentConfig]
plaintext: Optional[TextContentConfig]
github: Optional[GithubContentConfig]
notion: Optional[NotionContentConfig]
org: Optional[TextContentConfig] = None
image: Optional[ImageContentConfig] = None
markdown: Optional[TextContentConfig] = None
pdf: Optional[TextContentConfig] = None
plaintext: Optional[TextContentConfig] = None
github: Optional[GithubContentConfig] = None
notion: Optional[NotionContentConfig] = None
class ImageSearchConfig(ConfigBase):
encoder: str
encoder_type: Optional[str]
model_directory: Optional[Path]
encoder_type: Optional[str] = None
model_directory: Optional[Path] = None
class SearchConfig(ConfigBase):
image: Optional[ImageSearchConfig]
image: Optional[ImageSearchConfig] = None
class OpenAIProcessorConfig(ConfigBase):
@ -95,7 +95,7 @@ class ConversationProcessorConfig(ConfigBase):
class ProcessorConfig(ConfigBase):
conversation: Optional[ConversationProcessorConfig]
conversation: Optional[ConversationProcessorConfig] = None
class AppConfig(ConfigBase):
@ -113,8 +113,8 @@ class FullConfig(ConfigBase):
class SearchResponse(ConfigBase):
entry: str
score: float
cross_score: Optional[float]
additional: Optional[dict]
cross_score: Optional[float] = None
additional: Optional[dict] = None
corpus_id: str