Skip to content

Commit

Permalink
fix: fix yaml settings not loaded
Browse files Browse the repository at this point in the history
closes #6 and #5
  • Loading branch information
AlmogBaku committed May 22, 2024
1 parent 48f9f33 commit 46b56fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
11 changes: 5 additions & 6 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,21 @@ async def lifespan(app: FastAPI):
organization=settings.openai_organization
)

if ((settings.openai_base_url is None)
and settings.openai_api_key
and (settings.models.oai_urls is None or (oai_default_base_url not in settings.models.oai_urls))):
if settings.openai_base_url is None and settings.openai_api_key and settings.models.oai_urls is None:
if settings.models.oai_urls is None:
settings.models.oai_urls = []
settings.models.oai_urls.append(oai_default_base_url)

for url in settings.models.oai_urls or []:
base_url = url.rsplit("/models")[0] if url.startswith(
settings.openai_base_url or oai_default_base_url) else None
vendor = "OpenAI" if "openai" in url else urlparse(url).hostname.rsplit(".", 1)[0].rsplit(".", 1)[-1]

cli = AsyncOpenAI(
api_key=settings.openai_api_key,
base_url=url,
)
resp = await cli.models.list()
base_url = url.rsplit("/models")[0] if url.startswith(
settings.openai_base_url or "https://api.openai.com/v1") else None
vendor = "OpenAI" if "openai" in url else urlparse(url).hostname.rsplit(".", 1)[0].rsplit(".", 1)[-1]

models += [
Model(name=model.id, system_prompt=True, type='chat', vendor=vendor, base_url=base_url)
Expand Down
21 changes: 19 additions & 2 deletions server/src/protocol.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from typing import Literal, Optional, List, Union
from typing import Literal, Optional, List, Union, Type, Tuple

from openai.types.chat import ChatCompletionMessageParam
from pydantic import BaseModel, Field, SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
YamlConfigSettingsSource,
)


class Model(BaseModel):
Expand Down Expand Up @@ -33,6 +38,18 @@ class Settings(BaseSettings):

dist_dir: Optional[str] = None

@classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:

return YamlConfigSettingsSource(settings_cls), env_settings, dotenv_settings, file_secret_settings


class ChatCompletionsRequest(BaseModel):
model: str
Expand Down

0 comments on commit 46b56fb

Please sign in to comment.