Minor improvements including consistently setting override to True when loading dotenv

This commit is contained in:
Edward Donner
2025-03-01 15:03:18 -05:00
parent 41d958dba6
commit 8338dfc248
26 changed files with 89 additions and 43 deletions

View File

@@ -427,7 +427,12 @@
"with: \n",
"`import httpx` \n",
"`openai = OpenAI(http_client=httpx.Client(verify=False))` \n",
"And if that works, you're in good shape. You'll just have to change the labs in the same way any time you hit this cert error.\n",
"And also please replace: \n",
"`requests.get(url, headers=headers)` \n",
"with: \n",
"`requests.get(url, headers=headers, verify=False)` \n",
"And if that works, you're in good shape. You'll just have to change the labs in the same way any time you hit this cert error. \n",
"This approach isn't OK for production code, but it's fine for our experiments. You may need to contact IT support to understand whether there are restrictions in your environment.\n",
"\n",
"## If all else fails:\n",
"\n",

View File

@@ -82,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 155,
"execution_count": null,
"id": "0a521d84-d07c-49ab-a0df-d6451499ed97",
"metadata": {},
"outputs": [],
@@ -116,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 156,
"execution_count": null,
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
"metadata": {},
"outputs": [],
@@ -212,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 157,
"execution_count": null,
"id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2",
"metadata": {},
"outputs": [],
@@ -223,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": 158,
"execution_count": null,
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85",
"metadata": {},
"outputs": [],
@@ -373,7 +373,7 @@
},
{
"cell_type": "code",
"execution_count": 159,
"execution_count": null,
"id": "39fb9008",
"metadata": {},
"outputs": [],
@@ -475,7 +475,7 @@
},
{
"cell_type": "code",
"execution_count": 160,
"execution_count": null,
"id": "1f003836",
"metadata": {},
"outputs": [],
@@ -547,7 +547,7 @@
},
{
"cell_type": "code",
"execution_count": 161,
"execution_count": null,
"id": "f6b34b32",
"metadata": {},
"outputs": [],
@@ -618,7 +618,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "llm_env",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -632,7 +632,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.11"
}
},
"nbformat": 4,

View File

@@ -11,14 +11,6 @@
"\n",
"https://colab.research.google.com/drive/1WD6Y2N7ctQi1X9wa6rpkg8UfyA4iSVuz?usp=sharing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9289ba7-200c-43a9-b67a-c5ce826c9537",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {

View File

@@ -86,7 +86,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')"
]

View File

@@ -69,7 +69,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -57,7 +57,7 @@
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"openai = OpenAI()"
]

View File

@@ -64,7 +64,7 @@
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')"
]
},

View File

@@ -70,7 +70,7 @@
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')"
]
},

View File

@@ -71,7 +71,7 @@
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')"
]
},

View File

@@ -72,7 +72,7 @@
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')"
]
},

View File

@@ -76,7 +76,7 @@
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')"
]
},

View File

@@ -47,7 +47,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -60,7 +60,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -118,7 +118,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -69,7 +69,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -69,7 +69,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -61,7 +61,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
@@ -904,7 +904,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.10"
"version": "3.11.11"
}
},
"nbformat": 4,

View File

@@ -61,7 +61,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"

View File

@@ -92,7 +92,7 @@
"metadata": {},
"outputs": [],
"source": [
"from hello import app, hello"
"from hello import app, hello, hello_europe"
]
},
{
@@ -119,6 +119,35 @@
"reply"
]
},
{
"cell_type": "markdown",
"id": "a1c075e9-49c7-4ebd-812f-83196d32de32",
"metadata": {},
"source": [
"## Added thanks to student Tue H.\n",
"\n",
"If you look in hello.py, I've added a simple function hello_europe\n",
"\n",
"That uses the decorator: \n",
"`@app.function(image=image, region=\"eu\")`\n",
"\n",
"See the result below! More region specific settings are [here](https://modal.com/docs/guide/region-selection)\n",
"\n",
"Note that it does consume marginally more credits to specify a region."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b027da1a-c79d-42cb-810d-32ddca31aa02",
"metadata": {},
"outputs": [],
"source": [
"with app.run():\n",
" reply=hello_europe.remote()\n",
"reply"
]
},
{
"cell_type": "markdown",
"id": "22e8d804-c027-45fb-8fef-06e7bba6295a",
@@ -247,8 +276,8 @@
"metadata": {},
"outputs": [],
"source": [
"# You can also run \"modal deploy pricer_service2\" at the command line in an activated environment\n",
"!modal deploy pricer_service2"
"# You can also run \"modal deploy -m pricer_service2\" at the command line in an activated environment\n",
"!modal deploy -m pricer_service2"
]
},
{
@@ -264,6 +293,16 @@
"print(reply)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c29b8c58-4cb7-44b0-ab7e-6469d3a318e8",
"metadata": {},
"outputs": [],
"source": [
"!pip install --upgrade modal"
]
},
{
"cell_type": "markdown",
"id": "9c1b1451-6249-4462-bf2d-5937c059926c",

View File

@@ -58,7 +58,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')\n",
"DB = \"products_vectorstore\""

View File

@@ -61,7 +61,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
]

View File

@@ -79,7 +79,7 @@
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
]

View File

@@ -35,7 +35,7 @@
"source": [
"# Initialize and constants\n",
"\n",
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"MODEL = 'gpt-4o-mini'\n",
"openai = OpenAI()"

View File

@@ -42,7 +42,7 @@
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"load_dotenv(override=True)\n",
"DB = \"products_vectorstore\""
]
},

View File

@@ -16,3 +16,14 @@ def hello() -> str:
data = response.json()
city, region, country = data['city'], data['region'], data['country']
return f"Hello from {city}, {region}, {country}!!"
# New - added thanks to student Tue H.!
@app.function(image=image, region="eu")
def hello_europe() -> str:
import requests
response = requests.get('https://ipinfo.io/json')
data = response.json()
city, region, country = data['city'], data['region'], data['country']
return f"Hello from {city}, {region}, {country}!!"

View File

@@ -24,7 +24,6 @@ FINETUNED_DIR = MODEL_DIR + FINETUNED_MODEL
QUESTION = "How much does this cost to the nearest dollar?"
PREFIX = "Price is $"
@app.cls(image=image, secrets=secrets, gpu=GPU, timeout=1800)
class Pricer:
@modal.build()