Repository navigation
feat(config): route embeddings through named providers #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zeming-Yuan
wants to merge
2
commits into
EverMind-AI:main
Choose a base branch
from
Zeming-Yuan:feat/issue-666-embedding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Shared named-provider endpoint selection and OpenAI header isolation.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass | ||
|
|
||
| from everos.config import EndpointAuth, ProviderSettings | ||
|
|
||
| try: | ||
| from openai import Omit | ||
| except ImportError: # OpenAI 1.x exposed the sentinel only internally. | ||
| from openai._types import Omit | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ProviderConnection: | ||
| """A selected endpoint with a safe configuration source label.""" | ||
|
|
||
| base_url: str | ||
| source: str | ||
| auth: EndpointAuth | ||
| endpoint: str | None | ||
| implicit_endpoint: str | None | ||
|
|
||
|
|
||
| def resolve_provider_connection( | ||
| provider: ProviderSettings, | ||
| *, | ||
| origin: str, | ||
| path: str, | ||
| selected: str | None, | ||
| error_type: type[Exception] = ValueError, | ||
| ) -> ProviderConnection: | ||
| """Choose one OpenAI-compatible endpoint, preserving section-specific errors.""" | ||
| matches = [n for n, ep in provider.endpoints.items() if ep.api_type == "openai"] | ||
| implicit = matches[0] if len(matches) == 1 else None | ||
| selected = selected or implicit | ||
| if selected is None: | ||
| if provider.base_url is None: | ||
| raise error_type( | ||
| f"[{path}] needs one openai endpoint on [{origin}], " | ||
| f"found {len(matches)} ({', '.join(matches) or 'none'}). " | ||
| "Select a compatible endpoint by name." | ||
| ) | ||
| return ProviderConnection(provider.base_url, origin, "bearer", None, implicit) | ||
| endpoint = provider.endpoints.get(selected) | ||
| if endpoint is None: | ||
| raise error_type(f"[{path}]: unknown endpoint {selected!r} on [{origin}]") | ||
| source = f"{origin}.endpoints.{selected}" | ||
| if endpoint.api_type != "openai": | ||
| raise error_type( | ||
| f"[{path}] requires openai, but [{source}] uses {endpoint.api_type}" | ||
| ) | ||
| return ProviderConnection( | ||
| endpoint.base_url, source, endpoint.auth, selected, implicit | ||
| ) | ||
|
|
||
|
|
||
| def isolated_openai_headers( | ||
| headers: Mapping[str, str], *, api_key: str, auth: EndpointAuth | ||
| ) -> dict[str, str | Omit]: | ||
| """Omit SDK ambient identity and headers, then install this provider's values.""" | ||
| if any(name.lower() == "authorization" for name in headers): | ||
| raise ValueError("Authorization is reserved; configure auth and api_key") | ||
| return { | ||
| **{ | ||
| line.partition(":")[0].strip(): Omit() | ||
| for line in os.environ.get("OPENAI_CUSTOM_HEADERS", "").splitlines() | ||
| if ":" in line | ||
| }, | ||
| "OpenAI-Organization": Omit(), | ||
| "OpenAI-Project": Omit(), | ||
| **headers, | ||
| "Authorization": f"Bearer {api_key}" if auth == "bearer" else Omit(), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.