[PR #14667] [MERGED] feat: OpenAI-compatible /api/embeddings endpoint with provider-agnostic OpenWebUI architecture #10342

Closed
opened 2025-11-11 19:02:22 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/14667
Author: @hdnh2006
Created: 6/4/2025
Status: Merged
Merged: 6/4/2025
Merged by: @tjbck

Base: devHead: main


📝 Commits (7)

  • 0afe972 embeddings function added 100% OpenAI compatible
  • 4151145 Merge branch 'open-webui:main' into main
  • e0769c6 new embedding.py added for handling openai and ollama embedding
  • 8f6c3f4 payload and response modifed for compatibility
  • 3ddebef openai embeddings function modified
  • cc12e9e Ollama embeddings adapted to pydantic
  • 04da2b0 convert embedding function name to be more consistence

📊 Changes

5 files changed (+321 additions, -0 deletions)

View changed files

📝 backend/open_webui/main.py (+32 -0)
📝 backend/open_webui/routers/openai.py (+79 -0)
backend/open_webui/utils/embeddings.py (+127 -0)
📝 backend/open_webui/utils/payload.py (+31 -0)
📝 backend/open_webui/utils/response.py (+52 -0)

📄 Description

Pull Request Checklist

Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.

Before submitting, make sure you've checked the following:

  • Target branch: Please verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Have you written and run sufficient tests to validate the changes?
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following:
    • BREAKING CHANGE: Significant changes that may affect compatibility
    • build: Changes that affect the build system or external dependencies
    • ci: Changes to our continuous integration processes or workflows
    • chore: Refactor, cleanup, or other non-functional code changes
    • docs: Documentation update or addition
    • feat: Introduces a new feature or enhancement to the codebase
    • fix: Bug fix or error correction
    • i18n: Internationalization or localization changes
    • perf: Performance improvement
    • refactor: Code restructuring for better maintainability, readability, or scalability
    • style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.)
    • test: Adding missing tests or correcting existing tests
    • WIP: Work in progress, a temporary label for incomplete or ongoing work

Changelog Entry

Description

In this PR, I solve this feature request.

With this PR, a new endpoint /api/embeddings is introduced to the backend, enabling direct access to the embeddings functionality. This route is fully compatible with OpenAI, Ollama, and pipeline-based embedding providers available in Open WebUI, while preserving the existing /openai/embeddings route for backward compatibility. This enhancement streamlines integration by providing a unified and intuitive API for generating embeddings across multiple backends.

There's a big difference with my previous, I follow the suggestion of the author in this comment:

Your implementation was for OpenAI only, if you take a look at the /chat/completion endpoint, you'll see we have a > compatibility layer that allows users to access models from other providers including Ollama....

For this, my PR:

  • Implements a unified /api/embeddings endpoint following the OpenWebUI generic dispatch pattern.
  • Now supports OpenAI, Ollama, Arena, pipeline and any compatible provider using OpenWebUI’s dynamic routing infrastructure.
  • Ensures complete OpenAI API compatibility for embeddings.

Added

  • New route /api/embeddings that maps to the existing OpenAI embeddings functionality
  • Generic embeddings handler in utils/embeddings.py modeled after utils/chat.py.
  • Payload and response conversion utilities for OpenAI ↔️ Ollama embeddings compatibility.

Changed

  • Refactored /api/embeddings endpoint in main.py to use the new, extensible dispatch system.
  • Applied code style, docstrings, and handler registration to match core OpenWebUI conventions.

Deprecated

  • NA

Removed

  • NA

Fixed

  • This PR solves this feature request.

Security

  • NA

Breaking Changes

  • feat

Additional Information

  • This update preserves backward compatibility and introduces a more user-friendly API endpoint.
  • The solution leverages existing logic from the OpenAI router to ensure consistency.
  • No breaking changes have been introduced, and no new dependencies are needed.
  • This PR addresses feature request #8719.

Python code used for testing:

import requests
import json

# API endpoint
url = "http://localhost:3000/api/embeddings"

# Headers
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk-12345678"  # Replace with your actual token
}


# Request payload
payload = {
    "model": "text-embedding-ada-002",
    "input": [
        "Hello this is a single test",
    ]
}


# Make the POST request
response = requests.post(url, headers=headers, json=payload)

# Check if the request was successful
response.raise_for_status()

# Print the response
print("Response:", response.json())

Response gotten:

Response: {'model': 'text-embedding-ada-002-v2', 'data': [{'embedding': [-0.017571303993463516, 0.022639181464910507, -0.003385236021131277, -0.02396933175623417, ...], 'index': 0, 'object': 'embedding'}], 'object': 'list', 'usage': {'completion_tokens': 0, 'prompt_tokens': 6, 'total_tokens': 6, 'completion_tokens_details': None, 'prompt_tokens_details': None}}

Screenshots or Videos

image

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/14667 **Author:** [@hdnh2006](https://github.com/hdnh2006) **Created:** 6/4/2025 **Status:** ✅ Merged **Merged:** 6/4/2025 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `main` --- ### 📝 Commits (7) - [`0afe972`](https://github.com/open-webui/open-webui/commit/0afe972bc6bf3b52a77832a32d39607570457826) embeddings function added 100% OpenAI compatible - [`4151145`](https://github.com/open-webui/open-webui/commit/415114504fdf4c3f05d397b927f53273f4affba2) Merge branch 'open-webui:main' into main - [`e0769c6`](https://github.com/open-webui/open-webui/commit/e0769c6a1f5ca8f4528f970b2603592c7260cfa5) new embedding.py added for handling openai and ollama embedding - [`8f6c3f4`](https://github.com/open-webui/open-webui/commit/8f6c3f46d6521737df8afd1ee3891837750a282e) payload and response modifed for compatibility - [`3ddebef`](https://github.com/open-webui/open-webui/commit/3ddebefca28fc6f190b6c61332556de2ad099497) openai embeddings function modified - [`cc12e9e`](https://github.com/open-webui/open-webui/commit/cc12e9e1a3a80a35b4934d1c04f35eb0a7e398f8) Ollama embeddings adapted to pydantic - [`04da2b0`](https://github.com/open-webui/open-webui/commit/04da2b05f6ee7f77d26c131c224653d64ae10536) convert embedding function name to be more consistence ### 📊 Changes **5 files changed** (+321 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/main.py` (+32 -0) 📝 `backend/open_webui/routers/openai.py` (+79 -0) ➕ `backend/open_webui/utils/embeddings.py` (+127 -0) 📝 `backend/open_webui/utils/payload.py` (+31 -0) 📝 `backend/open_webui/utils/response.py` (+52 -0) </details> ### 📄 Description # Pull Request Checklist ### Note to first-time contributors: Please open a discussion post in [Discussions](https://github.com/open-webui/open-webui/discussions) and describe your changes before submitting a pull request. **Before submitting, make sure you've checked the following:** - [X] **Target branch:** Please verify that the pull request targets the `dev` branch. - [X] **Description:** Provide a concise description of the changes made in this pull request. - [X] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [X] **Documentation:** Have you updated relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs), or other documentation sources? - [X] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation? - [X] **Testing:** Have you written and run sufficient tests to validate the changes? - [X] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [X] **Prefix:** To clearly categorize this pull request, prefix the pull request title using one of the following: - **BREAKING CHANGE**: Significant changes that may affect compatibility - **build**: Changes that affect the build system or external dependencies - **ci**: Changes to our continuous integration processes or workflows - **chore**: Refactor, cleanup, or other non-functional code changes - **docs**: Documentation update or addition - **feat**: Introduces a new feature or enhancement to the codebase - **fix**: Bug fix or error correction - **i18n**: Internationalization or localization changes - **perf**: Performance improvement - **refactor**: Code restructuring for better maintainability, readability, or scalability - **style**: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.) - **test**: Adding missing tests or correcting existing tests - **WIP**: Work in progress, a temporary label for incomplete or ongoing work # Changelog Entry ### Description In this PR, I solve [this](https://github.com/open-webui/open-webui/issues/8719) feature request. With this PR, a new endpoint `/api/embeddings` is introduced to the backend, enabling direct access to the embeddings functionality. This route is fully compatible with OpenAI, Ollama, and pipeline-based embedding providers available in Open WebUI, while preserving the existing `/openai/embeddings` route for backward compatibility. This enhancement streamlines integration by providing a unified and intuitive API for generating embeddings across multiple backends. There's a big difference with my [previous](https://github.com/open-webui/open-webui/pull/10018), I follow the suggestion of the author in [this](https://github.com/open-webui/open-webui/issues/8719#issuecomment-2663780910) comment: > Your implementation was for OpenAI only, if you take a look at the /chat/completion endpoint, you'll see we have a > compatibility layer that allows users to access models from other providers including Ollama.... For this, my PR: - Implements a unified `/api/embeddings` endpoint following the OpenWebUI generic dispatch pattern. - Now supports OpenAI, Ollama, Arena, pipeline and any compatible provider using OpenWebUI’s dynamic routing infrastructure. - Ensures complete OpenAI API compatibility for embeddings. ### Added - New route `/api/embeddings` that maps to the existing OpenAI embeddings functionality - Generic embeddings handler in `utils/embeddings.py` modeled after `utils/chat.py`. - Payload and response conversion utilities for OpenAI ↔️ Ollama embeddings compatibility. ### Changed - Refactored `/api/embeddings` endpoint in `main.py` to use the new, extensible dispatch system. - Applied code style, docstrings, and handler registration to match core OpenWebUI conventions. ### Deprecated - NA ### Removed - NA ### Fixed - This PR solves [this](https://github.com/open-webui/open-webui/issues/8719) feature request. ### Security - NA ### Breaking Changes - **feat** --- ### Additional Information - This update preserves backward compatibility and introduces a more user-friendly API endpoint. - The solution leverages existing logic from the OpenAI router to ensure consistency. - No breaking changes have been introduced, and no new dependencies are needed. - This PR addresses [feature request #8719](https://github.com/open-webui/open-webui/issues/8719). **Python code used for testing:** ```python import requests import json # API endpoint url = "http://localhost:3000/api/embeddings" # Headers headers = { "Content-Type": "application/json", "Authorization": "Bearer sk-12345678" # Replace with your actual token } # Request payload payload = { "model": "text-embedding-ada-002", "input": [ "Hello this is a single test", ] } # Make the POST request response = requests.post(url, headers=headers, json=payload) # Check if the request was successful response.raise_for_status() # Print the response print("Response:", response.json()) ``` **Response gotten:** ```plaintext Response: {'model': 'text-embedding-ada-002-v2', 'data': [{'embedding': [-0.017571303993463516, 0.022639181464910507, -0.003385236021131277, -0.02396933175623417, ...], 'index': 0, 'object': 'embedding'}], 'object': 'list', 'usage': {'completion_tokens': 0, 'prompt_tokens': 6, 'total_tokens': 6, 'completion_tokens_details': None, 'prompt_tokens_details': None}} ``` ### Screenshots or Videos ![image](https://github.com/user-attachments/assets/ce09e7b5-9dbe-4d30-ab02-c2fb7ea65751) ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2025-11-11 19:02:22 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#10342