Skip to content

Commit 7fdb0fa

Browse files
committed
docs(examples): show iterating a search stream
1 parent 7095aa6 commit 7fdb0fa

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

‎examples/async_conversation_streaming.py‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
import typesense
1212

13-
from typesense.types.document import MessageChunk, StreamConfigBuilder
13+
from typesense.types.document import (
14+
MessageChunk,
15+
SearchResponse,
16+
StreamConfigBuilder,
17+
)
1418

1519

1620
def require_env(name: str) -> str:
@@ -107,24 +111,32 @@ async def main() -> None:
107111
}
108112
documents = client.collections[documents_collection].documents
109113

110-
@stream.on_chunk
114+
# Iterate over the answer as it is generated, then read the search response.
115+
async with await documents.search_stream(search_parameters) as answer_stream:
116+
async for chunk in answer_stream:
117+
print(chunk["message"], end="", flush=True)
118+
response = await answer_stream.get_final_response()
119+
print("\n---\nFound", response["found"], "documents")
120+
121+
# Or pass callbacks to search(), which returns the search response at the end.
122+
stream_config: StreamConfigBuilder[SearchResponse[typing.Any]] = (
123+
StreamConfigBuilder()
124+
)
125+
126+
@stream_config.on_chunk
111127
def on_chunk(chunk: MessageChunk) -> None:
112128
print(chunk["message"], end="", flush=True)
113129

114-
@stream.on_complete
115-
def on_complete(response: dict) -> None:
130+
@stream_config.on_complete
131+
def on_complete(response: SearchResponse[typing.Any]) -> None:
116132
print("\n---\nComplete response keys:", response.keys())
117133

118-
await client.collections["streaming_docs"].documents.search(
134+
await documents.search(
119135
{
120-
"q": "What is this document about?",
121-
"query_by": "embedding",
122-
"exclude_fields": "embedding",
136+
**search_parameters,
123137
"conversation": True,
124-
"prefix": False,
125138
"conversation_stream": True,
126-
"conversation_model_id": conversation_model["id"],
127-
"stream_config": stream,
139+
"stream_config": stream_config,
128140
}
129141
)
130142
finally:

‎examples/conversation_streaming.py‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from operator import truediv
21
import os
32
import sys
43
import typing
@@ -10,7 +9,11 @@
109

1110
import typesense
1211

13-
from typesense.types.document import MessageChunk, StreamConfigBuilder
12+
from typesense.types.document import (
13+
MessageChunk,
14+
SearchResponse,
15+
StreamConfigBuilder,
16+
)
1417

1518

1619
def require_env(name: str) -> str:
@@ -117,25 +120,21 @@ def require_env(name: str) -> str:
117120
stream_config: StreamConfigBuilder[SearchResponse[typing.Any]] = StreamConfigBuilder()
118121

119122

120-
@stream.on_chunk
123+
@stream_config.on_chunk
121124
def on_chunk(chunk: MessageChunk) -> None:
122125
print(chunk["message"], end="", flush=True)
123126

124127

125-
@stream.on_complete
126-
def on_complete(response: dict) -> None:
128+
@stream_config.on_complete
129+
def on_complete(response: SearchResponse[typing.Any]) -> None:
127130
print("\n---\nComplete response keys:", response.keys())
128131

129132

130133
client.collections[documents_collection].documents.search(
131134
{
132-
"q": "What is this document about?",
133-
"query_by": "embedding",
134-
"exclude_fields": "embedding",
135+
**search_parameters,
135136
"conversation": True,
136-
"prefix": False,
137137
"conversation_stream": True,
138-
"conversation_model_id": conversation_model["id"],
139-
"stream_config": stream,
138+
"stream_config": stream_config,
140139
}
141140
)

0 commit comments

Comments
 (0)