跳转到主要内容

概述

FIM One 围绕一组精简的抽象基类构建——每个可交换组件对应一个基类。每个组件都有单一职责和最小化接口。你实现抽象方法,将实例连接到适当的注册表或注入器,系统的其余部分会自动使用你的实现。

Custom LLM provider

BaseLLM has two required methods — chat and stream_chat — plus an optional abilities property that tells the rest of the system what the model can do.

Registration via ModelRegistry

ModelRegistry maps names to BaseLLM instances and resolves by role. The system looks up three roles: general, fast, and reasoning. You can add your own.
The abilities dict is the contract between the LLM and the ReAct engine. When tool_call=True and the agent was created with use_native_tools=True, the engine will use native function calling. Otherwise it falls back to JSON mode automatically.

自定义工具

工具是最常见的扩展。BaseTool 有三个必需的部分:name、description 和 run。其他所有内容都有合理的默认值。

自动发现

将您的文件放在 src/fim_one/core/tool/builtin/ 中。discover_builtin_tools() 扫描器会自动找到任何具体的(非抽象的)BaseTool 子类 — 无需手动注册。
扫描器会跳过在 _SKIP_AUTO_DISCOVER 中列出的类。对于需要外部配置(例如 API 密钥)并需要在启动时有条件地实例化的工具,请使用该集合。

信号不可用

覆盖 availability() 以在工具目录中显示依赖项缺失时的消息:

使用工件的丰富结果

当你的工具生成文件时,返回 ToolResult 而不是普通的 str:

Custom memory

BaseMemory is the persistence layer for conversation history. Three methods: add_message, get_messages, clear.
Inject via the agent constructor: ReActAgent(llm=llm, tools=registry, memory=RedisMemory(conv_id, url)).

自定义嵌入

BaseEmbedding 提供两个方法:embed_texts(批量)和 embed_query(单个),以及一个 dimension 属性。
embed_texts 和 embed_query 之间的区别存在是因为许多嵌入模型(例如 E5、BGE)对文档和查询使用不同的前缀,以提高检索质量。

自定义图像生成

BaseImageGen 有一个单一方法 generate。它将图像保存到 output_dir 并返回一个 ImageResult,包含文件路径和服务器相对 URL。

自定义重排序器

BaseReranker 接收一个查询和一个文档字符串列表,并返回重新排序后的文档及其分数。

自定义网络后端

Web 获取

BaseWebFetch 获取一个 URL 并将其内容作为 Markdown 或纯文本返回。

网络搜索

BaseWebSearch 返回一个排序的 SearchResult 对象列表。

自定义 RAG 组件

RAG 管道有三个独立可交换的阶段:加载、分块和检索。

文档加载器

BaseLoader 将文件路径转换为 LoadedDocument 对象列表。PDF 加载器通常每页返回一个文档。

文本分块器

BaseChunker 将文本分割成 Chunk 对象。MAX_CHUNK_SIZE = 6000 字符是硬性上限 — 超过此大小的分块可能会超出 Jina Embeddings v3 的令牌窗口。

检索器

BaseRetriever 查询任何后端并返回排序的 Document 对象。

设计原则

几个模式在所有基类中保持一致,使得自定义实现更容易正确编写: 异步优先。 每个方法都是 async def。即使你的实现是同步的,也应该用 asyncio.to_thread() 包装它,而不是阻塞事件循环。 工具的字符串输出。 BaseTool.run() 返回 str(或 ToolResult)。LLM 只能看到文本 — 工具实现负责将复杂数据序列化为可读格式。 最小化接口。 每个基类定义最小的必需契约。BaseMemory 是三个方法;BaseWebFetch 是一个。你永远不需要实现你不需要的功能。 组合优于继承。 基类是接口,而不是框架。你在构造时注入你的实现;运行时永远不会对其进行猴子补丁或进一步子类化。