Skip to content

Commit

Permalink
Merge pull request #13 from harmonydata/fix-env-google
Browse files Browse the repository at this point in the history
Fix loading env GOOGLE_APPLICATION_CREDENTIALS
  • Loading branch information
woodthom2 authored Dec 15, 2024
2 parents 6fd3e88 + c1c2d9a commit 5407710
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
51 changes: 24 additions & 27 deletions harmony_api/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,57 @@
SOFTWARE.
"""

import json
import os
from typing import Union

from pydantic import Field
from pydantic_settings import BaseSettings

GOOGLE_APPLICATION_CREDENTIALS = os.getenv("GOOGLE_APPLICATION_CREDENTIALS", '{}')
if GOOGLE_APPLICATION_CREDENTIALS:
if GOOGLE_APPLICATION_CREDENTIALS.startswith(
"{"): # only load JSON if it's JSON format
GOOGLE_APPLICATION_CREDENTIALS = json.loads(GOOGLE_APPLICATION_CREDENTIALS)


class Settings(BaseSettings):
# General harmony_api config
VERSION: str = "2.0"
APP_TITLE: str = "Harmony API"
TIKA_ENDPOINT: str = os.getenv("TIKA_ENDPOINT", "http://tika:9998")
OPENAI_API_KEY: str | None = os.getenv("OPENAI_API_KEY")
AZURE_OPENAI_API_KEY: str | None = os.getenv("AZURE_OPENAI_API_KEY")
AZURE_OPENAI_ENDPOINT: str | None = os.getenv("AZURE_OPENAI_ENDPOINT")
AZURE_STORAGE_URL: str | None = os.getenv("AZURE_STORAGE_URL")
GOOGLE_APPLICATION_CREDENTIALS: dict = GOOGLE_APPLICATION_CREDENTIALS
VERSION: str = Field(description="Application version.", default="2.0")
APP_TITLE: str = Field(description="Application title.", default="Harmony API")
TIKA_ENDPOINT: str = Field(description="Tika endpoint.", default="http://tika:9998")
OPENAI_API_KEY: str | None = Field(description="OpenAI API key.", default=None)
AZURE_OPENAI_API_KEY: str | None = Field(description="Azure OpenAI API key.", default=None)
AZURE_OPENAI_ENDPOINT: str | None = Field(description="Azure OpenAI endpoint.", default=None)
AZURE_STORAGE_URL: str | None = Field(description="Azure Storage URL.", default=None)
GOOGLE_APPLICATION_CREDENTIALS: str | None = Field(
description="A JSON string is expected here, this is the content of credentials.json.",
default=None
)


class DevSettings(Settings):
SERVER_HOST: str = "0.0.0.0"
DEBUG: bool = True
PORT: int = 8000
RELOAD: bool = True
CORS: dict = {
SERVER_HOST: str = Field(description="Host.", default="0.0.0.0")
DEBUG: bool = Field(description="Debug.", default=True)
PORT: int = Field(description="Port.", default=8000)
RELOAD: bool = Field(description="Reload.", default=True)
CORS: dict = Field(description="CORS.", default={
"origins": [
"*",
],
"allow_credentials": True,
"allow_methods": ["*"],
"allow_headers": ["*"],
}
})


class ProdSettings(Settings):
# TODO change
SERVER_HOST: str = "0.0.0.0"
DEBUG: bool = False
PORT: int = 8000
RELOAD: bool = False
CORS: dict = {
SERVER_HOST: str = Field(description="Host.", default="0.0.0.0")
DEBUG: bool = Field(description="Debug.", default=False)
PORT: int = Field(description="Port.", default=8000)
RELOAD: bool = Field(description="Reload.", default=False)
CORS: dict = Field(description="CORS.", default={
"origins": [
"*",
],
"allow_credentials": True,
"allow_methods": ["*"],
"allow_headers": ["*"],
}
})


def get_settings() -> Union[DevSettings | ProdSettings]:
Expand Down
2 changes: 1 addition & 1 deletion harmony_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def check_model_availability(model: dict) -> bool:

# Google
elif model["framework"] == "google":
if not settings.GOOGLE_APPLICATION_CREDENTIALS:
if not google_embeddings.GOOGLE_APPLICATION_CREDENTIALS_DICT:
return False

# Check model
Expand Down
33 changes: 23 additions & 10 deletions harmony_api/services/google_embeddings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import numpy as np
import vertexai
from google.api_core.exceptions import NotFound
Expand All @@ -14,20 +15,32 @@

settings = get_settings()

# Authenticate Vertex AI with Google service account
# Load GOOGLE_APPLICATION_CREDENTIALS as a dictionary
GOOGLE_APPLICATION_CREDENTIALS_DICT: dict | None = None
if settings.GOOGLE_APPLICATION_CREDENTIALS:
credentials = Credentials.from_service_account_info(
settings.GOOGLE_APPLICATION_CREDENTIALS,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
vertexai.init(
project=settings.GOOGLE_APPLICATION_CREDENTIALS["project_id"],
credentials=credentials,
)
try:
GOOGLE_APPLICATION_CREDENTIALS_DICT: dict = json.loads(settings.GOOGLE_APPLICATION_CREDENTIALS)
except (Exception,) as e:
print(f"The env GOOGLE_APPLICATION_CREDENTIALS is not a valid JSON, Google models will be unavailable. Error: {str(e)}")

# Authenticate Vertex AI with Google service account
if GOOGLE_APPLICATION_CREDENTIALS_DICT:
try:
credentials = Credentials.from_service_account_info(
GOOGLE_APPLICATION_CREDENTIALS_DICT,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
vertexai.init(
project=GOOGLE_APPLICATION_CREDENTIALS_DICT["project_id"],
credentials=credentials,
)
except (Exception,) as e:
GOOGLE_APPLICATION_CREDENTIALS_DICT = None
print(f"Error loading Google credentials: {str(e)}")

# Check available models
HARMONY_API_AVAILABLE_GOOGLE_MODELS_LIST: List[str] = []
if settings.GOOGLE_APPLICATION_CREDENTIALS:
if GOOGLE_APPLICATION_CREDENTIALS_DICT:
print("INFO:\t Checking Google models...")
for harmony_api_google_model in HARMONY_API_GOOGLE_MODELS_LIST:
try:
Expand Down

0 comments on commit 5407710

Please sign in to comment.