RAGbits:加速GenAI应用开发

June 09, 2025

RAGbits:RAG生成式AI应用快速开发工具包

在快速发展的生成式AI领域,开发者们一直在寻找强大高效的工具,以实现他们的创新应用。RAGbits应运而生,它是deepsense.ai推出的一个开源框架,专为加速构建可靠、可扩展的生成式AI解决方案而设计,尤其擅长利用检索增强生成(RAG)技术。

什么是RAGbits?

RAGbits是一套全面的组件,旨在简化整个生成式AI应用开发周期。它采用模块化、灵活的架构,开发者可以按需集成所需组件,从而减少依赖,优化性能。该框架高度注重实际应用,为管理大型语言模型(LLM)、处理多样化数据类型以及部署复杂的RAG管道提供了强大的功能。

RAGbits的主要特性:

RAGbits凭借其强大的功能集脱颖而出,让开发者能够轻松构建复杂的AI应用:

🔨 构建可靠且可扩展的生成式AI应用

  • 灵活的LLM集成:通过LiteLLM轻松切换100多种LLM,或集成本地模型,提供无与伦比的灵活性。
  • 类型安全的LLM调用:利用Python泛型在模型交互时强制执行严格的类型安全,确保鲁棒性并减少错误。
  • 自带向量数据库:可连接Qdrant、PgVector等主流向量数据库,或轻松集成自定义解决方案。
  • 内置开发者工具:提供一套命令行工具,可直接从终端管理向量数据库、配置查询管道和测试提示词。
  • 模块化安装:仅安装必需组件,根据项目需求定制框架,提高效率。

📚 快速灵活的RAG处理

  • 广泛数据摄取:处理20多种数据格式,包括PDF、HTML、电子表格和演示文稿。利用Docling和Unstructured等强大解析器,或实现自定义解析器。
  • 复杂数据处理:通过内置视觉语言模型(VLM)支持,提取结构化内容、表格和图像。
  • 任意数据源连接:使用预构建的连接器连接S3、GCS和Azure等云存储服务,或开发自己的连接器。
  • 可扩展摄取:利用基于Ray的并行处理高效处理大型数据集,快速完成数据导入。

🚀 信心十足地部署和监控

  • 实时可观测性:通过OpenTelemetry和全面的CLI分析,跟踪应用性能并获取洞察。
  • 内置测试:在部署应用之前,使用集成promptfoo测试来验证和优化提示词。
  • 自动优化:通过系统化的流程持续评估和优化模型性能。
  • 聊天UI:部署即用型聊天机器人界面,功能齐全,包括API、数据持久化和用户反馈机制。

RAGbits入门

安装非常简单。只需一个简单的pip命令即可快速上手:

pip install ragbits

该命令会安装一个入门包,包括ragbits-core(核心工具)、ragbits-agents(用于代理系统)、ragbits-document-search(检索与摄取)、ragbits-evaluate(统一评估)、ragbits-chat(对话式AI)和ragbits-cli(命令行界面)。您也可以按需单独安装组件。

实际案例:

RAGbits文档提供了清晰的快速入门指南,演示了常见用例。以下是其简洁性的一个缩影:

  • 定义和运行LLM提示词:轻松定义类型安全的提示词,并从您选择的LLM获取响应。

    # LLM提示词生成示例
    import asyncio
    from pydantic import BaseModel
    from ragbits.core.llms import LiteLLM
    from ragbits.core.prompt import Prompt
    
    class QuestionAnswerPromptInput(BaseModel):
      question: str
    
    class QuestionAnswerPromptOutput(BaseModel):
      answer: str
    
    class QuestionAnswerPrompt(Prompt[QuestionAnswerPromptInput, QuestionAnswerPromptOutput]):
      system_prompt = """
    你是一个问答代理人。尽你所能回答问题。
    """
      user_prompt = """
    问题:{{ question }}
    """
    
    llm = LiteLLM(model_name="gpt-4.1-nano", use_structured_output=True)
    
    async def main() -> None:
      prompt = QuestionAnswerPrompt(QuestionAnswerPromptInput(question="Linux上的高内存和低内存是什么?"))
      response = await llm.generate(prompt)
      print(response.answer)
    
    if __name__ == "__main__":
      asyncio.run(main())
    

  • 构建向量数据库索引:摄取文档并查询您的自定义知识库。

    # 文档搜索示例
    import asyncio
    from ragbits.core.embeddings import LiteLLMEmbedder
    from ragbits.core.vector_stores import InMemoryVectorStore
    from ragbits.document_search import DocumentSearch
    
    embedder = LiteLLMEmbedder(model_name="text-embedding-3-small")
    vector_store = InMemoryVectorStore(embedder=embedder)
    document_search = DocumentSearch(vector_store=vector_store)
    
    async def run() -> None:
      await document_search.ingest("web://https://arxiv.org/pdf/1706.03762")
      result = await document_search.search("本文提出的主要发现是什么?")
      print(result)
    
    if __name__ == "__main__":
      asyncio.run(run())
    

  • 构建RAG管道:将LLM与检索到的上下文结合,以获得准确且相关的响应。 ```python # RAG管道示例 import asyncio from pydantic import BaseModel from ragbits.core.embeddings import LiteLLMEmbedder from ragbits.core.llms import LiteLLM from ragbits.core.prompt import Prompt from ragbits.core.vector_stores import InMemoryVectorStore from ragbits.document_search import DocumentSearch

    class QuestionAnswerPromptInput(BaseModel): question: str context: list[str]

    class QuestionAnswerPromptOutput(BaseModel): answer: str

    class QuestionAnswerPrompt(Prompt[QuestionAnswerPromptInput, QuestionAnswerPromptOutput]): system_prompt = """ 你是一个问答代理人。请利用上下文回答所提供的问题。 如果给定上下文中的信息不足,请拒绝回答。 """ user_prompt = """ 问题:{{ query }} 上下文:{% for item in context %} {{ item }} {%- endfor %} """

    embedder = LiteLLMEmbedder(model_name="text-embedding-3-small") vector_store = InMemoryVectorStore(embedder=embedder) document_search = DocumentSearch(vector_store=vector_store) llm = LiteLLM(model_name="gpt-4.1-nano", use_structured_output=True)

    async def run() -> None: question = "本文提出的主要发现是什么?"

    await document_search.ingest("web://https://arxiv.org/pdf/1706.03762") result = await document_search.search(question)

    prompt = QuestionAnswerPrompt( QuestionAnswerPromptInput( question=question

原创文章: 查看原文

分享本文