Skip to content

Commit

Permalink
Merge pull request #28 from Azure-Samples/writeenv
Browse files Browse the repository at this point in the history
Move away from "azd env get-values > .env"
  • Loading branch information
pamelafox authored Sep 27, 2024
2 parents 2c4762f + 9762b5b commit dec09ac
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.azure
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Use these values to connect to the Azure database from within the devcontainer
# Set these values to connect to the Azure database
# Use write_azure_env.sh or write_azure_env.ps1 to set these values
POSTGRES_DATABASE="db"
POSTGRES_HOST="YOUR-SERVER-NAME.postgres.database.azure.com"
POSTGRES_SSL="require"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ Follow these steps to deploy a PostgreSQL Flexible Server to Azure with the pgve

This will create a new resource group, and create the PostgreSQL Flexible server inside that group.

1. The example Python scripts look for configuration variables from a `.env` file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this command that copies the `azd` environment variables into your local `.env`:
1. The example Python scripts look for configuration variables from a `.env` file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this script that copies the necessary `azd` environment variables into your local `.env`:

```shell
azd env get-values > .env
./write_azure_env.sh
```

1. Now you may run the Python scripts in order to interact with the PostgreSQL server.
Expand Down
3 changes: 3 additions & 0 deletions examples/asyncpg_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ async def async_main():
POSTGRES_PASSWORD = os.environ["POSTGRES_PASSWORD"]

DATABASE_URI = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}/{POSTGRES_DATABASE}"
# Specify SSL mode if needed
if POSTGRES_SSL := os.environ.get("POSTGRES_SSL"):
DATABASE_URI += f"?sslmode={POSTGRES_SSL}"

conn = await asyncpg.connect(DATABASE_URI)

Expand Down
2 changes: 2 additions & 0 deletions examples/sqlalchemy_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Item(Base):
id: Mapped[int] = mapped_column(primary_key=True)
embedding = mapped_column(Vector(3))


# Define HNSW index to support vector similarity search through the vector_l2_ops access method (Euclidean distance). The SQL operator for Euclidean distance is written as <->.
index = Index(
"hnsw_index_for_euclidean_distance_similarity_search",
Expand All @@ -30,6 +31,7 @@ class Item(Base):
postgresql_ops={"embedding": "vector_l2_ops"},
)


async def insert_objects(async_session: async_sessionmaker[AsyncSession]) -> None:
async with async_session() as session:
async with session.begin():
Expand Down
1 change: 1 addition & 0 deletions examples/sqlalchemy_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Item(Base):
id: Mapped[int] = mapped_column(primary_key=True)
embedding = mapped_column(Vector(3))


# Define HNSW index to support vector similarity search through the vector_l2_ops access method (Euclidean distance). The SQL operator for Euclidean distance is written as <->.
index = Index(
"hnsw_index_for_euclidean_distance_similarity_search",
Expand Down
6 changes: 3 additions & 3 deletions examples/sqlalchemy_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Movie(Base):
title: Mapped[str] = mapped_column()
title_vector = mapped_column(Vector(1536)) # ada-002 is 1536-dimensional


# Define HNSW index to support vector similarity search through the vector_cosine_ops access method (cosine distance). The SQL operator for cosine distance is written as <=>.
index = Index(
"hnsw_index_for_cosine_distance_similarity_search",
Expand Down Expand Up @@ -81,9 +82,8 @@ class Movie(Base):

# Find the 5 most similar movies to "Winnie the Pooh"
most_similars = session.scalars(
select(Movie).order_by(
Movie.title_vector.cosine_distance(target_movie.title_vector)
).limit(5))
select(Movie).order_by(Movie.title_vector.cosine_distance(target_movie.title_vector)).limit(5)
)
print(f"Five most similar movies to '{target_movie.title}':")
for movie in most_similars:
print(f"\t{movie.title}")
13 changes: 13 additions & 0 deletions write_azure_env.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Clear the contents of the .env file
Set-Content -Path .env -Value ""

# Append new values to the .env file
$postgresDatabase = azd env get-value POSTGRES_DATABASE
$postgresHost = azd env get-value POSTGRES_HOST
$postgresSSL = azd env get-value POSTGRES_SSL
$postgresUsername = azd env get-value POSTGRES_USERNAME

Add-Content -Path .env -Value "POSTGRES_DATABASE=$postgresDatabase"
Add-Content -Path .env -Value "POSTGRES_HOST=$postgresHost"
Add-Content -Path .env -Value "POSTGRES_SSL=$postgresSSL"
Add-Content -Path .env -Value "POSTGRES_USERNAME=$postgresUsername"
10 changes: 10 additions & 0 deletions write_azure_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Clear the contents of the .env file
> .env

# Append new values to the .env file
echo "POSTGRES_DATABASE=$(azd env get-value POSTGRES_DATABASE)" >> .env
echo "POSTGRES_HOST=$(azd env get-value POSTGRES_HOST)" >> .env
echo "POSTGRES_SSL=$(azd env get-value POSTGRES_SSL)" >> .env
echo "POSTGRES_USERNAME=$(azd env get-value POSTGRES_USERNAME)" >> .env

0 comments on commit dec09ac

Please sign in to comment.