[docs]classChatMaritalk(BaseChatModel):"""`MariTalk` Chat models API. This class allows interacting with the MariTalk chatbot API. To use it, you must provide an API key either through the constructor. Example: .. code-block:: python from langchain_community.chat_models import ChatMaritalk chat = ChatMaritalk(api_key="your_api_key_here") """api_key:str"""Your MariTalk API key."""model:str"""Chose one of the available models: - `sabia-2-medium` - `sabia-2-small` - `sabia-2-medium-2024-03-13` - `sabia-2-small-2024-03-13` - `maritalk-2024-01-08` (deprecated)"""temperature:float=Field(default=0.7,gt=0.0,lt=1.0)"""Run inference with this temperature. Must be in the closed interval [0.0, 1.0]."""max_tokens:int=Field(default=512,gt=0)"""The maximum number of tokens to generate in the reply."""do_sample:bool=Field(default=True)"""Whether or not to use sampling; use `True` to enable."""top_p:float=Field(default=0.95,gt=0.0,lt=1.0)"""Nucleus sampling parameter controlling the size of the probability mass considered for sampling."""@propertydef_llm_type(self)->str:"""Identifies the LLM type as 'maritalk'."""return"maritalk"
[docs]defparse_messages_for_model(self,messages:List[BaseMessage])->List[Dict[str,Union[str,List[Union[str,Dict[Any,Any]]]]]]:""" Parses messages from LangChain's format to the format expected by the MariTalk API. Parameters: messages (List[BaseMessage]): A list of messages in LangChain format to be parsed. Returns: A list of messages formatted for the MariTalk API. """parsed_messages=[]formessageinmessages:ifisinstance(message,HumanMessage):role="user"elifisinstance(message,AIMessage):role="assistant"elifisinstance(message,SystemMessage):role="system"parsed_messages.append({"role":role,"content":message.content})returnparsed_messages
def_call(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:""" Sends the parsed messages to the MariTalk API and returns the generated response or an error message. This method makes an HTTP POST request to the MariTalk API with the provided messages and other parameters. If the request is successful and the API returns a response, this method returns a string containing the answer. If the request is rate-limited or encounters another error, it returns a string with the error message. Parameters: messages (List[BaseMessage]): Messages to send to the model. stop (Optional[List[str]]): Tokens that will signal the model to stop generating further tokens. Returns: str: If the API call is successful, returns the answer. If an error occurs (e.g., rate limiting), returns a string describing the error. """url="https://chat.maritaca.ai/api/chat/inference"headers={"authorization":f"Key {self.api_key}"}stopping_tokens=stopifstopisnotNoneelse[]parsed_messages=self.parse_messages_for_model(messages)data={"messages":parsed_messages,"model":self.model,"do_sample":self.do_sample,"max_tokens":self.max_tokens,"temperature":self.temperature,"top_p":self.top_p,"stopping_tokens":stopping_tokens,**kwargs,}response=requests.post(url,json=data,headers=headers)ifresponse.ok:returnresponse.json().get("answer","No answer found")else:raiseMaritalkHTTPError(response)asyncdef_acall(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->str:""" Asynchronously sends the parsed messages to the MariTalk API and returns the generated response or an error message. This method makes an HTTP POST request to the MariTalk API with the provided messages and other parameters using async I/O. If the request is successful and the API returns a response, this method returns a string containing the answer. If the request is rate-limited or encounters another error, it returns a string with the error message. """try:importhttpxurl="https://chat.maritaca.ai/api/chat/inference"headers={"authorization":f"Key {self.api_key}"}stopping_tokens=stopifstopisnotNoneelse[]parsed_messages=self.parse_messages_for_model(messages)data={"messages":parsed_messages,"model":self.model,"do_sample":self.do_sample,"max_tokens":self.max_tokens,"temperature":self.temperature,"top_p":self.top_p,"stopping_tokens":stopping_tokens,**kwargs,}asyncwithhttpx.AsyncClient()asclient:response=awaitclient.post(url,json=data,headers=headers,timeout=None)ifresponse.status_code==200:returnresponse.json().get("answer","No answer found")else:raiseMaritalkHTTPError(response)# type: ignore[arg-type]exceptImportError:raiseImportError("Could not import httpx python package. ""Please install it with `pip install httpx`.")def_stream(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->Iterator[ChatGenerationChunk]:headers={"Authorization":f"Key {self.api_key}"}stopping_tokens=stopifstopisnotNoneelse[]parsed_messages=self.parse_messages_for_model(messages)data={"messages":parsed_messages,"model":self.model,"do_sample":self.do_sample,"max_tokens":self.max_tokens,"temperature":self.temperature,"top_p":self.top_p,"stopping_tokens":stopping_tokens,"stream":True,**kwargs,}response=requests.post("https://chat.maritaca.ai/api/chat/inference",data=json.dumps(data),headers=headers,stream=True,)ifresponse.ok:forlineinresponse.iter_lines():ifline.startswith(b"data: "):response_data=line.replace(b"data: ",b"").decode("utf-8")ifresponse_data:parsed_data=json.loads(response_data)if"text"inparsed_data:delta=parsed_data["text"]chunk=ChatGenerationChunk(message=AIMessageChunk(content=delta))ifrun_manager:run_manager.on_llm_new_token(delta,chunk=chunk)yieldchunkelse:raiseMaritalkHTTPError(response)asyncdef_astream(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->AsyncIterator[ChatGenerationChunk]:try:importhttpxheaders={"Authorization":f"Key {self.api_key}"}stopping_tokens=stopifstopisnotNoneelse[]parsed_messages=self.parse_messages_for_model(messages)data={"messages":parsed_messages,"model":self.model,"do_sample":self.do_sample,"max_tokens":self.max_tokens,"temperature":self.temperature,"top_p":self.top_p,"stopping_tokens":stopping_tokens,"stream":True,**kwargs,}asyncwithhttpx.AsyncClient()asclient:asyncwithclient.stream("POST","https://chat.maritaca.ai/api/chat/inference",data=json.dumps(data),# type: ignore[arg-type]headers=headers,timeout=None,)asresponse:ifresponse.status_code==200:asyncforlineinresponse.aiter_lines():ifline.startswith("data: "):response_data=line.replace("data: ","")ifresponse_data:parsed_data=json.loads(response_data)if"text"inparsed_data:delta=parsed_data["text"]chunk=ChatGenerationChunk(message=AIMessageChunk(content=delta))ifrun_manager:awaitrun_manager.on_llm_new_token(delta,chunk=chunk)yieldchunkelse:raiseMaritalkHTTPError(response)# type: ignore[arg-type]exceptImportError:raiseImportError("Could not import httpx python package. ""Please install it with `pip install httpx`.")def_generate(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->ChatResult:output_str=self._call(messages,stop=stop,run_manager=run_manager,**kwargs)message=AIMessage(content=output_str)generation=ChatGeneration(message=message)returnChatResult(generations=[generation])asyncdef_agenerate(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->ChatResult:output_str=awaitself._acall(messages,stop=stop,run_manager=run_manager,**kwargs)message=AIMessage(content=output_str)generation=ChatGeneration(message=message)returnChatResult(generations=[generation])@propertydef_identifying_params(self)->Dict[str,Any]:""" Identifies the key parameters of the chat model for logging or tracking purposes. Returns: A dictionary of the key configuration parameters. """return{"model":self.model,"temperature":self.temperature,"top_p":self.top_p,"max_tokens":self.max_tokens,}