11import json
22from collections .abc import Iterator
33from datetime import datetime , timezone
4- from typing import Any , Optional , Union
4+ from typing import Any
55
66from haystack import component , default_from_dict , default_to_dict , logging
77from haystack .components .generators .utils import _convert_streaming_chunks_to_chat_message
@@ -192,15 +192,15 @@ class LlamaCppChatGenerator:
192192 def __init__ (
193193 self ,
194194 model : str ,
195- n_ctx : Optional [ int ] = 0 ,
196- n_batch : Optional [ int ] = 512 ,
197- model_kwargs : Optional [ dict [str , Any ]] = None ,
198- generation_kwargs : Optional [ dict [str , Any ]] = None ,
195+ n_ctx : int | None = 0 ,
196+ n_batch : int | None = 512 ,
197+ model_kwargs : dict [str , Any ] | None = None ,
198+ generation_kwargs : dict [str , Any ] | None = None ,
199199 * ,
200- tools : Optional [ ToolsType ] = None ,
201- streaming_callback : Optional [ StreamingCallbackT ] = None ,
202- chat_handler_name : Optional [ str ] = None ,
203- model_clip_path : Optional [ str ] = None ,
200+ tools : ToolsType | None = None ,
201+ streaming_callback : StreamingCallbackT | None = None ,
202+ chat_handler_name : str | None = None ,
203+ model_clip_path : str | None = None ,
204204 ):
205205 """
206206 :param model: The path of a quantized model for text generation, for example, "zephyr-7b-beta.Q4_0.gguf".
@@ -238,7 +238,7 @@ def __init__(
238238
239239 _check_duplicate_tool_names (flatten_tools_or_toolsets (tools ))
240240
241- handler : Optional [ Llava15ChatHandler ] = None
241+ handler : Llava15ChatHandler | None = None
242242 # Validate multimodal requirements
243243 if chat_handler_name is not None :
244244 if model_clip_path is None :
@@ -256,7 +256,7 @@ def __init__(
256256 self .n_batch = n_batch
257257 self .model_kwargs = model_kwargs
258258 self .generation_kwargs = generation_kwargs
259- self ._model : Optional [ Llama ] = None
259+ self ._model : Llama | None = None
260260 self .tools = tools
261261 self .streaming_callback = streaming_callback
262262 self .chat_handler_name = chat_handler_name
@@ -324,10 +324,10 @@ def from_dict(cls, data: dict[str, Any]) -> "LlamaCppChatGenerator":
324324 def run (
325325 self ,
326326 messages : list [ChatMessage ],
327- generation_kwargs : Optional [ dict [str , Any ]] = None ,
327+ generation_kwargs : dict [str , Any ] | None = None ,
328328 * ,
329- tools : Optional [ ToolsType ] = None ,
330- streaming_callback : Optional [ StreamingCallbackT ] = None ,
329+ tools : ToolsType | None = None ,
330+ streaming_callback : StreamingCallbackT | None = None ,
331331 ) -> dict [str , list [ChatMessage ]]:
332332 """
333333 Run the text generation model on the given list of ChatMessages.
@@ -435,8 +435,8 @@ def _handle_streaming_response(
435435
436436 if chunk .get ("choices" ) and len (chunk ["choices" ]) > 0 :
437437 choice = chunk ["choices" ][0 ]
438- delta : Union [ ChatCompletionStreamResponseDelta , ChatCompletionStreamResponseDeltaEmpty , dict ] = (
439- choice . get ( "delta" , {})
438+ delta : ChatCompletionStreamResponseDelta | ChatCompletionStreamResponseDeltaEmpty | dict = choice . get (
439+ "delta" , {}
440440 )
441441
442442 finish_reason = choice .get ("finish_reason" )
0 commit comments