Skip to content

Commit

Permalink
fix: read api_key from env var for openai compatible server.
Browse files Browse the repository at this point in the history
  • Loading branch information
codito committed Oct 18, 2024
1 parent 19fcc74 commit dd37cb6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions arey/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def config_deserializer(cls, values: dict[str, Any]) -> dict[str, Any]:
# Fill in the model name if not explicitly provided
if "models" not in values:
raise AreyError("config", "`models` is not provided in configuration.")
for k, _ in values["models"].items(): # pyright: ignore[reportAny]
values["models"][k]["name"] = k
for k, v in values["models"].items(): # pyright: ignore[reportAny]
if "name" not in v:
values["models"][k]["name"] = k

# For chat and task config, let's fill the model config
for mode in ["chat", "task"]:
Expand Down
21 changes: 10 additions & 11 deletions arey/data/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ models:
openhermes25-mistral-7b:
path: TODO
type: gguf
template: chatml
# uncomment to specify configuration for llama-cpp
# n_threads: 10
# n_gpu_layers: 20
ollama-tinydolphin:
name: tinydolphin:latest # name of the model, see http://localhost:11434/api/tags
type: ollama
template: chatml
openai-example:
openai-example: # locally hosted openai compatible server, e.g. ollama server
name: "gemma2-9b-it"
type: openai
template: gemma2
base_url: "http://localhost:8080"
groq: # remote openai compatible server
name: llama-3.1-70b-versatile
type: openai
base_url: https://api.groq.com/openai/v1
api_key: env:GROQ_API_KEY # defined as env variable
# deepseek-coder-6.7b:
# path: ~/docs/models/deepseek-coder-6.7b/deepseek-coder-6.7b-instruct.Q5_K_M.gguf
# type: deepseek
# template: alpaca

profiles:
# See https://www.reddit.com/r/LocalLLaMA/comments/1343bgz/what_model_parameters_is_everyone_using/
Expand All @@ -45,12 +50,6 @@ profiles:
chat:
model: openhermes25-mistral-7b
profile: precise
# settings:
# n_threads: 11
# n_gpu_layers: 18
task:
model: openhermes25-mistral-7b
profile: precise
# settings:
# n_threads: 10
# n_gpu_layers: 20
8 changes: 7 additions & 1 deletion arey/platform/_openai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""OpenAI API based models."""

import os
import time
from collections.abc import Iterator
from typing import Any, override
Expand Down Expand Up @@ -28,7 +29,7 @@ class OpenAISettings(BaseModel):
"""Core model settings."""

base_url: str
api_key: str = "DUMMY KEY"
api_key: str = "DUMMY KEY" # or env:MY_API_KEY to read for env var


class OpenAIBaseModel(CompletionModel):
Expand All @@ -42,6 +43,11 @@ def __init__(self, model_config: ModelConfig) -> None:
self._model_name = model_config.name
self._model_settings = OpenAISettings(**model_config.settings) # pyright: ignore[reportAny]

# Fill the api key
if self._model_settings.api_key.startswith("env:"):
env_key = self._model_settings.api_key[4:]
self._model_settings.api_key = os.environ[env_key]

self._client = OpenAI(**self._model_settings.model_dump()) # pyright: ignore[reportAny]

@property
Expand Down

0 comments on commit dd37cb6

Please sign in to comment.