1 Star 0 Fork 1

python/ai-agent-imooc925

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
7.3链的高级使用-流式函数.py 957 Bytes
一键复制 编辑 原始数据 按行查看 历史
码农的世界 提交于 2025-04-18 15:15 +08:00 . Initial commit: Add project files
# 本地演示代码
# 链接: https://pan.baidu.com/s/1H_dzHjYojYlMu1tjtxuEiQ? pwd=cmbx 提取码: cmbx
# 这是一个自定义解析器,将LLM输出的标记迭代器
# 按逗号分隔转换为字符串列表
def split_into_list(input: Iterator[str]) -> Iterator[List[str]]:
# 保存部分输入直到遇到逗号
buffer = ""
for chunk in input:
# 将当前块添加到缓冲区
buffer += chunk
# 当缓冲区中有逗号时
while "," in buffer:
# 在逗号处分割缓冲区
comma_index = buffer.index(",")
# 输出逗号之前的所有内容
yield [buffer[:comma_index].strip()]
# 保存剩余部分用于下一次迭代
buffer = buffer[comma_index + 1 :]
# 输出最后一块
yield [buffer.strip()]
list_chain = str_chain | split_into_list
for chunk in list_chain.stream({"animal": "熊"}):
print(chunk, flush=True)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_76/ai-agent-imooc925.git
git@gitee.com:python_76/ai-agent-imooc925.git
python_76
ai-agent-imooc925
ai-agent-imooc925
main

搜索帮助