Skip to content

Commit aefba44

Browse files
committed
Merge branch 'azure_openai' of https://github.com/Eloquent-Algorithmics/Automotive-AI into azure_openai
2 parents e75ed7e + 7533486 commit aefba44

File tree

12 files changed

+565
-213
lines changed

12 files changed

+565
-213
lines changed

.env.template

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
################################################################################
2+
### User E-mail for Authentication
3+
################################################################################
4+
5+
EMAIL_ADDRESS=
6+
7+
################################################################################
8+
### ELM327 OBD2 Connection
9+
################################################################################
10+
11+
SERIAL_PORT=COM7
12+
BAUD_RATE=500000
13+
TIMEOUT=1
14+
115
################################################################################
216
### platform.openai.com
317
################################################################################
@@ -8,38 +22,38 @@ OPENAI_PROJECT_ID=
822
OPENAI_MODEL=gpt-4o-mini
923

1024
################################################################################
11-
### Azure OpenAI
25+
### Azure Auth and OpenAI
1226
################################################################################
1327

28+
AUTH_TENANT_ID=
29+
AUTH_CLIENT_ID=
30+
AUTH_CLIENT_SECRET=
31+
1432
AZURE_OPENAI_API_KEY=
1533
AZURE_OPENAI_ENDPOINT=
1634
AZURE_OPENAI_CHATGPT_DEPLOYMENT_NAME=gpt-4o-mini
17-
AZURE_OPENAI_API_VERSION=2024-10-21
35+
OPENAI_API_VERSION=2025-03-01-preview
1836

1937
################################################################################
20-
### ELM327 OBD2 Connection
38+
### Azure Speech
2139
################################################################################
2240

23-
SERIAL_PORT=COM7
24-
BAUD_RATE=500000
25-
TIMEOUT=1
41+
AZURE_SPEECH_KEY=
42+
AZURE_SPEECH_REGION=eastus2
43+
AZURE_SPEECH_VOICE=en-GB-OllieMultilingualNeural
2644

2745
################################################################################
28-
### Azure Speech
46+
### Bing Search
2947
################################################################################
3048

31-
AZURE_SPEECH_KEY=
32-
AZURE_SPEECH_REGION=
33-
AZURE_SPEECH_VOICE=en-US-AriaNeural
49+
BING_API_KEY=
3450

3551
################################################################################
36-
### Microsoft Entra ID
52+
### Google Custom Search
3753
################################################################################
3854

39-
EMAIL_ADDRESS=
40-
AUTH_CLIENT_ID=
41-
AUTH_CLIENT_SECRET=
42-
AUTH_TENANT_ID=
55+
GOOGLE_API_KEY=
56+
GOOGLE_CSE_ID=
4357

4458
################################################################################
4559
### Twilio
File renamed without changes.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,5 @@ elm.log
163163
.vscode
164164
.idea/
165165
*.code-workspace
166-
.vs/
166+
.vs
167+
*.env

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 John
3+
Copyright (c) 2025
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
3+
from openai import AzureOpenAI, OpenAI
4+
5+
azure_credential = None
6+
openai_client = None
7+
openai_model_arg = None
8+
9+
10+
def get_azure_credential():
11+
"""
12+
Get the Azure credential for authentication.
13+
This function uses the DefaultAzureCredential from the azure.identity package.
14+
It will first check if the credential is already set. If not, creates a new one.
15+
"""
16+
global azure_credential
17+
18+
if azure_credential is None:
19+
azure_credential = DefaultAzureCredential(
20+
exclude_shared_token_cache_credential=True
21+
)
22+
return azure_credential
23+
24+
25+
def configure_openai_client():
26+
"""
27+
Configure the OpenAI client based on environment variables set for Azure or OpenAI.
28+
29+
This function checks for the presence of the AZURE_OPENAI_ENDPOINT and
30+
OPENAI_API_KEY environment variables.
31+
32+
If AZURE_OPENAI_ENDPOINT is set, it configures the client for Azure OpenAI.
33+
Otherwise, it configures for standard OpenAI.
34+
35+
It also sets the API version and deployment name for Azure OpenAI if applicable.
36+
If neither environment variable is set, it raises a ValueError.
37+
"""
38+
global openai_client, openai_model_arg
39+
40+
if openai_client:
41+
return openai_client, openai_model_arg
42+
43+
if os.getenv("AZURE_OPENAI_ENDPOINT"):
44+
# Configure for Azure OpenAI
45+
api_key = os.getenv("AZURE_OPENAI_API_KEY")
46+
client_args = {}
47+
if api_key:
48+
client_args["api_key"] = api_key
49+
else:
50+
client_args["azure_ad_token_provider"] = get_bearer_token_provider(
51+
get_azure_credential(), "https://cognitiveservices.azure.com/.default"
52+
)
53+
54+
openai_client = AzureOpenAI(
55+
api_version=os.getenv("AZURE_OPENAI_API_VERSION", "2025-03-01-preview"),
56+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
57+
**client_args
58+
)
59+
60+
openai_model_arg = os.getenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT_NAME")
61+
62+
elif os.getenv("OPENAI_API_KEY"):
63+
# Configure for standard OpenAI
64+
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
65+
openai_model_arg = os.getenv("OPENAI_MODEL") or "gpt-4o-mini"
66+
67+
else:
68+
raise ValueError(
69+
"No OpenAI configuration provided. Check your environment variables."
70+
)
71+
72+
return openai_client, openai_model_arg

automotive_ai/_utils/_serial_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
sending commands to a serial device, and running diagnostic reports.
44
"""
55
import requests
6-
from automotive_ai._api._nhtsa._vin_decoder import (
6+
from _api._nhtsa._vin_decoder import (
77
parse_vin_response,
88
get_vehicle_data_from_nhtsa,
99
)
10-
from automotive_ai._api._msal._graph_api import send_email_with_attachments
10+
from _api._msal._graph_api import send_email_with_attachments
1111
from . import EMAIL_ADDRESS
1212

1313

automotive_ai/app.py

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,21 @@
22
This is the main script of the application.
33
"""
44

5-
import os
65
import argparse
76
import sys
8-
from openai import OpenAI, AzureOpenAI
9-
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
107

11-
from automotive_ai._api._msal import _ms_authserver
12-
from automotive_ai._api._msal import _graph_api
13-
from _audio._audio_output import tts_output, ssml_output
8+
from _api._msal import _graph_api, _ms_authserver
9+
from _audio._audio_output import ssml_output, tts_output
1410
from dotenv import load_dotenv
11+
from main import main_conversation
1512
from rich.console import Console
1613

1714
console = Console()
1815

1916
load_dotenv()
2017

21-
openai_client = None
22-
openai_model_arg = None
2318

24-
azure_credential = None
25-
26-
27-
def get_azure_credential():
28-
"""
29-
Retrieves the Azure credential for authentication.
30-
"""
31-
global azure_credential
32-
if azure_credential is None:
33-
azure_credential = DefaultAzureCredential(
34-
exclude_shared_token_cache_credential=True
35-
)
36-
return azure_credential
37-
38-
39-
def configure_openai():
40-
"""
41-
Configures the OpenAI client based on environment variables.
42-
43-
This function sets up the OpenAI client using different configurations depending on
44-
the environment variables provided. It supports Azure OpenAI endpoints, and OpenAI API keys.
45-
46-
Raises:
47-
ValueError: If required environment variables for Azure OpenAI are missing or
48-
if no OpenAI configuration is provided.
49-
50-
Environment Variables:
51-
AZURE_OPENAI_ENDPOINT: The Azure endpoint for OpenAI.
52-
AZURE_OPENAI_API_KEY: The API key for Azure OpenAI.
53-
AZURE_OPENAI_CHATGPT_DEPLOYMENT_NAME: The deployment name for Azure OpenAI ChatGPT.
54-
AZURE_OPENAI_API_VERSION: The API version for Azure OpenAI.
55-
OPENAICOM_API_KEY: The API key for OpenAI.
56-
OPENAICOM_MODEL: The model name for OpenAI (default is "gpt-4o-mini").
57-
58-
"""
59-
global openai_client, openai_model_arg
60-
61-
client_args = {}
62-
if os.getenv("AZURE_OPENAI_ENDPOINT"):
63-
if os.getenv("AZURE_OPENAI_API_KEY"):
64-
client_args["api_key"] = os.getenv("AZURE_OPENAI_API_KEY")
65-
else:
66-
client_args["azure_ad_token_provider"] = get_bearer_token_provider(
67-
get_azure_credential(), "https://cognitiveservices.azure.com/.default"
68-
)
69-
if not os.getenv("AZURE_OPENAI_ENDPOINT"):
70-
raise ValueError("AZURE_OPENAI_ENDPOINT is required for Azure OpenAI")
71-
if not os.getenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT_NAME"):
72-
raise ValueError(
73-
"AZURE_OPENAI_CHATGPT_DEPLOYMENT_NAME is required for Azure OpenAI"
74-
)
75-
openai_client = AzureOpenAI(
76-
api_version=os.getenv("AZURE_OPENAI_API_VERSION") or "2024-10-21",
77-
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
78-
**client_args,
79-
)
80-
openai_model_arg = os.getenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT_NAME")
81-
82-
elif os.getenv("OPENAI_API_KEY"):
83-
client_args["api_key"] = os.getenv("OPENAI_API_KEY")
84-
openai_client = OpenAI(
85-
**client_args,
86-
)
87-
openai_model_arg = os.getenv("OPENAI_MODEL") or "gpt-4o-mini"
88-
else:
89-
raise ValueError(
90-
"No OpenAI configuration provided. Check your environment variables."
91-
)
92-
93-
94-
def main():
19+
def main_intro():
9520
"""
9621
Main function to encapsulate the script logic.
9722
"""
@@ -112,8 +37,6 @@ def main():
11237

11338
ssml_output(ssml_text)
11439

115-
configure_openai()
116-
11740
console.print(
11841
"Allow me to introduce myself... I am Winston, your in car Virtual Assistant... Importing all preferences and settings.",
11942
style="bold green",
@@ -142,18 +65,12 @@ def main():
14265
"Systems now fully operational. How may I assist you today?", style="bold green"
14366
)
14467

145-
from main import main_conversation
146-
14768
main_conversation(args, email_module.user_object_id, use_elm327)
14869

149-
if openai_client is None:
150-
console.log("OpenAI client is not configured in app.py")
151-
return
152-
15370

15471
if __name__ == "__main__":
15572
try:
156-
main()
73+
main_intro()
15774
except KeyboardInterrupt:
15875
print("\nGoodbye for now ...\n")
15976
sys.exit(0)

0 commit comments

Comments
 (0)