This program provides a way to extend it functionality with "functions". Let's focus on class Pipe. Please tell me how it works on the back end? How are things organized and extecuted?
Assistant
User
basic Pipe looks like this:
python
from pydantic import BaseModel, Field
class Pipe:
# Valves: Configuration options for the Pipe.
class Valves(BaseModel):
# Example valve (optional)
EXAMPLE_CONFIG: str = Field(default="default_value", description="Example configuration")
pass
# Constructor: Initializes the valves for this pipe.
def __init__(self):
self.valves = self.Valves()
# Pipe function: Core logic for processing input and returning output.
def pipe(self, body: dict) -> str:
print(f"pipe:{__name__}")
# Add your custom logic here.
return "Hello, World!"
# Pipes function: (Optional) Defines multiple models for this Pipe.
def pipes(self):
# Example of multiple model definitions.
return [
{"id": "model_id_1", "name": "model_1"},
{"id": "model_id_2", "name": "model_2"},
]
Assistant
User
do I understand correctly that pipe() get's called if user starts to interact with a model that was created by the Pipe function but pipes() get's executed when the function itself is created or updated?
Assistant
User
OK please write up a quick and proffessional documentation that has the goal of fully explaining to the reader how the function defined in the front end and the backend interact together.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The Open WebUI provides a way to extend its functionality through "functions", which are essentially plugins that can be created, managed, and executed within the system. The key class for handling these functions is the Function class, which is defined in the database schema (open_webui/models/functions.py). However, the concept of a "pipe" is more related to how these functions are executed and organized in the backend.
Key Components and Organization
Function Class (Function):
This class represents a function in the database. It stores metadata about the function, such as its ID, name, content, type, and whether it is active or global.
The Function class is defined in open_webui/models/functions.py and is used to interact with the database.
Function Execution (generate_function_chat_completion):
The main logic for executing a function is found in the generate_function_chat_completion function in open_webui/functions.py.
This function is responsible for handling the execution of a function (or "pipe") when a request is made to the API.
Function Module Loading (get_function_module_by_id):
Before a function can be executed, its module needs to be loaded. This is handled by the get_function_module_by_id function in open_webui/functions.py.
This function checks if the function is already loaded in the application state. If not, it loads the function module using load_function_module_by_id.
Function Types and Manifolds:
Functions can be of different types, such as "pipe" or "manifold". A "manifold" is a special type of function that contains multiple sub-functions (or "pipes").
The get_function_models function in open_webui/functions.py is responsible for retrieving the list of available function models, including both single pipes and manifolds.
Execution Flow
Function Request:
When a request is made to the API to execute a function, the generate_function_chat_completion function is called.
This function first retrieves the function module using get_function_module_by_id.
Parameter Handling:
The function parameters are extracted from the request and passed to the function module.
If the function is a manifold, it will handle the execution of its sub-pipes.
Execution:
The function's pipe method is called with the appropriate parameters.
If the function returns a generator or an asynchronous generator, the results are streamed back to the client.
Response Handling:
The response from the function is processed and returned to the client, either as a stream (for generators) or as a single response.
Example of Function Execution
Here's a simplified breakdown of how a function is executed:
if isinstance(res, Generator):
for line in res:
yield process_line(form_data, line)
[Redacted Person] isinstance(res, AsyncGenerator):
async for line in res:
yield process_line(form_data, line)
Summary
Functions are plugins that extend the functionality of Open WebUI.
Pipes are the core execution units within functions.
The backend organizes functions in the database and loads their modules dynamically.
Functions can be simple (single pipe) or complex (manifold with multiple sub-pipes).
The execution flow involves loading the function, handling parameters, executing the function, and processing the response.
This system allows for flexible and extensible functionality within Open WebUI, enabling users to create and manage custom functions that can be executed on demand.
please help me understand how the parts connect together here. basically how do these defined classes and functions get called and used? Also should the def pipe() be actually async def pipe()?
Let's break down how the Pipe class and its components are connected and used in the Open WebUI backend. I'll also address whether pipe() should be async def pipe().
1. Structure of the Pipe Class
The Pipe class is a template for defining a custom function (or "pipe") in Open WebUI. It consists of the following parts:
a. Valves Inner Class
This is a Pydantic model that defines configuration options (or "valves") for the pipe.
Example:
python
class Valves(BaseModel):
EXAMPLE_CONFIG: str = Field(default="default_value", description="Example configuration")
Purpose: These valves allow the pipe to be configured dynamically. For example, an admin can update the EXAMPLE_CONFIG value via the API, and the pipe will use the updated value during execution.
b. __init__ Method
Initializes the valves attribute with an instance of the Valves class.
Example:
python
def __init__(self):
self.valves = self.Valves()
Purpose: Ensures that the pipe has access to its configuration options when it is instantiated.
c. pipe Method
This is the core logic of the pipe. It processes input data (body) and returns output.
Purpose: If your pipe supports multiple models or configurations, you can define them here. These models will be exposed as separate endpoints in the API.
2. How the Pipe is Used in the Backend
a. Function Registration
When a pipe is created, it is registered in the backend via the /functions/create API endpoint. The content of the pipe (i.e., the Python code) is stored in the database.
The backend calls the pipe method with body = { "name": "[Redacted Person]" }.
The result is "Hello, [Redacted Person]!".
6. Summary
The Pipe class defines the logic and configuration for a custom function.
The backend dynamically loads and executes the pipe when the API is called.
Use async def pipe() for I/O-bound tasks and def pipe() for CPU-bound tasks.
The Valves class and pipes method provide flexibility for configuration and multi-model support.
Almost, but not quite! Let me clarify the roles of pipe() and pipes() and when they are called.
1. pipe() Method
When is it called?
The pipe() method is called when a user interacts with a model that is associated with the pipe.
For example, if the pipe defines a model (or multiple models via pipes()), and the user sends a request to that model, the pipe() method is executed to process the request.
What does it do?
It contains the core logic for processing input data (body) and returning output.
When the function is created, the backend calls pipes() to discover the available models (friendly_greet and formal_greet).
These models are registered as API endpoints:
/api/v1/functions/my_pipe.friendly_greet
/api/v1/functions/my_pipe.formal_greet
c. User Interaction
A user sends a request to /api/v1/functions/my_pipe.friendly_greet with the body {"name": "[Redacted Person]"}.
The backend calls the pipe() method for the friendly_greet model.
The result is "Hello, [Redacted Person]!".
5. Summary
pipe(): Called when a user interacts with a model. It processes the request and returns a result.
pipes(): Called when the function is created or updated. It defines the available models for the pipe.
You were close, but pipes() is not called during user interaction—it’s only called during function creation/update to define the models. The pipe() method is the one that gets called during user interaction.
Open WebUI Function Integration Guide
This document provides a comprehensive explanation of how functions (or "pipes") defined in the frontend interact with the backend in Open WebUI. It covers the lifecycle of a function, from creation to execution, and explains the roles of key components.
1. Overview
Open WebUI allows users to define custom functions (referred to as "pipes") that extend the system's functionality. These functions are defined in Python and can be created, updated, and executed via the API. The integration between the frontend and backend involves:
Function Creation: Defining a function in the frontend and registering it in the backend.
Function Execution: Triggering the function via API requests and processing the results.
Function Configuration: Dynamically configuring the function using "valves" (configuration options).
2. Key Components
Frontend
Function Definition: Users define the function's Python code, including its pipe() method and optional pipes() and Valves classes.
API Integration: The frontend interacts with the backend via the /functions API to create, update, and execute functions.
Backend
Function Storage: Functions are stored in the database with their metadata (e.g., ID, name, content).
Function Execution: The backend dynamically loads and executes functions when requested.
Configuration Management: The backend manages configuration options ("valves") for each function.
3. Function Lifecycle
Step 1: Function Creation
Frontend:
The user defines a function in Python, including the pipe() method and optional pipes() and Valves classes.
Purpose: Defines configuration options for the function.
When Used: During function configuration and execution.
Example:
python
class Valves(BaseModel):
GREETING: str = Field(default="Hello", description="Greeting message")
5. Example Workflow
Frontend
Define the function in Python.
Send the function definition to the backend via /functions/create.
Configure the function's valves via /functions/id/{id}/valves/update.
Send a request to the function's API endpoint with input data.
Backend
Store the function and register its models.
Update the function's valves.
Load the function and execute the pipe() method with the input data.
Return the result to the frontend.
6. Conclusion
The integration between the frontend and backend in Open WebUI enables users to define, configure, and execute custom functions seamlessly. By understanding the roles of the pipe(), pipes(), and Valves components, developers can create powerful and flexible extensions to the system.