本文详细介绍了如何编写 OpenWebUI 自定义工具包,涵盖工具包结构、类型提示、Valves 配置以及事件发射器的使用。通过本指南,开发者可以快速上手创建功能强大的自定义工具包,提升 OpenWebUI 的扩展能力。 在 OpenWebUI 中开发自定义工具包时,首先需要定义一个包含元数据的顶级文档字符串。该文档字符串包含工具的标题、作者、依赖项等信息。 ``` """ title: String Inverse author: Your Name author_url: https://website.com git_url: https://github.com/username/string-reverse.git description: This tool calculates the inverse of a string required_open_webui_version: 0.4.0 requirements: langchain-openai, langgraph, ollama, langchain_ollama version: 0.4.0 licence: MIT """ ``` 接下来,定义 `Tools` 类,其中包含方法和 `Valves` 配置。每个方法必须提供参数类型提示,确保工具的正确调用。 ``` class Tools: def __init__(self): self.valves = self.Valves() class Valves(BaseModel): api_key: str = Field("", description="Your API key here") def reverse_string(self, string: str) -> str: """ Reverses the input string. :param string: The string to reverse """ if self.valves.api_key != "42": return "Wrong API key" return string[::-1] ``` Valves 和 UserValves 用于配置工具参数,是自定义工具包的关键部分。通过合理设置 Valves,可以实现工具的灵活配置。 在工具执行过程中,使用事件发射器(Event Emitter)向聊天界面添加状态或消息。例如,发送处理状态: ``` await __event_emitter__( { "type": "status", "data": {"description": "Processing string reversal", "done": False}, } ) ``` 此外,通过事件发射器的引用类型(citation),可以添加来源信息,增强文档的可信度。 掌握自定义工具包的开发流程,能够显著提升 OpenWebUI 的功能扩展性。开发者应充分利用 OpenWebUI 的工具开发框架,构建高效、可靠的自定义工具包。 Loading... 本文详细介绍了如何编写 OpenWebUI 自定义工具包,涵盖工具包结构、类型提示、Valves 配置以及事件发射器的使用。通过本指南,开发者可以快速上手创建功能强大的自定义工具包,提升 OpenWebUI 的扩展能力。 在 OpenWebUI 中开发自定义工具包时,首先需要定义一个包含元数据的顶级文档字符串。该文档字符串包含工具的标题、作者、依赖项等信息。 ``` """ title: String Inverse author: Your Name author_url: https://website.com git_url: https://github.com/username/string-reverse.git description: This tool calculates the inverse of a string required_open_webui_version: 0.4.0 requirements: langchain-openai, langgraph, ollama, langchain_ollama version: 0.4.0 licence: MIT """ ``` 接下来,定义 `Tools` 类,其中包含方法和 `Valves` 配置。每个方法必须提供参数类型提示,确保工具的正确调用。 ``` class Tools: def __init__(self): self.valves = self.Valves() class Valves(BaseModel): api_key: str = Field("", description="Your API key here") def reverse_string(self, string: str) -> str: """ Reverses the input string. :param string: The string to reverse """ if self.valves.api_key != "42": return "Wrong API key" return string[::-1] ``` Valves 和 UserValves 用于配置工具参数,是自定义工具包的关键部分。通过合理设置 Valves,可以实现工具的灵活配置。 在工具执行过程中,使用事件发射器(Event Emitter)向聊天界面添加状态或消息。例如,发送处理状态: ``` await __event_emitter__( { "type": "status", "data": {"description": "Processing string reversal", "done": False}, } ) ``` 此外,通过事件发射器的引用类型(citation),可以添加来源信息,增强文档的可信度。 掌握自定义工具包的开发流程,能够显著提升 OpenWebUI 的功能扩展性。开发者应充分利用 OpenWebUI 的工具开发框架,构建高效、可靠的自定义工具包。 最后修改:2025 年 11 月 16 日 © 允许规范转载 赞 别打赏,我怕忍不住购买辣条与续命水