[docs]classUsageMetadata(TypedDict):"""Usage metadata for a message, such as token counts. This is a standard representation of token usage that is consistent across models. Example: .. code-block:: python { "input_tokens": 10, "output_tokens": 20, "total_tokens": 30 } """input_tokens:int"""Count of input (or prompt) tokens."""output_tokens:int"""Count of output (or completion) tokens."""total_tokens:int"""Total token count."""
[docs]classAIMessage(BaseMessage):"""Message from an AI. AIMessage is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields (e.g., tool calls, usage metadata) added by the LangChain framework. """example:bool=False"""Use to denote that a message is part of an example conversation. At the moment, this is ignored by most models. Usage is discouraged. """tool_calls:List[ToolCall]=[]"""If provided, tool calls associated with the message."""invalid_tool_calls:List[InvalidToolCall]=[]"""If provided, tool calls with parsing errors associated with the message."""usage_metadata:Optional[UsageMetadata]=None"""If provided, usage metadata for a message, such as token counts. This is a standard representation of token usage that is consistent across models. """type:Literal["ai"]="ai""""The type of the message (used for deserialization). Defaults to "ai"."""def__init__(self,content:Union[str,List[Union[str,Dict]]],**kwargs:Any)->None:"""Pass in content as positional arg. Args: content: The content of the message. kwargs: Additional arguments to pass to the parent class. """super().__init__(content=content,**kwargs)@classmethoddefget_lc_namespace(cls)->List[str]:"""Get the namespace of the langchain object. Returns: The namespace of the langchain object. Defaults to ["langchain", "schema", "messages"]. """return["langchain","schema","messages"]@propertydeflc_attributes(self)->Dict:"""Attrs to be serialized even if they are derived from other init args."""return{"tool_calls":self.tool_calls,"invalid_tool_calls":self.invalid_tool_calls,}@root_validator(pre=True)def_backwards_compat_tool_calls(cls,values:dict)->dict:check_additional_kwargs=notany(values.get(k)forkin("tool_calls","invalid_tool_calls","tool_call_chunks"))ifcheck_additional_kwargsand(raw_tool_calls:=values.get("additional_kwargs",{}).get("tool_calls")):try:ifissubclass(cls,AIMessageChunk):# type: ignorevalues["tool_call_chunks"]=default_tool_chunk_parser(raw_tool_calls)else:parsed_tool_calls,parsed_invalid_tool_calls=default_tool_parser(raw_tool_calls)values["tool_calls"]=parsed_tool_callsvalues["invalid_tool_calls"]=parsed_invalid_tool_callsexceptException:pass# Ensure "type" is properly set on all tool call-like dicts.iftool_calls:=values.get("tool_calls"):updated:List=[]fortcintool_calls:updated.append(create_tool_call(**{k:vfork,vintc.items()ifk!="type"}))values["tool_calls"]=updatedifinvalid_tool_calls:=values.get("invalid_tool_calls"):updated=[]fortcininvalid_tool_calls:updated.append(create_invalid_tool_call(**{k:vfork,vintc.items()ifk!="type"}))values["invalid_tool_calls"]=updatediftool_call_chunks:=values.get("tool_call_chunks"):updated=[]fortcintool_call_chunks:updated.append(create_tool_call_chunk(**{k:vfork,vintc.items()ifk!="type"}))values["tool_call_chunks"]=updatedreturnvalues
[docs]defpretty_repr(self,html:bool=False)->str:"""Return a pretty representation of the message. Args: html: Whether to return an HTML-formatted string. Defaults to False. Returns: A pretty representation of the message. """base=super().pretty_repr(html=html)lines=[]def_format_tool_args(tc:Union[ToolCall,InvalidToolCall])->List[str]:lines=[f" {tc.get('name','Tool')} ({tc.get('id')})",f" Call ID: {tc.get('id')}",]iftc.get("error"):lines.append(f" Error: {tc.get('error')}")lines.append(" Args:")args=tc.get("args")ifisinstance(args,str):lines.append(f" {args}")elifisinstance(args,dict):forarg,valueinargs.items():lines.append(f" {arg}: {value}")returnlinesifself.tool_calls:lines.append("Tool Calls:")fortcinself.tool_calls:lines.extend(_format_tool_args(tc))ifself.invalid_tool_calls:lines.append("Invalid Tool Calls:")foritcinself.invalid_tool_calls:lines.extend(_format_tool_args(itc))return(base.strip()+"\n"+"\n".join(lines)).strip()
AIMessage.update_forward_refs()
[docs]classAIMessageChunk(AIMessage,BaseMessageChunk):"""Message chunk from an AI."""# Ignoring mypy re-assignment here since we're overriding the value# to make sure that the chunk variant can be discriminated from the# non-chunk variant.type:Literal["AIMessageChunk"]="AIMessageChunk"# type: ignore"""The type of the message (used for deserialization). Defaults to "AIMessageChunk"."""tool_call_chunks:List[ToolCallChunk]=[]"""If provided, tool call chunks associated with the message."""@classmethoddefget_lc_namespace(cls)->List[str]:"""Get the namespace of the langchain object. Returns: The namespace of the langchain object. Defaults to ["langchain", "schema", "messages"]. """return["langchain","schema","messages"]@propertydeflc_attributes(self)->Dict:"""Attrs to be serialized even if they are derived from other init args."""return{"tool_calls":self.tool_calls,"invalid_tool_calls":self.invalid_tool_calls,}@root_validator(pre=False,skip_on_failure=True)definit_tool_calls(cls,values:dict)->dict:"""Initialize tool calls from tool call chunks. Args: values: The values to validate. Returns: The values with tool calls initialized. Raises: ValueError: If the tool call chunks are malformed. """ifnotvalues["tool_call_chunks"]:ifvalues["tool_calls"]:values["tool_call_chunks"]=[create_tool_call_chunk(name=tc["name"],args=json.dumps(tc["args"]),id=tc["id"],index=None,)fortcinvalues["tool_calls"]]ifvalues["invalid_tool_calls"]:tool_call_chunks=values.get("tool_call_chunks",[])tool_call_chunks.extend([create_tool_call_chunk(name=tc["name"],args=tc["args"],id=tc["id"],index=None)fortcinvalues["invalid_tool_calls"]])values["tool_call_chunks"]=tool_call_chunksreturnvaluestool_calls=[]invalid_tool_calls=[]forchunkinvalues["tool_call_chunks"]:try:args_=parse_partial_json(chunk["args"])ifchunk["args"]!=""else{}ifisinstance(args_,dict):tool_calls.append(create_tool_call(name=chunk["name"]or"",args=args_,id=chunk["id"],))else:raiseValueError("Malformed args.")exceptException:invalid_tool_calls.append(create_invalid_tool_call(name=chunk["name"],args=chunk["args"],id=chunk["id"],error=None,))values["tool_calls"]=tool_callsvalues["invalid_tool_calls"]=invalid_tool_callsreturnvaluesdef__add__(self,other:Any)->BaseMessageChunk:# type: ignoreifisinstance(other,AIMessageChunk):returnadd_ai_message_chunks(self,other)elifisinstance(other,(list,tuple))andall(isinstance(o,AIMessageChunk)foroinother):returnadd_ai_message_chunks(self,*other)returnsuper().__add__(other)
[docs]defadd_ai_message_chunks(left:AIMessageChunk,*others:AIMessageChunk)->AIMessageChunk:"""Add multiple AIMessageChunks together."""ifany(left.example!=o.exampleforoinothers):raiseValueError("Cannot concatenate AIMessageChunks with different example values.")content=merge_content(left.content,*(o.contentforoinothers))additional_kwargs=merge_dicts(left.additional_kwargs,*(o.additional_kwargsforoinothers))response_metadata=merge_dicts(left.response_metadata,*(o.response_metadataforoinothers))# Merge tool call chunksifraw_tool_calls:=merge_lists(left.tool_call_chunks,*(o.tool_call_chunksforoinothers)):tool_call_chunks=[create_tool_call_chunk(name=rtc.get("name"),args=rtc.get("args"),index=rtc.get("index"),id=rtc.get("id"),)forrtcinraw_tool_calls]else:tool_call_chunks=[]# Token usageifleft.usage_metadataorany(o.usage_metadataisnotNoneforoinothers):usage_metadata_:UsageMetadata=left.usage_metadataorUsageMetadata(input_tokens=0,output_tokens=0,total_tokens=0)forotherinothers:ifother.usage_metadataisnotNone:usage_metadata_["input_tokens"]+=other.usage_metadata["input_tokens"]usage_metadata_["output_tokens"]+=other.usage_metadata["output_tokens"]usage_metadata_["total_tokens"]+=other.usage_metadata["total_tokens"]usage_metadata:Optional[UsageMetadata]=usage_metadata_else:usage_metadata=Nonereturnleft.__class__(example=left.example,content=content,additional_kwargs=additional_kwargs,tool_call_chunks=tool_call_chunks,response_metadata=response_metadata,usage_metadata=usage_metadata,id=left.id,)