[docs]classChatGLM(LLM):"""ChatGLM LLM service. Example: .. code-block:: python from langchain_community.llms import ChatGLM endpoint_url = ( "http://127.0.0.1:8000" ) ChatGLM_llm = ChatGLM( endpoint_url=endpoint_url ) """endpoint_url:str="http://127.0.0.1:8000/""""Endpoint URL to use."""model_kwargs:Optional[dict]=None"""Keyword arguments to pass to the model."""max_token:int=20000"""Max token allowed to pass to the model."""temperature:float=0.1"""LLM model temperature from 0 to 10."""history:List[List]=[]"""History of the conversation"""top_p:float=0.7"""Top P for nucleus sampling from 0 to 1"""with_history:bool=False"""Whether to use history or not"""@propertydef_llm_type(self)->str:return"chat_glm"@propertydef_identifying_params(self)->Mapping[str,Any]:"""Get the identifying parameters."""_model_kwargs=self.model_kwargsor{}return{**{"endpoint_url":self.endpoint_url},**{"model_kwargs":_model_kwargs},}def_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:"""Call out to a ChatGLM LLM inference endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Example: .. code-block:: python response = chatglm_llm.invoke("Who are you?") """_model_kwargs=self.model_kwargsor{}# HTTP headers for authorizationheaders={"Content-Type":"application/json"}payload={"prompt":prompt,"temperature":self.temperature,"history":self.history,"max_length":self.max_token,"top_p":self.top_p,}payload.update(_model_kwargs)payload.update(kwargs)logger.debug(f"ChatGLM payload: {payload}")# call apitry:response=requests.post(self.endpoint_url,headers=headers,json=payload)exceptrequests.exceptions.RequestExceptionase:raiseValueError(f"Error raised by inference endpoint: {e}")logger.debug(f"ChatGLM response: {response}")ifresponse.status_code!=200:raiseValueError(f"Failed with response: {response}")try:parsed_response=response.json()# Check if response content does existsifisinstance(parsed_response,dict):content_keys="response"ifcontent_keysinparsed_response:text=parsed_response[content_keys]else:raiseValueError(f"No content in response : {parsed_response}")else:raiseValueError(f"Unexpected response type: {parsed_response}")exceptrequests.exceptions.JSONDecodeErrorase:raiseValueError(f"Error raised during decoding response from inference endpoint: {e}."f"\nResponse: {response.text}")ifstopisnotNone:text=enforce_stop_tokens(text,stop)ifself.with_history:self.history=parsed_response["history"]returntext