[ChatStream] Creation and Initialization
Hello from the Product Development Division at Qualiteg.
In this article, we explain how to create and initialize ChatStream.
The ChatStream class is the core class of the ChatStream package. It receives a FastAPI/Starlette Request and is responsible for sending a streaming response to the client while controlling the load.
You initialize it as shown below, specifying the model, tokenizer, device, the maximum number of concurrent executions num_of_concurrent_executions, the maximum queue length max_queue_size, and the prompt class ChatPrompt.
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16)
model.to(device)
chat_stream = ChatStream(
num_of_concurrent_executions=2,
max_queue_size=5,
model=model,
tokenizer=tokenizer,
device=device,
chat_prompt_clazz=ChatPrompt,
)
List of Options
The initialization options (constructor arguments) of ChatStream are listed below.
| Parameter | Description |
|---|---|
| model | A pre-trained language model in HuggingFace format. |
| tokenizer | A tokenizer in HuggingFace format. |
| device | The execution device. Choose from "cpu" / "cuda" / "mps". |
| num_of_concurrent_executions | The number of text generation tasks the pre-trained language model runs concurrently. Default is 2. |
| max_queue_size | The maximum queue size for text generation tasks on the pre-trained language model. Default is 5. |
| too_many_request_as_http_error | Whether to return status 429 when a 'Too many requests' situation occurs. Default is False. |
| use_mock_response | Whether to return fixed phrases for testing. Since no model needs to be loaded, startup is immediate. Default is False. |
| mock_params | The type of phrase returned when use_mock_response=True: "round" / "long". Default is {"type": "round"}. |
| chat_prompt_clazz | The class that manages the prompt sent to the language model. Inherit from AbstractChatPrompt and implement a class that generates chat prompts following the etiquette of each model. |
| max_new_tokens | The maximum number of newly generated tokens. Default is 256. |
| context_len | The context size (in tokens). Default is 1024. |
| temperature | The temperature value for randomness in prediction. Default is 1.0. |
| top_k | The top-K value for sampling. Default is 50. |
| top_p | The top-P value for sampling. Default is 1.0. |
| repetition_penalty | The repetition penalty. Default is None. |
| repetition_penalty_method | The method used to calculate the repetition penalty. Default is "multiplicative". |
| add_special_tokens | A tokenizer option. Default is None. |
| request_handler | The request handler. By default, a handler that simply keeps sessions is used. |
| logger | The logging object. Default is None. |
Example:
chat_stream = ChatStream(
model=None, # Pre-trained language model in HuggingFace format
tokenizer=None, # Tokenizer in HuggingFace format
device=None, # Execution device "cpu" / "cuda" / "mps"
num_of_concurrent_executions: int = 2, # Number of text generation tasks run concurrently on the pre-trained language model
max_queue_size: int = 5, # Maximum queue size for text generation tasks on the pre-trained language model
too_many_request_as_http_error=False, # Return status 429 when a 'Too many requests' situation occurs
use_mock_response=False, # Return fixed phrases for testing. Since no model needs to be loaded, startup is immediate
mock_params={type: "round"}, # Type of phrase returned when use_mock_response=True: "round" / "long"
chat_prompt_clazz=None, # Specify the class that manages the prompt sent to the language model. Inherit from AbstractChatPrompt and implement a class that generates chat prompts following the etiquette of each model
max_new_tokens=256, # Maximum number of newly generated tokens
context_len=1024, # Context size (in tokens)
temperature=1.0, # Temperature value for randomness in prediction
top_k=50, # Top-K value for sampling
top_p=1.0, # Top-P value for sampling
repetition_penalty=None, # Repetition penalty
repetition_penalty_method="multiplicative", # Method used to calculate the repetition penalty
# Token-related processing
add_special_tokens=None, # Tokenizer option
request_handler=SimpleSessionRequestHandler(),
# Request handler. By default, a handler that simply keeps sessions is used
logger=None, # Logging object
)