Skip to content

feat: OpenAI-compatible /api/embeddings endpoint with provider-agnostic OpenWebUI architecture#14667

Merged
tjbck merged 7 commits intoopen-webui:devfrom
hdnh2006:main
Jun 4, 2025
Merged

feat: OpenAI-compatible /api/embeddings endpoint with provider-agnostic OpenWebUI architecture#14667
tjbck merged 7 commits intoopen-webui:devfrom
hdnh2006:main

Conversation

@hdnh2006
Copy link
Contributor

@hdnh2006 hdnh2006 commented Jun 4, 2025

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.

@hdnh2006 hdnh2006 changed the title feat: OpenAI-compatible /api/embeddings endpoint with provider-agnostic OpenWebUI architecture feat: OpenAI-compatible /api/embeddings endpoint with provider-agnostic OpenWebUI architecture Jun 4, 2025
@Ithanil
Copy link
Contributor

Ithanil commented Jun 4, 2025

Funny, I added a comment to #13898 just one hour ago, hoping for it to get picked up again. But this PR looks much cleaner at a first glance, very nice!

@hdnh2006
Copy link
Contributor Author

hdnh2006 commented Jun 4, 2025

Funny, I added a comment to #13898 just one hour ago, hoping for it to get picked up again. But this PR looks much cleaner at a first glance, very nice!

Yes, I hope this PR meets the author's quality requirements. Let's cross finger...

@Ithanil
Copy link
Contributor

Ithanil commented Jun 4, 2025

It seems to be well separated and maintainable code, so I'm going to use it anyway. 😅 I really want this for our users.

@hdnh2006
Copy link
Contributor Author

hdnh2006 commented Jun 4, 2025

Hi @Ithanil,

The code with your suggestions has been pushed.

Thanks for your feedback.

@Ithanil
Copy link
Contributor

Ithanil commented Jun 4, 2025

Tested your implementation both with your suggested Python code as well as the Continue.dev Extension for VSCode. Works for admin user as well as regular user, but only if he has access to the embeddings model. Perfect!

@tjbck
Copy link
Contributor

tjbck commented Jun 4, 2025

Amazing addition, will refactor here and there but this can be merged as it is! Thanks for the hard work!

@tjbck tjbck merged commit 14e158f into open-webui:dev Jun 4, 2025
@hdnh2006
Copy link
Contributor Author

hdnh2006 commented Jun 5, 2025

Thank you @tjbck! Your kind words motivate me to keep working hard and to continue loving coding while contributing to the open source community.

@sthemeow
Copy link

It doesn't seem to be working with embedding models through external API instead of local Ollama. Would you be able to help that out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants