agentchat.utils
gather_usage_summary
def gather_usage_summary(
agents: List[Agent]) -> Dict[Dict[str, Dict], Dict[str, Dict]]
收集所有代理的使用摘要。
参数:
agents
- (列表): 代理人列表。
返回:
dictionary
- A dictionary containing two keys:- "usage_including_cached_inference": 关于总使用量的成本信息,包括缓存推理中的令牌。
- "usage_excluding_cached_inference": 关于使用的token成本信息,不包括缓存中的tokens。不大于"usage_including_cached_inference"。
示例:
{
"usage_including_cached_inference" : {
"total_cost": 0.0006090000000000001,
"gpt-35-turbo": {
"cost": 0.0006090000000000001,
"prompt_tokens": 242,
"completion_tokens": 123,
"total_tokens": 365
},
},
"usage_excluding_cached_inference" : {
"total_cost": 0.0006090000000000001,
"gpt-35-turbo": {
"cost": 0.0006090000000000001,
"prompt_tokens": 242,
"completion_tokens": 123,
"total_tokens": 365
},
}
}
注意:
如果没有代理产生任何成本(没有客户),那么usage_including_cached_inference和usage_excluding_cached_inference将会是{'total_cost': 0}
。
从内容中解析标签
def parse_tags_from_content(
tag: str,
content: Union[str, List[Dict[str,
Any]]]) -> List[Dict[str, Dict[str, str]]]
解析消息内容中的HTML样式标签。
解析是通过查找文本中与HTML标签格式匹配的模式来完成的。要解析的标签作为函数的参数指定。函数在文本中查找该标签并提取其内容。标签的内容是标签内部的所有内容,位于开始和结束尖括号之间。内容可以是单个字符串或一组属性-值对。
示例:
http://example.com/image.png> -> [{"tag": "img", "attr": {"src": "http://example.com/image.png"}, "match": re.Match}]
[{"tag"
- "audio", "attr": {"text": "你好我是机器人", "prompt": "whisper"}, "match": re.Match}]
参数:
tag
str - 要解析的HTML样式标签。content
Union[str, List[Dict[str, Any]]] - 要解析的消息内容。可以是字符串或内容项的列表。
返回:
List[Dict[str, str]]: 一个字典列表,其中每个字典代表一个解析后的标签。每个字典包含三个键值对:'type'表示标签,'attr'是解析后的属性字典,'match'是正则表达式匹配对象。
引发:
ValueError
- 如果内容不是字符串或列表。