### Install Dependencies and Start Pipelines Server Source: https://github.com/open-webui/pipelines/blob/main/README.md Commands to clone the Pipelines repository, install its Python dependencies, and start the server locally. Ensure Python 3.11 is installed before running these commands. ```sh git clone https://github.com/open-webui/pipelines.git cd pipelines pip install -r requirements.txt sh ./start.sh ``` -------------------------------- ### Install a pipeline from URL Source: https://context7.com/open-webui/pipelines/llms.txt Installs a Python pipeline by downloading it from a provided URL. This includes support for GitHub blob URLs, which are automatically converted to raw URLs. After installation, all pipelines are hot-reloaded. ```APIDOC ## POST /v1/pipelines/add — Install a pipeline from URL ### Description Downloads a Python pipeline file from a URL (including GitHub blob URLs, which are auto-converted to raw URLs) and hot-reloads all pipelines. ### Method POST ### Endpoint /v1/pipelines/add ### Parameters #### Request Body - **url** (string) - Required - The URL of the Python pipeline file to install. ### Request Example ```bash # Install from a GitHub URL (auto-converted to raw) curl -X POST http://localhost:9099/v1/pipelines/add \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "url": "https://github.com/open-webui/pipelines/blob/main/examples/filters/rate_limit_filter_pipeline.py" }' ``` ### Response #### Success Response (200) - **status** (boolean) - Indicates if the operation was successful. - **detail** (string) - A message describing the result of the operation. ### Response Example ```json {"status": true, "detail": "Pipeline added successfully from ./pipelines/rate_limit_filter_pipeline.py"} ``` ``` -------------------------------- ### Start LangGraph API Server Source: https://github.com/open-webui/pipelines/blob/main/examples/pipelines/integrations/langgraph_pipeline/README.md Run the LangGraph API server using uvicorn. Ensure you are in the correct directory containing the 'langgraph_example.py' file. ```bash uvicorn langgraph_example:app --reload ``` -------------------------------- ### Install Dependencies for LangGraph Pipeline Source: https://github.com/open-webui/pipelines/blob/main/examples/pipelines/integrations/langgraph_pipeline/README.md Install the necessary Python packages for the LangGraph integration by running this command in the specified directory. ```bash pip install -r requirements.txt ``` -------------------------------- ### Upload a pipeline file directly Source: https://context7.com/open-webui/pipelines/llms.txt Upload a `.py` file directly to install it as a new pipeline and trigger a hot-reload of all pipelines. ```bash curl -X POST http://localhost:9099/v1/pipelines/upload \ -H "Authorization: Bearer 0p3n-w3bu!" \ -F "file=@./my_custom_pipeline.py" # Response: # {"status": true, "detail": "Pipeline uploaded successfully to ./pipelines/my_custom_pipeline.py"} ``` -------------------------------- ### Install a pipeline from URL Source: https://context7.com/open-webui/pipelines/llms.txt Download a Python pipeline file from a URL and hot-reload all pipelines. GitHub blob URLs are automatically converted to raw URLs. ```bash # Install from a GitHub URL (auto-converted to raw) curl -X POST http://localhost:9099/v1/pipelines/add \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "url": "https://github.com/open-webui/pipelines/blob/main/examples/filters/rate_limit_filter_pipeline.py" }' # Response: # {"status": true, "detail": "Pipeline added successfully from ./pipelines/rate_limit_filter_pipeline.py"} ``` -------------------------------- ### Docker Deployment with Environment Variables Source: https://context7.com/open-webui/pipelines/llms.txt Configure the Pipelines server at startup using a comprehensive list of environment variables. This example demonstrates setting core API keys, directories, ports, and optional pre-loading of pipelines. ```bash docker run -d \ -p 9099:9099 \ --add-host=host.docker.internal:host-gateway \ -v pipelines:/app/pipelines \ --name pipelines \ --restart always \ # Core settings -e PIPELINES_API_KEY="my-secret-key" \ -e PIPELINES_DIR="/app/pipelines" \ -e HOST="0.0.0.0" \ -e PORT="9099" \ -e GLOBAL_LOG_LEVEL="INFO" \ # Pre-load pipelines from URLs (semicolon-separated) -e PIPELINES_URLS="https://github.com/open-webui/pipelines/blob/main/examples/filters/rate_limit_filter_pipeline.py;https://github.com/open-webui/pipelines/blob/main/examples/filters/langfuse_filter_pipeline.py" \ # Optional: path to a requirements.txt for extra dependencies -e PIPELINES_REQUIREMENTS_PATH="/app/extra_requirements.txt" \ # Reset pipelines dir on start -e RESET_PIPELINES_DIR="false" \ # Pipeline-specific secrets (read via os.getenv in pipeline files) -e OPENAI_API_KEY="sk-..." \ -e LANGFUSE_SECRET_KEY="sk-lf-..." \ -e LANGFUSE_PUBLIC_KEY="pk-lf-..." \ ghcr.io/open-webui/pipelines:main ``` -------------------------------- ### Run Pipelines Container with Custom Pipeline URL Source: https://github.com/open-webui/pipelines/blob/main/README.md This command runs the Pipelines container and specifies a custom pipeline URL to be installed. Use this when you need to load a specific pipeline from a URL during container startup. The `PIPELINES_URLS` environment variable accepts a semicolon-separated list of URLs. ```sh docker run -d -p 9099:9099 --add-host=host.docker.internal:host-gateway -e PIPELINES_URLS="https://github.com/open-webui/pipelines/blob/main/examples/filters/detoxify_filter_pipeline.py" -v pipelines:/app/pipelines --name pipelines --restart always ghcr.io/open-webui/pipelines:main ``` -------------------------------- ### Get Pipeline Valve Configuration Source: https://context7.com/open-webui/pipelines/llms.txt Retrieves the current Valves configuration for a specific pipeline using its ID. ```bash curl http://localhost:9099/v1/rate_limit_filter_pipeline/valves \ -H "Authorization: Bearer 0p3n-w3bu!" # Response: # { # "pipelines": ["*"], # "priority": 0, # "requests_per_minute": 10, # "requests_per_hour": 1000, # "sliding_window_limit": 100, # "sliding_window_minutes": 15 # } ``` -------------------------------- ### Declare Pipeline Dependencies in Docstring Source: https://context7.com/open-webui/pipelines/llms.txt Use a triple-quoted docstring at the top of your pipeline file to declare metadata like title, author, and version. The 'requirements' key automatically triggers pip installation of specified packages. ```python """ title: My RAG Pipeline author: yourname date: 2024-06-01 version: 1.2 license: MIT description: RAG pipeline using LlamaIndex with Ollama. requirements: llama-index, llama-index-llms-ollama """ from typing import List, Union, Generator, Iterator class Pipeline: def __init__(self): self.name = "My RAG Pipeline" self.index = None async def on_startup(self): from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("./data").load_data() self.index = VectorStoreIndex.from_documents(documents) async def on_shutdown(self): pass def pipe(self, user_message: str, model_id: str, messages: List[dict], body: dict): query_engine = self.index.as_query_engine(streaming=True) response = query_engine.query(user_message) return response.response_gen # returns a streaming Iterator ``` -------------------------------- ### GET /v1/{pipeline_id}/valves Source: https://context7.com/open-webui/pipelines/llms.txt Returns the current Valves configuration for a specific pipeline. ```APIDOC ## GET /v1/{pipeline_id}/valves ### Description Returns the current Valves configuration for a specific pipeline. ### Method GET ### Endpoint /v1/{pipeline_id}/valves ### Parameters #### Path Parameters - **pipeline_id** (string) - Required - The ID of the pipeline to retrieve configuration for. ### Response #### Success Response (200) - **pipelines** (array of strings) - List of pipelines. - **priority** (integer) - The priority of the pipeline. - **requests_per_minute** (integer) - The limit for requests per minute. - **requests_per_hour** (integer) - The limit for requests per hour. - **sliding_window_limit** (integer) - The limit for the sliding window. - **sliding_window_minutes** (integer) - The duration of the sliding window in minutes. ``` -------------------------------- ### Manage System Prompt in Messages Source: https://context7.com/open-webui/pipelines/llms.txt Provides functions to get the system message or remove it from the message list. `pop_system_message` is useful for preparing messages for backends that do not support a separate system role. ```python from utils.pipelines.main import pop_system_message, get_system_message messages = [ {"role": "system", "content": "Be concise."}, {"role": "user", "content": "Explain quantum computing."}, ] system_msg, chat_messages = pop_system_message(messages) print(system_msg) # {"role": "system", "content": "Be concise."} print(chat_messages) # [{"role": "user", "content": "Explain quantum computing."}] ``` -------------------------------- ### Filter Pipeline for Request Modification Source: https://context7.com/open-webui/pipelines/llms.txt A filter pipeline that intercepts requests to prepend a system prompt and logs token usage. It applies to specific models based on `valves.pipelines` and `valves.priority`. Ensure the `requests` library is installed. ```python # pipelines/my_filter.py """ title: My Custom Filter author: yourname date: 2024-01-01 version: 1.0 license: MIT description: Adds a system prompt prefix and logs token usage. requirements: requests """ from typing import List, Optional from pydantic import BaseModel class Pipeline: class Valves(BaseModel): pipelines: List[str] = ["*"] # "*" = apply to all models priority: int = 0 # lower = runs first system_prefix: str = "Always respond in bullet points.\n" def __init__(self): self.type = "filter" self.name = "My Custom Filter" self.valves = self.Valves() async def on_startup(self): print(f"Filter ready: {self.name}") async def on_shutdown(self): pass async def inlet(self, body: dict, user: Optional[dict] = None) -> dict: """Prepend a system instruction before the request reaches the LLM.""" messages = body.get("messages", []) if messages and messages[0].get("role") == "system": messages[0]["content"] = self.valves.system_prefix + messages[0]["content"] else: messages.insert(0, {"role": "system", "content": self.valves.system_prefix}) body["messages"] = messages return body async def outlet(self, body: dict, user: Optional[dict] = None) -> dict: """Log token usage after the LLM responds.""" messages = body.get("messages", []) for msg in reversed(messages): if msg.get("role") == "assistant": usage = msg.get("usage", {}) print(f"[Usage] prompt={usage.get('prompt_tokens')} completion={usage.get('completion_tokens')}") break return body ``` -------------------------------- ### GET /v1/{pipeline_id}/valves/spec Source: https://context7.com/open-webui/pipelines/llms.txt Returns the Pydantic JSON schema for a pipeline's Valves model, usable for dynamic form generation. ```APIDOC ## GET /v1/{pipeline_id}/valves/spec ### Description Returns the Pydantic JSON schema for a pipeline's Valves model, usable for dynamic form generation. ### Method GET ### Endpoint /v1/{pipeline_id}/valves/spec ### Parameters #### Path Parameters - **pipeline_id** (string) - Required - The ID of the pipeline to retrieve the schema for. ### Response #### Success Response (200) - **title** (string) - The title of the schema. - **type** (string) - The type of the schema (e.g., 'object'). - **properties** (object) - An object containing the schema properties. ``` -------------------------------- ### Get Valve JSON Schema Source: https://context7.com/open-webui/pipelines/llms.txt Fetches the Pydantic JSON schema for a pipeline's Valves model. This schema is useful for dynamic form generation. ```bash curl http://localhost:9099/v1/rate_limit_filter_pipeline/valves/spec # Response (truncated): # { # "title": "Valves", # "type": "object", # "properties": { # "requests_per_minute": {"title": "Requests Per Minute", "type": "integer"}, # "requests_per_hour": {"title": "Requests Per Hour", "type": "integer"}, # ... # } # } ``` -------------------------------- ### Build Docker Image with Custom Pipelines Source: https://github.com/open-webui/pipelines/blob/main/README.md This bash script prepares environment variables for building a Docker image with custom pipelines. It iterates through a specified directory, collects Python files, and sets the `PIPELINES_URLS` build argument. Use this to pre-install your own pipelines into the Docker image. ```sh # build in the specific pipelines PIPELINE_DIR="pipelines-custom" # assuming the above directory is in your source repo and not skipped by `.dockerignore`, it will get copied to the image PIPELINE_PREFIX="file:///app" # retrieve all the sub files export PIPELINES_URLS= for file in "$PIPELINE_DIR"/*; do if [[ -f "$file" ]]; then if [[ "$file" == *.py ]]; then if [ -z "$PIPELINES_URLS" ]; then PIPELINES_URLS="$PIPELINE_PREFIX/$file" else PIPELINES_URLS="$PIPELINES_URLS;$PIPELINE_PREFIX/$file" fi fi fi done echo "New Custom Install Pipes: $PIPELINES_URLS" docker build --build-arg PIPELINES_URLS=$PIPELINES_URLS --build-arg MINIMUM_BUILD=true -f Dockerfile . ``` -------------------------------- ### List available pipelines as OpenAI models Source: https://context7.com/open-webui/pipelines/llms.txt Use this endpoint to retrieve a list of all loaded pipelines, formatted as OpenAI model-list. This includes pipeline type metadata and valve availability. ```bash curl http://localhost:9099/v1/models \ -H "Authorization: Bearer 0p3n-w3bu!" # Response: # { # "data": [ # { # "id": "my_rag_pipeline", # "name": "My RAG Pipeline", # "object": "model", # "created": 1717000000, # "owned_by": "openai", # "pipeline": { # "type": "pipe", # "valves": true # } # }, # { # "id": "rate_limit_filter", # "name": "Rate Limit Filter", # "object": "model", # "pipeline": { # "type": "filter", # "pipelines": ["llama3:latest"], # "priority": 0, # "valves": true # } # } # ], # "object": "list", # "pipelines": true # } ``` -------------------------------- ### Docker Compose for Open WebUI and Pipelines Source: https://github.com/open-webui/pipelines/blob/main/README.md This Docker Compose configuration sets up both Open WebUI and the Pipelines service. It defines volumes and environment variables for each service. Connect Open WebUI to `http://pipelines:9099`. ```yaml services: openwebui: image: ghcr.io/open-webui/open-webui:main ports: - "3000:8080" volumes: - open-webui:/app/backend/data pipelines: image: ghcr.io/open-webui/pipelines:main volumes: - pipelines:/app/pipelines restart: always environment: - PIPELINES_API_KEY=0p3n-w3bu! volumes: open-webui: {} pipelines: {} ``` -------------------------------- ### Run Pipelines Container with Docker Source: https://github.com/open-webui/pipelines/blob/main/README.md Use this command to run the main Pipelines container. It maps port 9099 and mounts a volume for pipeline data. Ensure Open WebUI is configured to use `http://localhost:9099` as the API URL. ```sh docker run -d -p 9099:9099 --add-host=host.docker.internal:host-gateway -v pipelines:/app/pipelines --name pipelines --restart always ghcr.io/open-webui/pipelines:main ``` -------------------------------- ### List pipeline modules (admin only) Source: https://context7.com/open-webui/pipelines/llms.txt Retrieve the raw list of loaded pipeline modules. This endpoint requires the master API key for access. ```bash curl http://localhost:9099/v1/pipelines \ -H "Authorization: Bearer 0p3n-w3bu!" # Response: # { # "data": [ # {"id": "rate_limit_filter", "name": "rate_limit_filter_pipeline", "type": "filter", "valves": true}, # {"id": "llamaindex_pipeline", "name": "llamaindex_pipeline", "type": "pipe", "valves": false} # ] # } ``` -------------------------------- ### Run inference through a pipe pipeline (streaming) Source: https://context7.com/open-webui/pipelines/llms.txt Send a chat completion request to a named pipeline for streaming inference using Server-Sent Events (SSE). This is fully compatible with the OpenAI Chat Completions API. ```bash # Streaming request (SSE) curl http://localhost:9099/v1/chat/completions \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "model": "my_rag_pipeline", "stream": true, "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Summarize the uploaded document."} ] }' # Streaming response chunks (text/event-stream): # data: {"id":"my_rag_pipeline-uuid","object":"chat.completion.chunk","model":"my_rag_pipeline","choices":[{"index":0,"delta":{"content":"Paris"},"finish_reason":null}]} # data: {"id":"my_rag_pipeline-uuid","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} # data: [DONE] ``` -------------------------------- ### List available pipelines as OpenAI models Source: https://context7.com/open-webui/pipelines/llms.txt Retrieves a list of all loaded pipelines, formatted as OpenAI model-list. This response includes pipeline type metadata and valve availability, ensuring compatibility with any OpenAI-spec client. ```APIDOC ## GET /v1/models — List available pipelines as OpenAI models ### Description Returns all loaded pipelines in OpenAI model-list format. The response includes pipeline type metadata and valve availability, making it compatible with any OpenAI-spec client. ### Method GET ### Endpoint /v1/models ### Request Example ```bash curl http://localhost:9099/v1/models \ -H "Authorization: Bearer 0p3n-w3bu!" ``` ### Response #### Success Response (200) - **data** (array) - List of pipeline models. - **id** (string) - Unique identifier for the pipeline. - **name** (string) - Human-readable name of the pipeline. - **object** (string) - Type of the object, typically "model". - **created** (integer) - Timestamp of creation. - **owned_by** (string) - Owner of the model. - **pipeline** (object) - Pipeline specific details. - **type** (string) - Type of the pipeline (e.g., "pipe", "filter"). - **valves** (boolean) - Indicates if valves are available for this pipeline. - **pipelines** (array, optional) - List of pipelines used by this filter. - **priority** (integer, optional) - Priority of the filter. - **object** (string) - Type of the object, typically "list". - **pipelines** (boolean) - Indicates if pipelines are enabled. ### Response Example ```json { "data": [ { "id": "my_rag_pipeline", "name": "My RAG Pipeline", "object": "model", "created": 1717000000, "owned_by": "openai", "pipeline": { "type": "pipe", "valves": true } }, { "id": "rate_limit_filter", "name": "Rate Limit Filter", "object": "model", "pipeline": { "type": "filter", "pipelines": ["llama3:latest"], "priority": 0, "valves": true } } ], "object": "list", "pipelines": true } ``` ``` -------------------------------- ### Generate OpenAI Function-Calling Specs from Tools Class Source: https://context7.com/open-webui/pipelines/llms.txt Use `get_tools_specs` to introspect a Python class with type hints and docstrings, generating an OpenAI-compatible tool specification array. This is essential for enabling function-calling capabilities in your pipelines. ```python from utils.pipelines.main import get_tools_specs from typing import Literal class MyTools: def get_weather(self, location: str, unit: Literal["metric", "fahrenheit"] = "fahrenheit") -> str: """ Get the current weather for a location. :param location: The city name to look up. :param unit: Temperature unit. Default is fahrenheit. :return: Weather description string. """ return f"Sunny, 72°F in {location}" def get_time(self) -> str: """ Get the current server time. :return: Current time as a string. """ from datetime import datetime return datetime.now().strftime("%H:%M:%S") specs = get_tools_specs(MyTools()) ``` -------------------------------- ### List pipeline modules (admin only) Source: https://context7.com/open-webui/pipelines/llms.txt Retrieves a raw list of all loaded pipeline modules. This endpoint requires the master API key for access. ```APIDOC ## GET /v1/pipelines — List pipeline modules (admin only) ### Description Returns the raw list of loaded pipeline modules. Requires the master API key. ### Method GET ### Endpoint /v1/pipelines ### Request Example ```bash curl http://localhost:9099/v1/pipelines \ -H "Authorization: Bearer 0p3n-w3bu!" ``` ### Response #### Success Response (200) - **data** (array) - List of pipeline modules. - **id** (string) - Unique identifier for the pipeline module. - **name** (string) - Name of the pipeline module. - **type** (string) - Type of the pipeline module (e.g., "filter", "pipe"). - **valves** (boolean) - Indicates if valves are available. ### Response Example ```json { "data": [ {"id": "rate_limit_filter", "name": "rate_limit_filter_pipeline", "type": "filter", "valves": true}, {"id": "llamaindex_pipeline", "name": "llamaindex_pipeline", "type": "pipe", "valves": false} ] } ``` ``` -------------------------------- ### Upload a pipeline file directly Source: https://context7.com/open-webui/pipelines/llms.txt Uploads a Python pipeline file directly to the server. This action triggers a hot-reload of all pipelines. ```APIDOC ## POST /v1/pipelines/upload — Upload a pipeline file directly ### Description Uploads a `.py` file as a new pipeline and hot-reloads all pipelines. ### Method POST ### Endpoint /v1/pipelines/upload ### Parameters #### Request Body - **file** (file) - Required - The Python pipeline file (`.py`) to upload. ### Request Example ```bash curl -X POST http://localhost:9099/v1/pipelines/upload \ -H "Authorization: Bearer 0p3n-w3bu!" \ -F "file=@./my_custom_pipeline.py" ``` ### Response #### Success Response (200) - **status** (boolean) - Indicates if the operation was successful. - **detail** (string) - A message describing the result of the operation. ### Response Example ```json {"status": true, "detail": "Pipeline uploaded successfully to ./pipelines/my_custom_pipeline.py"} ``` ``` -------------------------------- ### Run inference through a pipe pipeline (non-streaming) Source: https://context7.com/open-webui/pipelines/llms.txt Send a chat completion request to a named pipeline for non-streaming inference. This is fully compatible with the OpenAI Chat Completions API. ```bash # Non-streaming request curl http://localhost:9099/v1/chat/completions \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "model": "my_rag_pipeline", "stream": false, "messages": [ {"role": "user", "content": "What is the capital of France?"} ] }' ``` -------------------------------- ### Build SSE Chunk Dictionary for Streaming Responses Source: https://context7.com/open-webui/pipelines/llms.txt The `stream_message_template` function creates a correctly formatted dictionary for OpenAI chat completion chunks, suitable for streaming responses. Ensure the model and message arguments are provided. ```python from utils.pipelines.main import stream_message_template chunk = stream_message_template("my-pipeline", "Hello, world!") ``` -------------------------------- ### Update Pipeline Valve Settings Source: https://context7.com/open-webui/pipelines/llms.txt Updates a pipeline's Valves settings at runtime and persists them to `valves.json`. This action also triggers `on_valves_updated()` if it is defined. ```bash curl -X POST http://localhost:9099/v1/rate_limit_filter_pipeline/valves/update \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "pipelines": ["llama3:latest", "gpt-4"], "priority": 1, "requests_per_minute": 5, "requests_per_hour": 500, "sliding_window_limit": 50, "sliding_window_minutes": 10 }' # Response: updated Valves object ``` -------------------------------- ### Convert GitHub Blob URL to Raw URL Source: https://context7.com/open-webui/pipelines/llms.txt Use `convert_to_raw_url` to transform a GitHub file URL into a direct download URL from `raw.githubusercontent.com`. Non-GitHub URLs are returned unchanged. ```python from utils.pipelines.misc import convert_to_raw_url raw = convert_to_raw_url( "https://github.com/open-webui/pipelines/blob/main/examples/filters/rate_limit_filter_pipeline.py" ) print(raw) # Non-GitHub URLs are returned unchanged unchanged = convert_to_raw_url("https://example.com/my_pipeline.py") print(unchanged) ``` -------------------------------- ### Run inference through a pipe pipeline Source: https://context7.com/open-webui/pipelines/llms.txt Executes a chat completion request through a specified pipeline. This endpoint supports both streaming (SSE) and non-streaming responses, adhering to the OpenAI Chat Completions API standard. ```APIDOC ## POST /v1/chat/completions — Run inference through a pipe pipeline ### Description Sends a chat completion request through a named pipeline. Supports both streaming (SSE) and non-streaming responses, fully compatible with the OpenAI Chat Completions API. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The ID of the pipeline to use for inference. - **stream** (boolean) - Optional - Whether to stream the response. Defaults to false. - **messages** (array) - Required - The conversation messages. - **role** (string) - The role of the message sender (e.g., "user", "system"). - **content** (string) - The content of the message. ### Request Example ```bash # Non-streaming request curl http://localhost:9099/v1/chat/completions \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "model": "my_rag_pipeline", "stream": false, "messages": [ {"role": "user", "content": "What is the capital of France?"} ] }' # Streaming request (SSE) curl http://localhost:9099/v1/chat/completions \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{ "model": "my_rag_pipeline", "stream": true, "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Summarize the uploaded document."} ] }' ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the completion. - **object** (string) - Type of the object, typically "chat.completion" or "chat.completion.chunk". - **model** (string) - The model used for the completion. - **choices** (array) - List of completion choices. - **index** (integer) - Index of the choice. - **delta** (object) - The delta content for streaming responses. - **content** (string, optional) - The content of the message chunk. - **finish_reason** (string, optional) - The reason for finishing the generation (e.g., "stop"). ### Response Example ``` # Streaming response chunks (text/event-stream): data: {"id":"my_rag_pipeline-uuid","object":"chat.completion.chunk","model":"my_rag_pipeline","choices":[{"index":0,"delta":{"content":"Paris"},"finish_reason":null}]} data: {"id":"my_rag_pipeline-uuid","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} data: [DONE] ``` ``` -------------------------------- ### POST /v1/{pipeline_id}/valves/update Source: https://context7.com/open-webui/pipelines/llms.txt Updates a pipeline's Valves at runtime and persists them to `valves.json`. Triggers `on_valves_updated()` if defined. ```APIDOC ## POST /v1/{pipeline_id}/valves/update ### Description Updates a pipeline's Valves at runtime and persists them to `valves.json`. Triggers `on_valves_updated()` if defined. ### Method POST ### Endpoint /v1/{pipeline_id}/valves/update ### Parameters #### Path Parameters - **pipeline_id** (string) - Required - The ID of the pipeline to update. #### Request Body - **pipelines** (array of strings) - Optional - List of pipelines. - **priority** (integer) - Optional - The priority of the pipeline. - **requests_per_minute** (integer) - Optional - The limit for requests per minute. - **requests_per_hour** (integer) - Optional - The limit for requests per hour. - **sliding_window_limit** (integer) - Optional - The limit for the sliding window. - **sliding_window_minutes** (integer) - Optional - The duration of the sliding window in minutes. ### Request Example ```json { "pipelines": ["llama3:latest", "gpt-4"], "priority": 1, "requests_per_minute": 5, "requests_per_hour": 500, "sliding_window_limit": 50, "sliding_window_minutes": 10 } ``` ### Response #### Success Response (200) - The updated Valves object. ``` -------------------------------- ### OpenAI API Integration Pipeline Source: https://context7.com/open-webui/pipelines/llms.txt A pipeline that directly interacts with the OpenAI API for chat completions. It handles API key management, model selection, and request/response streaming. Ensure the OPENAI_API_KEY environment variable is set. ```python from typing import List, Union, Generator, Iterator from pydantic import BaseModel import os, requests class Pipeline: class Valves(BaseModel): OPENAI_API_KEY: str = "" MODEL: str = "gpt-4o" def __init__(self): self.name = "My OpenAI Pipeline" self.valves = self.Valves( OPENAI_API_KEY=os.getenv("OPENAI_API_KEY", ""), MODEL=os.getenv("MODEL", "gpt-4o"), ) async def on_startup(self): print(f"Pipeline starting: {self.name}") async def on_shutdown(self): print(f"Pipeline stopping: {self.name}") async def on_valves_updated(self): print("Valves updated, reinitializing connections...") def pipe( self, user_message: str, model_id: str, messages: List[dict], body: dict ) -> Union[str, Generator, Iterator]: headers = { "Authorization": f"Bearer {self.valves.OPENAI_API_KEY}", "Content-Type": "application/json", } payload = {**body, "model": self.valves.MODEL} # Strip non-OpenAI keys for key in ["user", "chat_id", "title"]: payload.pop(key, None) try: r = requests.post( "https://api.openai.com/v1/chat/completions", json=payload, headers=headers, stream=True, ) r.raise_for_status() return r.iter_lines() if body.get("stream") else r.json() except Exception as e: return f"Error: {e}" ``` -------------------------------- ### POST /v1/pipelines/reload Source: https://context7.com/open-webui/pipelines/llms.txt Shuts down all running pipelines and re-loads them from the pipelines directory. ```APIDOC ## POST /v1/pipelines/reload ### Description Shuts down all running pipelines and re-loads them from the pipelines directory. ### Method POST ### Endpoint /v1/pipelines/reload ### Response #### Success Response (200) - **message** (string) - A confirmation message indicating that pipelines have been reloaded. ``` -------------------------------- ### POST /v1/{pipeline_id}/filter/inlet Source: https://context7.com/open-webui/pipelines/llms.txt Manually trigger the inlet (pre-processing) of a filter pipeline. Used internally by Open WebUI before forwarding a request to the LLM. ```APIDOC ## POST /v1/{pipeline_id}/filter/inlet ### Description Manually trigger the inlet (pre-processing) of a filter pipeline. Used internally by Open WebUI before forwarding a request to the LLM. ### Method POST ### Endpoint /v1/{pipeline_id}/filter/inlet ### Parameters #### Path Parameters - **pipeline_id** (string) - Required - The ID of the pipeline to invoke the inlet for. #### Request Body - **body** (object) - Required - The request body to be processed. - **model** (string) - Required - The model to use. - **messages** (array of objects) - Required - The message history. - **user** (object) - Required - Information about the user. - **id** (string) - Required - The user ID. - **role** (string) - Required - The user role. - **email** (string) - Required - The user email. ### Request Example ```json { "body": { "model": "llama3:latest", "messages": [{"role": "user", "content": "Hello"}] }, "user": {"id": "user-123", "role": "user", "email": "user@example.com"} } ``` ### Response #### Success Response (200) - The (possibly modified) body dictionary, or raises 429 if rate-limited. ``` -------------------------------- ### Define Custom Tools for a Pipeline Source: https://context7.com/open-webui/pipelines/llms.txt This Python code defines a custom Tools class within a Pipeline, providing methods like `get_current_time` and `search_docs` that can be utilized by the pipeline's execution logic. Ensure the `datetime` module is imported. ```python import os from typing import Literal from datetime import datetime from blueprints.function_calling_blueprint import Pipeline as FunctionCallingBlueprint class Pipeline(FunctionCallingBlueprint): class Valves(FunctionCallingBlueprint.Valves): MY_API_KEY: str = "" class Tools: def __init__(self, pipeline) -> None: self.pipeline = pipeline def get_current_time(self) -> str: """ Get the current time. :return: The current time in HH:MM:SS format. """ return f"Current Time = {datetime.now().strftime('%H:%M:%S')}" def search_docs(self, query: str) -> str: """ Search internal documentation for an answer. :param query: The search query string. :return: Relevant documentation snippet. """ # Replace with actual doc search logic return f"Documentation result for '{query}': ..." def __init__(self): super().__init__() self.name = "My Tools Pipeline" self.valves = self.Valves(**{ **self.valves.model_dump(), "pipelines": ["*"], "TASK_MODEL": "gpt-4o-mini", "MY_API_KEY": os.getenv("MY_API_KEY", ""), }) self.tools = self.Tools(self) ``` -------------------------------- ### Reload All Pipelines Source: https://context7.com/open-webui/pipelines/llms.txt This endpoint shuts down all currently running pipelines and reloads them from the designated pipelines directory. ```bash curl -X POST http://localhost:9099/v1/pipelines/reload \ -H "Authorization: Bearer 0p3n-w3bu!" # Response: # {"message": "Pipelines reloaded successfully."} ``` -------------------------------- ### POST /v1/{pipeline_id}/filter/outlet Source: https://context7.com/open-webui/pipelines/llms.txt Manually trigger the outlet (post-processing) of a filter pipeline. Used internally by Open WebUI after receiving the LLM response. ```APIDOC ## POST /v1/{pipeline_id}/filter/outlet ### Description Manually trigger the outlet (post-processing) of a filter pipeline. Used internally by Open WebUI after receiving the LLM response. ### Method POST ### Endpoint /v1/{pipeline_id}/filter/outlet ### Parameters #### Path Parameters - **pipeline_id** (string) - Required - The ID of the pipeline to invoke the outlet for. #### Request Body - **body** (object) - Required - The response body from the LLM. - **model** (string) - Required - The model used. - **chat_id** (string) - Required - The chat ID. - **messages** (array of objects) - Required - The message history. - **user** (object) - Required - Information about the user. - **id** (string) - Required - The user ID. - **email** (string) - Required - The user email. ### Request Example ```json { "body": { "model": "llama3:latest", "chat_id": "chat-abc123", "messages": [ {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!", "usage": {"prompt_tokens": 5, "completion_tokens": 3}} ] }, "user": {"id": "user-123", "email": "user@example.com"} } ``` ``` -------------------------------- ### Add or Update System Message Source: https://context7.com/open-webui/pipelines/llms.txt This function injects or prepends content to the system message in a conversation. It's useful for adding context, such as retrieved information in RAG pipelines, before sending messages to a model. ```python from utils.pipelines.main import add_or_update_system_message messages = [{"role": "user", "content": "What is the return policy?"}] context = "Use this context:\nReturns accepted within 30 days.\n" updated = add_or_update_system_message(context, messages) print(updated[0]) # {"role": "system", "content": "Use this context:\nReturns accepted within 30 days.\n"} print(updated[1]) # {"role": "user", "content": "What is the return policy?"} ``` -------------------------------- ### Pipe Pipeline Base Class Source: https://context7.com/open-webui/pipelines/llms.txt The base class for custom pipelines. Place `.py` files in the `./pipelines/` directory for auto-loading. The `pipe()` method handles user messages, model IDs, history, and raw body, returning a `str`, `Generator`, or `Iterator`. ```python class Pipe: def pipe(self, user_message: str, model_id: str, history: list[dict], body: dict) -> str | Iterator: # Your custom pipeline logic here pass ``` -------------------------------- ### Invoke Filter Pipeline Outlet Source: https://context7.com/open-webui/pipelines/llms.txt Manually triggers the outlet (post-processing) of a filter pipeline. This is used internally by Open WebUI after receiving the LLM response. ```bash curl -X POST http://localhost:9099/v1/langfuse_filter_pipeline/filter/outlet \ -H "Content-Type: application/json" \ -d '{ "body": { "model": "llama3:latest", "chat_id": "chat-abc123", "messages": [ {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!", "usage": {"prompt_tokens": 5, "completion_tokens": 3}} ] }, "user": {"id": "user-123", "email": "user@example.com"} }' ``` -------------------------------- ### DELETE /v1/pipelines/delete Source: https://context7.com/open-webui/pipelines/llms.txt Deletes a pipeline by its ID, calls its `on_shutdown` hook, and hot-reloads the pipeline configuration. ```APIDOC ## DELETE /v1/pipelines/delete ### Description Deletes a pipeline by its ID, calls its `on_shutdown` hook, and hot-reloads. ### Method DELETE ### Endpoint /v1/pipelines/delete ### Parameters #### Request Body - **id** (string) - Required - The ID of the pipeline to delete. ### Request Example ```json { "id": "rate_limit_filter_pipeline" } ``` ### Response #### Success Response (200) - **status** (boolean) - Indicates if the operation was successful. - **detail** (string) - A message describing the result of the deletion. ``` -------------------------------- ### Manifold Pipeline for Multiple Models Source: https://context7.com/open-webui/pipelines/llms.txt A manifold pipeline that registers itself as a provider for multiple sub-models, all handled by a single `pipe()` method. The `model_id` argument in `pipe()` determines which backend model is used. ```python # pipelines/my_manifold.py from typing import List, Union, Generator, Iterator class Pipeline: def __init__(self): self.type = "manifold" self.name = "My Provider: " # prefix for all sub-model names self.pipelines = [ {"id": "fast-model", "name": "Fast Model"}, {"id": "precise-model", "name": "Precise Model"}, ] async def on_startup(self): print("Manifold ready") async def on_shutdown(self): pass def pipe( self, user_message: str, model_id: str, messages: List[dict], body: dict ) -> Union[str, Generator, Iterator]: # model_id will be "fast-model" or "precise-model" backend_model = "gpt-3.5-turbo" if model_id == "fast-model" else "gpt-4o" # ... forward to appropriate backend ... return f"[{backend_model}] Response to: {user_message}" ``` -------------------------------- ### Invoke Filter Pipeline Inlet Source: https://context7.com/open-webui/pipelines/llms.txt Manually triggers the inlet (pre-processing) of a filter pipeline. This is used internally by Open WebUI before forwarding a request to the LLM. It returns the modified body dict or raises a 429 error if rate-limited. ```bash curl -X POST http://localhost:9099/v1/rate_limit_filter_pipeline/filter/inlet \ -H "Content-Type: application/json" \ -d '{ "body": { "model": "llama3:latest", "messages": [{"role": "user", "content": "Hello"}] }, "user": {"id": "user-123", "role": "user", "email": "user@example.com"} }' # Returns the (possibly modified) body dict, or raises 429 if rate-limited. ``` -------------------------------- ### Delete a Pipeline Source: https://context7.com/open-webui/pipelines/llms.txt Use this endpoint to delete a pipeline by its ID. It also triggers the pipeline's `on_shutdown` hook and performs a hot-reload. ```bash curl -X DELETE http://localhost:9099/v1/pipelines/delete \ -H "Authorization: Bearer 0p3n-w3bu!" \ -H "Content-Type: application/json" \ -d '{"id": "rate_limit_filter_pipeline"}' # Response: # {"status": true, "detail": "Pipeline rate_limit_filter_pipeline deleted successfully"} ``` -------------------------------- ### Extract Last User Message from Conversation Source: https://context7.com/open-webui/pipelines/llms.txt This utility function iterates through a list of messages to find and return the content of the most recent message from the 'user' role. It handles messages with text content and multimodal content arrays. ```python from utils.pipelines.main import get_last_user_message messages = [ {"role": "system", "content": "You are helpful."}, {"role": "user", "content": "What is 2+2?"}, {"role": "assistant", "content": "4"}, {"role": "user", "content": [{"type": "text", "text": "Are you sure?"}, {"type": "image_url", "image_url": "..."}]} ] last = get_last_user_message(messages) print(last) # "Are you sure?" # Returns None if no user message found empty_result = get_last_user_message([{"role": "assistant", "content": "hi"}]) print(empty_result) # None ``` -------------------------------- ### Extract Last Assistant Message from Conversation Source: https://context7.com/open-webui/pipelines/llms.txt This function finds and returns the content of the most recent message from the 'assistant' role in a conversation history. It returns None if the last message is not from the assistant. ```python from utils.pipelines.main import get_last_assistant_message messages = [ {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}, {"role": "user", "content": "What can you do?"}, ] # Returns None since the last message is from user result = get_last_assistant_message(messages) print(result) # "Hi there!" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.