Skip to main content

A modular, async, composable AI pipeline library for building conversational agents and content processing pipelines

Project description

llm-pipelines

一个模块化、异步、可组合的 AI Pipeline 库,用于构建:

  • 对话代理 (Conversational agents)
  • 内容处理管道 (Content processing pipelines)
  • 多步推理系统 (Multi-step reasoning)

核心设计原则

1. 统一的流式抽象 - 一切皆流

所有数据都是 AsyncIterable[StreamItem],输入是流,输出也是流。

2. 可组合性优先 - 乐高式构建

通过运算符重载(+ 链式,// 并行),Processor 像乐高积木一样自由组合。

3. 双层处理抽象 - 灵活与性能兼得

  • Processor: 处理整个流,适合需要上下文的操作
  • ItemProcessor: 处理单个 item,自动并发执行,性能最优

安装

基础安装:

pip install llm-pipelines

包含 AI 集成(OpenAI SDK):

pip install "llm-pipelines[openai]"

开发安装:

pip install -e ".[dev,openai]"

快速开始

from llm_pipelines import Processor, StreamItem, stream_content
import asyncio

# 创建简单的 Processor
class UppercaseProcessor(Processor):
    async def call(self, content):
        async for item in content:
            yield StreamItem(
                data=item.data.upper(),
                role=item.role,
                mimetype=item.mimetype
            )

# 使用 Processor
async def main():
    processor = UppercaseProcessor()
    input_stream = stream_content([
        StreamItem(data="hello", role="user")
    ])

    async for item in processor(input_stream):
        print(item.data)  # 输出: HELLO

asyncio.run(main())

组合 Processors

# 链式组合 (+)
pipeline = processor1 + processor2 + processor3

# 并行组合 (//)
parallel_pipeline = item_processor1 // item_processor2

AI 集成

支持 OpenAI 兼容的 API(OpenAI、DeepSeek、Qwen 等):

from llm_pipelines import StreamingChatProcessor, StreamItem, stream_content

# 流式 AI 对话
chat = StreamingChatProcessor(
    api_key="your-api-key",
    base_url="https://api.openai.com/v1",
    model="gpt-4"
)

input_stream = stream_content([
    StreamItem(data="你好!", role="user")
])

# 实时输出
async for chunk in chat(input_stream):
    print(chunk.data, end="", flush=True)

查看 examples/ai_chat_example.py 获取更多 AI 集成示例。

Context 管理

统一的异步任务管理,确保资源正确清理:

from llm_pipelines import context, create_task

async with context():
    # 所有在此创建的任务会被自动追踪
    task = create_task(some_coroutine(), name="my_task")
    result = await task
# 退出时自动清理所有未完成的任务

查看 examples/context_example.py 获取更多 Context 管理示例。

项目状态

版本 0.3.0 - Context 管理已完成

已实现:

  • StreamItem 数据模型
  • Processor 和 ItemProcessor 抽象
  • 组合运算符 (+, //)
  • Stream 工具函数
  • 装饰器支持
  • StreamingChatProcessor - 流式 AI 调用
  • ChatCompletionProcessor - 批量 AI 调用
  • OpenAI 兼容 API 支持
  • Context 管理 - 统一任务管理和自动清理

开发

运行测试:

pytest

类型检查:

mypy src/llm_pipelines

代码格式化:

ruff check src/ tests/

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_pipelines-0.3.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_pipelines-0.3.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file llm_pipelines-0.3.0.tar.gz.

File metadata

  • Download URL: llm_pipelines-0.3.0.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for llm_pipelines-0.3.0.tar.gz
Algorithm Hash digest
SHA256 40b4a51817732e7fd796bd617e21d351df0c151f4f6ddab53b8c27c28c288064
MD5 551a1477dddf88ba28e9d2067f9b22c2
BLAKE2b-256 49cb3fbed45faaf96a6bd6bef2d158f2ab14b4438f5355887d9d8ee0a1868035

See more details on using hashes here.

File details

Details for the file llm_pipelines-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: llm_pipelines-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for llm_pipelines-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97476737a0cca69e4d8216050dc446b3fda9687f7e897a75652786855affbed8
MD5 8ce5e0ce0e4ff7e5e6cf2ea81c2651a9
BLAKE2b-256 5b2a8a805bc5382341cd90e1e767ca477c3932d5cf064b5537884cd6ba03b9b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page