Skip to content

Commit

Permalink
feat(integration-service): added env file to load the API_KEYS for va…
Browse files Browse the repository at this point in the history
…rious integrations (#855)

<!-- ELLIPSIS_HIDDEN -->



> [!IMPORTANT]
> Add environment variable handling for API keys in integration services
using `environs`.
> 
>   - **Environment Variables**:
> - Add `env.py` to load API keys and other sensitive data using
`environs`.
> - Update `docker-compose.yml` to include new environment variables for
integrations.
>   - **Integration Updates**:
> - Modify `brave.py`, `browserbase.py`, `cloudinary.py`, `email.py`,
`llama_parse.py`, `spider.py`, and `weather.py` to use environment
variables for API keys.
> - Replace hardcoded 'DEMO_API_KEY' and similar placeholders with
actual environment variables.
>   - **Dependencies**:
> - Add `environs` to `pyproject.toml` for environment variable
management.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup>
for 65d5c0a. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
Vedantsahai18 authored Nov 21, 2024
1 parent c957c33 commit 6ebe65e
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 8 deletions.
15 changes: 14 additions & 1 deletion integrations-service/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ name: julep-integrations

# Shared environment variables
x--shared-environment: &shared-environment
OPENAI_API_KEY: ${OPENAI_API_KEY}
INTEGRATIONS_SERVICE_PORT: ${INTEGRATIONS_SERVICE_PORT:-8000}
RAPID_API_KEY: ${RAPID_API_KEY}
RAPID_API_HOST: ${RAPID_API_HOST}
ARYSHARE_KEY: ${ARYSHARE_KEY}
ARYSHARE_PROFILE_ID: ${ARYSHARE_PROFILE_ID}
BROWSERBASE_API_KEY: ${BROWSERBASE_API_KEY}
BROWSERBASE_PROJECT_ID: ${BROWSERBASE_PROJECT_ID}
OPENWEATHER_API_KEY: ${OPENWEATHER_API_KEY}
SPIDER_API_KEY: ${SPIDER_API_KEY}
BRAVE_API_KEY: ${BRAVE_API_KEY}
LLAMA_API_KEY: ${LLAMA_API_KEY}
CLOUDINARY_API_KEY: ${CLOUDINARY_API_KEY}
CLOUDINARY_API_SECRET: ${CLOUDINARY_API_SECRET}
CLOUDINARY_CLOUD_NAME: ${CLOUDINARY_CLOUD_NAME}
MAILGUN_PASSWORD: ${MAILGUN_PASSWORD}

services:
integrations:
Expand Down
21 changes: 21 additions & 0 deletions integrations-service/integrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from environs import Env

# Initialize the Env object for environment variable parsing.
env = Env()
env.read_env() # Read .env file, if it exists

# Load environment variables
rapid_api_key = env.str("RAPID_API_KEY")
rapid_api_host = env.str("RAPID_API_HOST")
aryshare_key = env.str("ARYSHARE_KEY")
aryshare_profile_id = env.str("ARYSHARE_PROFILE_ID")
browserbase_api_key = env.str("BROWSERBASE_API_KEY")
browserbase_project_id = env.str("BROWSERBASE_PROJECT_ID")
openweather_api_key = env.str("OPENWEATHER_API_KEY")
spider_api_key = env.str("SPIDER_API_KEY")
brave_api_key = env.str("BRAVE_API_KEY")
llama_api_key = env.str("LLAMA_API_KEY")
cloudinary_api_key = env.str("CLOUDINARY_API_KEY")
cloudinary_api_secret = env.str("CLOUDINARY_API_SECRET")
cloudinary_cloud_name = env.str("CLOUDINARY_CLOUD_NAME")
mailgun_password = env.str("MAILGUN_PASSWORD")
5 changes: 5 additions & 0 deletions integrations-service/integrations/utils/integrations/brave.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tenacity import retry, stop_after_attempt, wait_exponential

from ...autogen.Tools import BraveSearchArguments, BraveSearchSetup
from ...env import brave_api_key # Import env to access environment variables
from ...models import BraveSearchOutput, SearchResult


Expand All @@ -24,6 +25,10 @@ async def search(
assert isinstance(setup, BraveSearchSetup), "Invalid setup"
assert isinstance(arguments, BraveSearchArguments), "Invalid arguments"

# Check if the setup.api_key is 'DEMO_API_KEY' and load from environment if true
if setup.api_key == "DEMO_API_KEY":
setup.api_key = brave_api_key

tool = BraveSearch.from_api_key(api_key=setup.api_key, search_kwargs={"count": 3})

result = tool.run(arguments.query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
BrowserbaseListSessionsArguments,
BrowserbaseSetup,
)
from ...env import ( # Import env to access environment variables
browserbase_api_key,
browserbase_project_id,
)
from ...models import (
BrowserbaseCompleteSessionOutput,
BrowserbaseCreateSessionOutput,
Expand All @@ -34,6 +38,11 @@


def get_browserbase_client(setup: BrowserbaseSetup) -> Browserbase:
if setup.api_key == "DEMO_API_KEY":
setup.api_key = browserbase_api_key
if setup.project_id == "DEMO_PROJECT_ID":
setup.project_id = browserbase_project_id

return Browserbase(
api_key=setup.api_key,
project_id=setup.project_id,
Expand Down Expand Up @@ -71,6 +80,9 @@ async def create_session(
) -> BrowserbaseCreateSessionOutput:
client = get_browserbase_client(setup)

if arguments.project_id == "DEMO_PROJECT_ID":
arguments.project_id = setup.browserbase_project_id

options = CreateSessionOptions(
projectId=arguments.project_id or setup.project_id,
extensionId=arguments.extension_id,
Expand Down
29 changes: 23 additions & 6 deletions integrations-service/integrations/utils/integrations/cloudinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
CloudinarySetup,
CloudinaryUploadArguments,
)
from ...env import ( # Import env to access environment variables
cloudinary_api_key,
cloudinary_api_secret,
cloudinary_cloud_name,
)
from ...models.cloudinary import CloudinaryEditOutput, CloudinaryUploadOutput


Expand All @@ -32,9 +37,15 @@ async def media_upload(
try:
# Configure Cloudinary with credentials
cloudinary.config(
cloud_name=setup.cloudinary_cloud_name,
api_key=setup.cloudinary_api_key,
api_secret=setup.cloudinary_api_secret,
cloud_name=setup.cloudinary_cloud_name
if setup.cloudinary_cloud_name != "DEMO_CLOUD_NAME"
else cloudinary_cloud_name,
api_key=setup.cloudinary_api_key
if setup.cloudinary_api_key != "DEMO_API_KEY"
else cloudinary_api_key,
api_secret=setup.cloudinary_api_secret
if setup.cloudinary_api_secret != "DEMO_API_SECRET"
else cloudinary_api_secret,
**(setup.params or {}),
)

Expand Down Expand Up @@ -93,9 +104,15 @@ async def media_edit(
try:
# Configure Cloudinary with credentials
cloudinary.config(
cloud_name=setup.cloudinary_cloud_name,
api_key=setup.cloudinary_api_key,
api_secret=setup.cloudinary_api_secret,
cloud_name=setup.cloudinary_cloud_name
if setup.cloudinary_cloud_name != "DEMO_CLOUD_NAME"
else cloudinary_cloud_name,
api_key=setup.cloudinary_api_key
if setup.cloudinary_api_key != "DEMO_API_KEY"
else cloudinary_api_key,
api_secret=setup.cloudinary_api_secret
if setup.cloudinary_api_secret != "DEMO_API_SECRET"
else cloudinary_api_secret,
**(setup.params or {}),
)

Expand Down
4 changes: 4 additions & 0 deletions integrations-service/integrations/utils/integrations/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tenacity import retry, stop_after_attempt, wait_exponential

from ...autogen.Tools import EmailArguments, EmailSetup
from ...env import mailgun_password # Import env to access environment variables
from ...models import EmailOutput


Expand All @@ -25,6 +26,9 @@ async def send(setup: EmailSetup, arguments: EmailArguments) -> EmailOutput:
message["From"] = arguments.from_
message["To"] = arguments.to

if setup.password == "DEMO_PASSWORD":
setup.password = mailgun_password

with SMTP(setup.host, setup.port) as server:
server.login(setup.user, setup.password)
server.send_message(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tenacity import retry, stop_after_attempt, wait_exponential

from ...autogen.Tools import LlamaParseFetchArguments, LlamaParseSetup
from ...env import llama_api_key # Import env to access environment variables
from ...models import LlamaParseFetchOutput


Expand All @@ -25,6 +26,9 @@ async def parse(
assert isinstance(setup, LlamaParseSetup), "Invalid setup"
assert isinstance(arguments, LlamaParseFetchArguments), "Invalid arguments"

if setup.llamaparse_api_key == "DEMO_API_KEY":
setup.llamaparse_api_key = llama_api_key

parser = LlamaParse(
api_key=setup.llamaparse_api_key,
result_type=arguments.result_format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tenacity import retry, stop_after_attempt, wait_exponential

from ...autogen.Tools import SpiderFetchArguments, SpiderSetup
from ...env import spider_api_key # Import env to access environment variables
from ...models import SpiderFetchOutput


Expand All @@ -27,6 +28,9 @@ async def crawl(
if not url:
raise ValueError("URL parameter is required for spider")

if setup.spider_api_key == "DEMO_API_KEY":
setup.spider_api_key = spider_api_key

spider_loader = SpiderLoader(
api_key=setup.spider_api_key,
url=str(url),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tenacity import retry, stop_after_attempt, wait_exponential

from ...autogen.Tools import WeatherGetArguments, WeatherSetup
from ...env import openweather_api_key # Import env to access environment variables
from ...models import WeatherGetOutput


Expand All @@ -23,6 +24,9 @@ async def get(setup: WeatherSetup, arguments: WeatherGetArguments) -> WeatherGet
location = arguments.location

openweathermap_api_key = setup.openweathermap_api_key
if openweathermap_api_key == "DEMO_API_KEY":
openweathermap_api_key = openweather_api_key

if not location:
raise ValueError("Location parameter is required for weather data")

Expand Down
22 changes: 21 additions & 1 deletion integrations-service/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integrations-service/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pillow = "^11.0.0"
llama-index = "^0.11.22"
llama-parse = "^0.5.13"
cloudinary = "^1.41.0"
environs = "^11.2.1"

[tool.poe.tasks]
format = "ruff format"
Expand Down

0 comments on commit 6ebe65e

Please sign in to comment.