Skip to content

Commit b93b0ef

Browse files
author
ajosh0504
committed
Cleaning up utils, adding error handling
1 parent 93c0a19 commit b93b0ef

File tree

6 files changed

+105574
-125
lines changed

6 files changed

+105574
-125
lines changed

data/mongodb_docs_embeddings.json

Lines changed: 105514 additions & 107 deletions
Large diffs are not rendered by default.

labs/ai-rag-lab.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"outputs": [],
8181
"source": [
8282
"# Obtain a Voyage API key from our AI model proxy and set it as an environment variable-- DO NOT CHANGE\n",
83-
"set_env(\"VOYAGE_API_KEY\", PASSKEY)"
83+
"set_env([\"voyageai\"], PASSKEY)"
8484
]
8585
},
8686
{
@@ -764,8 +764,12 @@
764764
" messages = <CODE_BLOCK_17>\n",
765765
" # Send the chat messages to the AI model proxy get back an LLM response\n",
766766
" response = requests.post(url=SERVERLESS_URL, json={\"task\": \"completion\", \"data\": messages})\n",
767-
" # Print the final answer\n",
768-
" print(response.json()[\"text\"])"
767+
" if response.status_code == 200:\n",
768+
" # Print the final answer\n",
769+
" print(response.json()[\"text\"])\n",
770+
" else:\n",
771+
" # Print the error message\n",
772+
" print(response.json()[\"error\"])"
769773
]
770774
},
771775
{

labs/vector-search-lab.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"outputs": [],
7171
"source": [
7272
"# Obtain a Voyage API key from our AI model proxy and set it as an environment variable-- DO NOT CHANGE\n",
73-
"set_env(\"VOYAGE_API_KEY\", PASSKEY)"
73+
"set_env([\"voyageai\"], PASSKEY)"
7474
]
7575
},
7676
{

solutions/ai-rag-lab.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"outputs": [],
8181
"source": [
8282
"# Obtain a Voyage API key from our AI model proxy and set it as an environment variable-- DO NOT CHANGE\n",
83-
"set_env(\"VOYAGE_API_KEY\", PASSKEY)"
83+
"set_env([\"voyageai\"], PASSKEY)"
8484
]
8585
},
8686
{
@@ -853,8 +853,12 @@
853853
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
854854
" # Send the chat messages to the AI model proxy get back an LLM response\n",
855855
" response = requests.post(url=SERVERLESS_URL, json={\"task\": \"completion\", \"data\": messages})\n",
856-
" # Print the final answer\n",
857-
" print(response.json()[\"text\"])"
856+
" if response.status_code == 200:\n",
857+
" # Print the final answer\n",
858+
" print(response.json()[\"text\"])\n",
859+
" else:\n",
860+
" # Print the error message\n",
861+
" print(response.json()[\"error\"])"
858862
]
859863
},
860864
{

solutions/vector-search-lab.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"outputs": [],
7171
"source": [
7272
"# Obtain a Voyage API key from our AI model proxy and set it as an environment variable-- DO NOT CHANGE\n",
73-
"set_env(\"VOYAGE_API_KEY\", PASSKEY)"
73+
"set_env([\"voyageai\"], PASSKEY)"
7474
]
7575
},
7676
{

utils/utils.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from pymongo.errors import OperationFailure
22
from pymongo.collection import Collection
3+
from langchain_aws import ChatBedrock
4+
from langchain_openai import AzureChatOpenAI
5+
from langchain_google_genai import ChatGoogleGenerativeAI
36
import requests
4-
from typing import Dict
7+
from typing import Dict, List
58
import time
69
import os
710

811
SLEEP_TIMER = 5
9-
SERVERLESS_URL = os.getenv("SERVERLESS_URL")
12+
SERVERLESS_URL = "https://vtqjvgchmwcjwsrela2oyhlegu0hwqnw.lambda-url.us-west-2.on.aws/"
1013
SANDBOX_NAME = os.getenv("CODESPACE_NAME") or os.getenv("_SANDBOX_ID")
1114

1215

@@ -85,17 +88,48 @@ def track_progress(task: str, workshop_id: str) -> None:
8588
payload = {"task": task, "workshop_id": workshop_id, "sandbox_id": SANDBOX_NAME}
8689
requests.post(url=SERVERLESS_URL, json={"task": "track_progress", "data": payload})
8790

88-
def set_env(name: str, passkey: str) -> None:
91+
92+
def set_env(providers: List[str], passkey: str) -> None:
8993
"""
90-
Set environment variable in sandbox
94+
Set environment variables in sandbox
9195
9296
Args:
93-
name (str): Environment variable name
97+
providers (List[str]): List of provider names
9498
passkey (str): Passkey to get token
9599
"""
96-
response = requests.post(url=SERVERLESS_URL, json={"task": "get_token", "data": passkey})
97-
if response.status_code == 200:
98-
os.environ[name] = response.json().get("token")
99-
print(f"{name} environment variable set successfully.")
100+
for provider in providers:
101+
payload = {"provider": provider, "passkey": passkey}
102+
response = requests.post(url=SERVERLESS_URL, json={"task": "get_token", "data": payload})
103+
status_code = response.status_code
104+
if status_code == 200:
105+
result = response.json().get("token")
106+
for key in result:
107+
os.environ[key] = result[key]
108+
print(f"Set {key} environment variable.")
109+
elif status_code == 401:
110+
print(f"{response.json()['error']}. Follow steps in the lab documentation to obtain your own credentials and set them as environment variables.")
111+
else:
112+
print(f"{response.json()['error']}")
113+
114+
115+
def get_llm(provider: str):
116+
if provider == "aws":
117+
return ChatBedrock(
118+
model_id="anthropic.claude-3-5-sonnet-20240620-v1:0",
119+
model_kwargs=dict(temperature=0),
120+
region_name="us-west-2",
121+
)
122+
elif provider == "google":
123+
return ChatGoogleGenerativeAI(
124+
model="gemini-1.5-pro",
125+
temperature=0,
126+
)
127+
elif provider == "microsoft":
128+
return AzureChatOpenAI(
129+
azure_endpoint="https://gai-326.openai.azure.com/",
130+
azure_deployment="gpt-4o",
131+
api_version="2023-06-01-preview",
132+
temperature=0,
133+
)
100134
else:
101-
print("Passkey expired. Follow steps in the lab documentation to obtain your own credentials and set them as environment variables.")
135+
print("Unsupported provider. provider can be one of 'aws', 'google', 'microsoft'.")

0 commit comments

Comments
 (0)