[docs]classChatGeneration(Generation):"""A single chat generation output. A subclass of Generation that represents the response from a chat model that generates chat messages. The `message` attribute is a structured representation of the chat message. Most of the time, the message will be of type `AIMessage`. Users working with chat models will usually access information via either `AIMessage` (returned from runnable interfaces) or `LLMResult` (available via callbacks). """message:BaseMessage"""The message output by the chat model."""type:Literal["ChatGeneration"]="ChatGeneration"# type: ignore[assignment]"""Type is used exclusively for serialization purposes."""@computed_field# type: ignore[prop-decorator]@propertydeftext(self)->str:"""Set the text attribute to be the contents of the message."""text_=""ifisinstance(self.message.content,str):text_=self.message.content# Assumes text in content blocks in OpenAI format.# Uses first text block.elifisinstance(self.message.content,list):forblockinself.message.content:ifisinstance(block,str):text_=blockbreakifisinstance(block,dict)and"text"inblock:text_=block["text"]breakreturntext_
[docs]classChatGenerationChunk(ChatGeneration):"""ChatGeneration chunk. ChatGeneration chunks can be concatenated with other ChatGeneration chunks. """message:BaseMessageChunk"""The message chunk output by the chat model."""type:Literal["ChatGenerationChunk"]="ChatGenerationChunk"# type: ignore[assignment]"""Type is used exclusively for serialization purposes."""def__add__(self,other:Union[ChatGenerationChunk,list[ChatGenerationChunk]])->ChatGenerationChunk:"""Concatenate two ChatGenerationChunks. Args: other: The other ChatGenerationChunk or list of ChatGenerationChunks to concatenate. """ifisinstance(other,ChatGenerationChunk):generation_info=merge_dicts(self.generation_infoor{},other.generation_infoor{},)returnChatGenerationChunk(message=self.message+other.message,generation_info=generation_infoorNone,)ifisinstance(other,list)andall(isinstance(x,ChatGenerationChunk)forxinother):generation_info=merge_dicts(self.generation_infoor{},*[chunk.generation_infoforchunkinotherifchunk.generation_info],)returnChatGenerationChunk(message=self.message+[chunk.messageforchunkinother],generation_info=generation_infoorNone,)msg=f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'"raiseTypeError(msg)
[docs]defmerge_chat_generation_chunks(chunks:list[ChatGenerationChunk],)->Union[ChatGenerationChunk,None]:"""Merge a list of ChatGenerationChunks into a single ChatGenerationChunk."""ifnotchunks:returnNoneiflen(chunks)==1:returnchunks[0]returnchunks[0]+chunks[1:]