Args: messages (Union[list[ChatCompletionMessageParam], list[list[ChatCompletionMessageParam]]]): Single conversation or a list of conversations. sampling_params (Optional[Union[SamplingParams, list[SamplingParams]]], optional): The sa
(
self,
messages: Union[list[Any], list[list[Any]]],
sampling_params: Optional[Union[SamplingParams, list[SamplingParams]]] = None,
use_tqdm: bool = True,
chat_template_kwargs: Optional[dict[str, Any]] = None,
chat_template: Optional[str] = None,
tools: Optional[Union[ChatCompletionToolsParam, list[ChatCompletionToolsParam]]] = None,
stream: bool = False,
)
| 214 | return outputs |
| 215 | |
| 216 | def chat( |
| 217 | self, |
| 218 | messages: Union[list[Any], list[list[Any]]], |
| 219 | sampling_params: Optional[Union[SamplingParams, list[SamplingParams]]] = None, |
| 220 | use_tqdm: bool = True, |
| 221 | chat_template_kwargs: Optional[dict[str, Any]] = None, |
| 222 | chat_template: Optional[str] = None, |
| 223 | tools: Optional[Union[ChatCompletionToolsParam, list[ChatCompletionToolsParam]]] = None, |
| 224 | stream: bool = False, |
| 225 | ): |
| 226 | """ |
| 227 | Args: |
| 228 | messages (Union[list[ChatCompletionMessageParam], list[list[ChatCompletionMessageParam]]]): |
| 229 | Single conversation or a list of conversations. |
| 230 | sampling_params (Optional[Union[SamplingParams, list[SamplingParams]]], optional): |
| 231 | The sampling parameters to use for generating the response. Defaults to None. |
| 232 | use_tqdm (bool, optional): Whether to use tqdm for the progress bar. Defaults to True. |
| 233 | chat_template_kwargs(Optional[dict[str,Any]]): Additional kwargs to pass to the chat |
| 234 | template. |
| 235 | stream (bool, optional): Whether to return a streaming iterator. Defaults to False. |
| 236 | |
| 237 | Returns: |
| 238 | If stream=False: Union[str, list[str]]: The generated response. |
| 239 | If stream=True: Iterator: An iterator that yields partial responses as they become available. |
| 240 | """ |
| 241 | |
| 242 | if not self._check_master(): |
| 243 | err_msg = f"Only master node can accept completion request, please send request to master node: {self.master_node_ip}" |
| 244 | raise ValueError(err_msg) |
| 245 | |
| 246 | if sampling_params is None: |
| 247 | sampling_params = self.default_sampling_params |
| 248 | |
| 249 | if isinstance(sampling_params, SamplingParams): |
| 250 | sampling_params_len = 1 |
| 251 | else: |
| 252 | sampling_params_len = len(sampling_params) |
| 253 | |
| 254 | if isinstance(messages, list) and isinstance(messages[0], dict): |
| 255 | messages = [messages] |
| 256 | |
| 257 | if sampling_params_len != 1 and len(messages) != sampling_params_len: |
| 258 | raise ValueError("messages and sampling_params must be the same length.") |
| 259 | |
| 260 | if chat_template is None: |
| 261 | chat_template = self.chat_template |
| 262 | |
| 263 | validated_tools = None |
| 264 | if tools is not None: |
| 265 | try: |
| 266 | validated_tools = self._validate_tools(tools) |
| 267 | except ValueError as e: |
| 268 | raise RuntimeError(f"Failed to validate 'tools' parameter in chat method: {e}") from e |
| 269 | |
| 270 | req_ids = self._add_request( |
| 271 | prompts=[{"messages": msg} for msg in messages], |
| 272 | sampling_params=sampling_params, |
| 273 | chat_template_kwargs=chat_template_kwargs, |