Skip to content

Commit e09e00a

Browse files
committed
Replace os.getenv with os.environ so that we can get an Environment Variable not found error when an environment variable isn't set
1 parent eb82320 commit e09e00a

File tree

7 files changed

+46
-37
lines changed

7 files changed

+46
-37
lines changed

src/copilot_flow/copilot.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class ChatResponse(TypedDict):
2121
@trace
2222
def get_chat_response(chat_input: str, chat_history: list = []) -> ChatResponse:
2323
model_config = AzureOpenAIModelConfiguration(
24-
azure_deployment=os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT"),
25-
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
26-
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
24+
azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"],
25+
api_version=os.environ["AZURE_OPENAI_API_VERSION"],
26+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"]
2727
)
2828

2929
searchQuery = chat_input
@@ -64,23 +64,26 @@ def get_chat_response(chat_input: str, chat_history: list = []) -> ChatResponse:
6464
@trace
6565
def get_documents(search_query: str, num_docs=3):
6666

67-
index_name = os.getenv("AZUREAI_SEARCH_INDEX_NAME")
67+
index_name = os.environ["AZUREAI_SEARCH_INDEX_NAME"]
6868

6969
# retrieve documents relevant to the user's question from Cognitive Search
7070
search_client = SearchClient(
71-
endpoint=os.getenv("AZURE_SEARCH_ENDPOINT"),
71+
endpoint=os.environ["AZURE_SEARCH_ENDPOINT"],
7272
credential=DefaultAzureCredential(),
73-
index_name=index_name)
73+
index_name=index_name
74+
)
7475

7576
aoai_client = AzureOpenAI(
76-
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
77+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
7778
azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
78-
api_version=os.getenv("AZURE_OPENAI_API_VERSION")
79+
api_version=os.environ["AZURE_OPENAI_API_VERSION"]
7980
)
8081

8182
# generate a vector embedding of the user's question
82-
embedding = aoai_client.embeddings.create(input=search_query,
83-
model=os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT"))
83+
embedding = aoai_client.embeddings.create(
84+
input=search_query,
85+
model=os.environ["AZURE_OPENAI_EMBEDDING_DEPLOYMENT"]
86+
)
8487
embedding_to_query = embedding.data[0].embedding
8588

8689
context = ""

src/deployment/deploy.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ def deploy_flow(endpoint_name, deployment_name):
6868
"PRT_CONFIG_OVERRIDE": f"deployment.subscription_id={client.subscription_id},deployment.resource_group={client.resource_group_name},deployment.workspace_name={client.workspace_name},deployment.endpoint_name={endpoint_name},deployment.deployment_name={deployment_name}",
6969
# the following is enabled by secret injection
7070
# make sure your environment variables here match the environment variables your code depends on
71-
'AZURE_OPENAI_ENDPOINT': os.getenv('AZURE_OPENAI_ENDPOINT'),
72-
'AZURE_SEARCH_ENDPOINT': os.getenv('AZURE_SEARCH_ENDPOINT'),
73-
'AZURE_OPENAI_API_VERSION': os.getenv('AZURE_OPENAI_API_VERSION'),
74-
'AZURE_OPENAI_CHAT_DEPLOYMENT': os.getenv('AZURE_OPENAI_CHAT_DEPLOYMENT'),
75-
'AZURE_OPENAI_EVALUATION_DEPLOYMENT': os.getenv('AZURE_OPENAI_EVALUATION_DEPLOYMENT'),
76-
'AZURE_OPENAI_EMBEDDING_DEPLOYMENT': os.getenv('AZURE_OPENAI_EMBEDDING_DEPLOYMENT'),
77-
'AZUREAI_SEARCH_INDEX_NAME': os.getenv('AZUREAI_SEARCH_INDEX_NAME')
71+
'AZURE_OPENAI_ENDPOINT': os.environ['AZURE_OPENAI_ENDPOINT'],
72+
'AZURE_SEARCH_ENDPOINT': os.environ['AZURE_SEARCH_ENDPOINT'],
73+
'AZURE_OPENAI_API_VERSION': os.environ['AZURE_OPENAI_API_VERSION'],
74+
'AZURE_OPENAI_CHAT_DEPLOYMENT': os.environ['AZURE_OPENAI_CHAT_DEPLOYMENT'],
75+
'AZURE_OPENAI_EVALUATION_DEPLOYMENT': os.environ['AZURE_OPENAI_EVALUATION_DEPLOYMENT'],
76+
'AZURE_OPENAI_EMBEDDING_DEPLOYMENT': os.environ['AZURE_OPENAI_EMBEDDING_DEPLOYMENT'],
77+
'AZUREAI_SEARCH_INDEX_NAME': os.environ['AZUREAI_SEARCH_INDEX_NAME']
7878
}
7979
)
8080

@@ -90,20 +90,23 @@ def deploy_flow(endpoint_name, deployment_name):
9090

9191
# 4. provide endpoint access to Azure Open AI resource
9292
create_role_assignment(
93-
scope=f"/subscriptions/{os.getenv("AZURE_SUBSCRIPTION_ID")}/resourceGroups/{os.getenv("AZURE_RESOURCE_GROUP")}/providers/Microsoft.CognitiveServices/accounts/{os.getenv("AZURE_OPENAI_CONNECTION_NAME")}",
93+
scope=f"/subscriptions/{os.environ["AZURE_SUBSCRIPTION_ID"]}/resourceGroups/{os.environ["AZURE_RESOURCE_GROUP"]}/providers/Microsoft.CognitiveServices/accounts/{os.environ["AZURE_OPENAI_CONNECTION_NAME"]}",
9494
role_name="Cognitive Services OpenAI User",
95-
principal_id=endpoint.identity.principal_id)
95+
principal_id=endpoint.identity.principal_id
96+
)
9697

9798
# 5. provide endpoint access to Azure AI Search resource
9899
create_role_assignment(
99-
scope=f"/subscriptions/{os.getenv("AZURE_SUBSCRIPTION_ID")}/resourceGroups/{os.getenv("AZURE_RESOURCE_GROUP")}/providers/Microsoft.Search/searchServices/{os.getenv("AZURE_SEARCH_CONNECTION_NAME")}",
100+
scope=f"/subscriptions/{os.environ["AZURE_SUBSCRIPTION_ID"]}/resourceGroups/{os.environ["AZURE_RESOURCE_GROUP"]}/providers/Microsoft.Search/searchServices/{os.environ["AZURE_SEARCH_CONNECTION_NAME"]}",
100101
role_name="Search Index Data Contributor",
101-
principal_id=endpoint.identity.principal_id)
102+
principal_id=endpoint.identity.principal_id
103+
)
102104

103105
output_deployment_details(
104106
client=client,
105107
endpoint_name=endpoint_name,
106-
deployment_name=deployment_name)
108+
deployment_name=deployment_name
109+
)
107110

108111
def create_role_assignment(scope, role_name, principal_id):
109112

@@ -113,7 +116,8 @@ def create_role_assignment(scope, role_name, principal_id):
113116
# Instantiate the authorization management client
114117
auth_client = AuthorizationManagementClient(
115118
credential=credential,
116-
subscription_id=os.getenv("AZURE_SUBSCRIPTION_ID"))
119+
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"]
120+
)
117121

118122
roles = list(auth_client.role_definitions.list(
119123
scope,
@@ -126,13 +130,15 @@ def create_role_assignment(scope, role_name, principal_id):
126130
parameters = RoleAssignmentCreateParameters(
127131
role_definition_id=role.id,
128132
principal_id=principal_id,
129-
principal_type="ServicePrincipal")
133+
principal_type="ServicePrincipal"
134+
)
130135

131136
# Create role assignment
132137
role_assignment = auth_client.role_assignments.create(
133138
scope=scope,
134139
role_assignment_name=uuid.uuid4(),
135-
parameters=parameters)
140+
parameters=parameters
141+
)
136142

137143
return role_assignment
138144

src/evaluation/evaluate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def copilot_qna(*, chat_input, **kwargs):
3434
def run_evaluation(eval_name, dataset_path):
3535

3636
model_config = AzureOpenAIModelConfiguration(
37-
azure_deployment=os.getenv("AZURE_OPENAI_EVALUATION_DEPLOYMENT"),
38-
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
39-
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
37+
azure_deployment=os.environ["AZURE_OPENAI_EVALUATION_DEPLOYMENT"],
38+
api_version=os.environ["AZURE_OPENAI_API_VERSION"],
39+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"]
4040
)
4141

4242
# Initializing Evaluators

src/evaluation/evaluate_completeness.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ def copilot_qna(*, chat_input, **kwargs):
3838
def run_evaluation(name, dataset_path, prompty_filename: str):
3939

4040
model_config = AzureOpenAIModelConfiguration(
41-
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
42-
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
43-
azure_deployment=os.getenv("AZURE_OPENAI_EVALUATION_DEPLOYMENT"),
44-
)
41+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
42+
api_version=os.environ["AZURE_OPENAI_API_VERSION"],
43+
azure_deployment=os.environ["AZURE_OPENAI_EVALUATION_DEPLOYMENT"]
44+
)
4545

4646
# Initializing Evaluators
4747
# relevance_eval = RelevanceEvaluator(model_config)

src/helper_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def get_client() -> MLClient:
1010
# check if env variables are set and initialize client from those
11-
client = MLClient(DefaultAzureCredential(), os.getenv("AZURE_SUBSCRIPTION_ID"), os.getenv("AZURE_RESOURCE_GROUP"), os.getenv("AZUREAI_PROJECT_NAME"))
11+
client = MLClient(DefaultAzureCredential(), os.environ["AZURE_SUBSCRIPTION_ID"], os.environ["AZURE_RESOURCE_GROUP"], os.environ["AZUREAI_PROJECT_NAME"])
1212
if client:
1313
return client
1414

src/indexing/build_index.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def build_aisearch_index(index_name, path_to_data):
2020
name=index_name, # name of your index
2121
vector_store="azure_ai_search", # the type of vector store - in this case it is Azure AI Search. Users can also use "azure_cognitive search"
2222
embeddings_model_config=EmbeddingsModelConfig(
23-
model_name=os.getenv('AZURE_OPENAI_EMBEDDING_DEPLOYMENT'),
24-
deployment_name=os.getenv('AZURE_OPENAI_EMBEDDING_DEPLOYMENT'),
23+
model_name=os.environ['AZURE_OPENAI_EMBEDDING_DEPLOYMENT'],
24+
deployment_name=os.environ['AZURE_OPENAI_EMBEDDING_DEPLOYMENT'],
2525
connection_config=ConnectionConfig(
2626
subscription_id=client.subscription_id,
2727
resource_group_name=client.resource_group_name,
2828
workspace_name=client.workspace_name,
29-
connection_name=os.getenv('AZURE_OPENAI_CONNECTION_NAME')
29+
connection_name=os.environ['AZURE_OPENAI_CONNECTION_NAME']
3030
)
3131
),
3232
input_source=LocalSource(input_data=path_to_data), # the location of your files/folders

src/provisioning/check_quota.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def main():
124124
"--subscription-id",
125125
help="Azure subscription id",
126126
type=str,
127-
default=os.getenv("AZURE_SUBSCRIPTION_ID"),
127+
default=os.environ["AZURE_SUBSCRIPTION_ID"]
128128
)
129129
args = parser.parse_args()
130130

0 commit comments

Comments
 (0)