Skip to content

Commit ca6def8

Browse files
committed
feat(types): add natural language query support to search parameters
- add `NLLanguageParameters` type with nl query configuration options - add `LLMResponse` and `ParsedNLQuery` types for nl query responses - extend `SearchParameters` to include natural language parameters - add `parsed_nl_query` field to `SearchResponse` for nl query metadata
1 parent cb27c74 commit ca6def8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/typesense/types/document.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,23 @@ class CachingParameters(typing.TypedDict):
569569
cache_ttl: typing.NotRequired[int]
570570

571571

572+
class NLLanguageParameters(typing.TypedDict):
573+
"""
574+
Parameters regarding [caching search results](https://typesense.org/docs/26.0/api/search.html#caching-parameters).
575+
576+
Attributes:
577+
nl_query_prompt_cache_ttl (int): The duration (in seconds) that determines how long the schema prompts are cached.
578+
nl_query (bool): Whether to use natural language in the query or not.
579+
nl_model_id (str): The ID of the natural language model to use for the query.
580+
nl_query_debug (bool): Whether to return the raw LLM response or not.
581+
"""
582+
583+
nl_query_prompt_cache_ttl: typing.NotRequired[int]
584+
nl_query: typing.NotRequired[bool]
585+
nl_model_id: typing.NotRequired[str]
586+
nl_query_debug: typing.NotRequired[bool]
587+
588+
572589
class SearchParameters(
573590
RequiredSearchParameters,
574591
QueryParameters,
@@ -580,6 +597,7 @@ class SearchParameters(
580597
ResultsParameters,
581598
TypoToleranceParameters,
582599
CachingParameters,
600+
NLLanguageParameters,
583601
):
584602
"""Parameters for searching documents."""
585603

@@ -823,6 +841,38 @@ class Conversation(typing.TypedDict):
823841
query: str
824842

825843

844+
class LLMResponse(typing.TypedDict):
845+
"""
846+
Schema for a raw LLM response.
847+
848+
Attributes:
849+
content (str): Content of the LLM response.
850+
extraction_method (str): Extraction method of the LLM response (e.g. "regex").
851+
model (str): Model used to generate the response.
852+
"""
853+
854+
content: str
855+
extraction_method: str
856+
model: str
857+
858+
859+
class ParsedNLQuery(typing.TypedDict):
860+
"""
861+
Schema for a parsed natural language query.
862+
863+
Attributes:
864+
parse_time_ms (int): Parse time in milliseconds.
865+
generated_params (SearchParameters): Generated parameters.
866+
augmented_params (SearchParameters): Augmented parameters.
867+
llm_response (LLMResponse): Raw LLM response.
868+
"""
869+
870+
parse_time_ms: int
871+
generated_params: SearchParameters
872+
augmented_params: SearchParameters
873+
llm_response: typing.NotRequired[LLMResponse]
874+
875+
826876
class SearchResponse(typing.Generic[TDoc], typing.TypedDict):
827877
"""
828878
Schema for a search response.
@@ -838,6 +888,7 @@ class SearchResponse(typing.Generic[TDoc], typing.TypedDict):
838888
hits (list[Hit[TDoc]]): List of hits in the search results.
839889
grouped_hits (list[GroupedHit[TDoc]]): List of grouped hits in the search results.
840890
conversation (Conversation): Conversation in the search results.
891+
parsed_nl_query (ParsedNLQuery): Information about the natural language query
841892
"""
842893

843894
facet_counts: typing.List[SearchResponseFacetCountSchema]
@@ -850,6 +901,7 @@ class SearchResponse(typing.Generic[TDoc], typing.TypedDict):
850901
hits: typing.List[Hit[TDoc]]
851902
grouped_hits: typing.NotRequired[typing.List[GroupedHit[TDoc]]]
852903
conversation: typing.NotRequired[Conversation]
904+
parsed_nl_query: typing.NotRequired[ParsedNLQuery]
853905

854906

855907
class DeleteSingleDocumentParameters(typing.TypedDict):

0 commit comments

Comments
 (0)