Skip to content

Commit

Permalink
feat: Support custom base URL via env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrosquall committed Jun 1, 2024
1 parent 89fe392 commit 5b8dd0a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Used by dotenv package to load environment variables in scripts and python_server
# Copy this file to .env and replace the XXX with the actual keys / URLs
OPENAI_API_KEY=XXX
OPENAI_BASE_URL=XXX # optional, defaults to https://api.openai.com

VOYAGE_API_KEY=XXX
TOGETHER_API_KEY=XXX
COHERE_API_KEY=XXX
MISTRAL_API_KEY=XXX
LATENT_SCOPE_DATA='~/latent-scope-data'

LATENT_SCOPE_DATA='~/latent-scope-data'
10 changes: 8 additions & 2 deletions latentscope/models/providers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ def load_model(self):
if api_key is None:
print("ERROR: No API key found for OpenAI")
print("Missing 'OPENAI_API_KEY' variable in:", f"{os.getcwd()}/.env")
self.client = OpenAI(api_key=api_key)

base_url = get_key("OPENAI_BASE_URL")
if base_url is not None:
self.client = OpenAI(api_key=api_key, base_url=base_url)
else:
self.client = OpenAI(api_key=api_key)

self.encoder = tiktoken.encoding_for_model(self.name)

def embed(self, inputs, dimensions=None):
Expand Down Expand Up @@ -48,4 +54,4 @@ def chat(self, messages):
model=self.name,
messages=messages
)
return response.choices[0].message.content
return response.choices[0].message.content

0 comments on commit 5b8dd0a

Please sign in to comment.