bittensor.core.stream#
Classes#
|
|
The |
Module Contents#
- class bittensor.core.stream.BTStreamingResponseModel(/, **data)[source]#
Bases:
pydantic.BaseModel
BTStreamingResponseModel()
is a Pydantic model that encapsulates the token streamer callable for Pydantic validation. It is used within theStreamingSynapse()
class to create aBTStreamingResponse()
object, which is responsible for handling the streaming of tokens.The token streamer is a callable that takes a send function and returns an awaitable. It is responsible for generating the content of the streaming response, typically by processing tokens and sending them to the client.
This model ensures that the token streamer conforms to the expected signature and provides a clear interface for passing the token streamer to the BTStreamingResponse class.
- Variables:
token_streamer – Callable[[Send], Awaitable[None]] The token streamer callable, which takes a send function (provided by the ASGI server) and returns an awaitable. It is responsible for generating the content of the streaming response.
- Parameters:
data (Any)
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- class bittensor.core.stream.StreamingSynapse(/, **data)[source]#
Bases:
bittensor.core.synapse.Synapse
,abc.ABC
The
StreamingSynapse()
class is designed to be subclassed for handling streaming responses in the Bittensor network. It provides abstract methods that must be implemented by the subclass to deserialize, process streaming responses, and extract JSON data. It also includes a method to create a streaming response object.Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- model_config#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class BTStreamingResponse(model, *, synapse=None, **kwargs)[source]#
Bases:
starlette.responses.StreamingResponse
BTStreamingResponse()
is a specialized subclass of the Starlette StreamingResponse designed to handle the streaming of tokens within the Bittensor network. It is used internally by the StreamingSynapse class to manage the response streaming process, including sending headers and calling the token streamer provided by the subclass.This class is not intended to be directly instantiated or modified by developers subclassing StreamingSynapse. Instead, it is used by the
create_streaming_response()
method to create a response object based on the token streamer provided by the subclass.Initializes the BTStreamingResponse with the given token streamer model.
- Parameters:
model (bittensor.core.stream.BTStreamingResponseModel) – A BTStreamingResponseModel instance containing the token streamer callable, which is responsible for generating the content of the response.
synapse (bittensor.core.stream.StreamingSynapse) – The response Synapse to be used to update the response headers etc.
**kwargs – Additional keyword arguments passed to the parent StreamingResponse class.
- token_streamer#
- synapse#
- async stream_response(send)[source]#
Asynchronously streams the response by sending headers and calling the token streamer.
This method is responsible for initiating the response by sending the appropriate headers, including the content type for event-streaming. It then calls the token streamer to generate the content and sends the response body to the client.
- Parameters:
send (starlette.types.Send) – A callable to send the response, provided by the ASGI server.
- async __call__(scope, receive, send)[source]#
Asynchronously calls the
stream_response method()
, allowing theBTStreamingResponse()
object to be used as an ASGI application.This method is part of the ASGI interface and is called by the ASGI server to handle the request and send the response. It delegates to the
stream_response()
method to perform the actual streaming process.- Parameters:
scope (starlette.types.Scope) – The scope of the request, containing information about the client, server, and request itself.
receive (starlette.types.Receive) – A callable to receive the request, provided by the ASGI server.
send (starlette.types.Send) – A callable to send the response, provided by the ASGI server.
- abstract process_streaming_response(response)[source]#
- Async:
- Parameters:
response (aiohttp.ClientResponse)
Abstract method that must be implemented by the subclass. This method should provide logic to handle the streaming response, such as parsing and accumulating data. It is called as the response is being streamed from the network, and should be implemented to handle the specific streaming data format and requirements of the subclass.
- Parameters:
response (aiohttp.ClientResponse) – The response object to be processed, typically containing chunks of data.
- abstract extract_response_json(response)[source]#
Abstract method that must be implemented by the subclass. This method should provide logic to extract JSON data from the response, including headers and content. It is called after the response has been processed and is responsible for retrieving structured data that can be used by the application.
- Parameters:
response (aiohttp.ClientResponse) – The response object from which to extract JSON data.
- Return type:
- create_streaming_response(token_streamer)[source]#
Creates a streaming response using the provided token streamer. This method can be used by the subclass to create a response object that can be sent back to the client. The token streamer should be implemented to generate the content of the response according to the specific requirements of the subclass.
- Parameters:
token_streamer (Callable[[starlette.types.Send], Awaitable[None]]) – A callable that takes a send function and returns an awaitable. It’s responsible for generating the content of the response.
- Returns:
The streaming response object, ready to be sent to the client.
- Return type:
BTStreamingResponse (bittensor.core.stream.StreamingSynapse.BTStreamingResponse)