Skip to content

Commit a5ce75f

Browse files
committed
Add http demos
1 parent 2b47a47 commit a5ce75f

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

http/.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
AZURE_OPENAI_SERVICE=evaldjgxrh3u7sxlu-openai
2+
AUTH_TOKEN=

http/auth.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pathlib import Path
2+
3+
import azure.identity
4+
from dotenv import load_dotenv
5+
6+
# Load environment variables from .env file
7+
load_dotenv(override=True)
8+
9+
# Get a token from Azure
10+
token_provider = azure.identity.get_bearer_token_provider(
11+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
12+
)
13+
token = token_provider()
14+
15+
# Path to the .env file
16+
env_path = Path(__file__).parent / ".env"
17+
18+
# Read the existing lines
19+
with open(env_path) as f:
20+
lines = f.readlines()
21+
22+
# Write back non-AUTH_TOKEN lines and append the new token
23+
with open(env_path, "w") as f:
24+
for line in lines:
25+
if not line.startswith("TOKEN"):
26+
f.write(line)
27+
f.write(f"TOKEN={token}\n")

http/azure_openai.http

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
POST https://{{$dotenv SERVICE}}.openai.azure.com/openai/deployments/{{$dotenv DEPLOYMENT}}/chat/completions?api-version=2024-02-15-preview
2+
Authorization: Bearer {{$dotenv TOKEN}}
3+
Content-Type: application/json
4+
5+
{
6+
"messages": [{"role":"system","content":"You are an AI assistant that loves emojis."},
7+
{"role":"user","content":"What is the capital of France?"}],
8+
"max_tokens": 800,
9+
"temperature": 0.7,
10+
"frequency_penalty": 0,
11+
"presence_penalty": 0,
12+
"top_p": 0.95,
13+
"stop": null
14+
}

retrieval_augmented_generation.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
API_HOST = os.getenv("API_HOST")
1111

1212
if API_HOST == "azure":
13-
1413
token_provider = azure.identity.get_bearer_token_provider(
1514
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
1615
)
@@ -22,29 +21,22 @@
2221
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2322

2423
elif API_HOST == "ollama":
25-
2624
client = openai.OpenAI(
2725
base_url=os.getenv("OLLAMA_ENDPOINT"),
2826
api_key="nokeyneeded",
2927
)
3028
MODEL_NAME = os.getenv("OLLAMA_MODEL")
3129

3230
elif API_HOST == "github":
33-
3431
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
3532
MODEL_NAME = os.getenv("GITHUB_MODEL")
3633

3734
else:
38-
3935
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
4036
MODEL_NAME = os.getenv("OPENAI_MODEL")
4137

42-
SYSTEM_MESSAGE = """
43-
You are a helpful assistant that answers questions about cars based off a hybrid car data set.
44-
You must use the data set to answer the questions, you should not provide any info that is not in the provided sources.
45-
"""
4638

47-
USER_MESSAGE = "how fast is a prius?"
39+
USER_MESSAGE = "how fast is the prius v?"
4840

4941
# Open the CSV and store in a list
5042
with open("hybrid.csv") as file:
@@ -69,6 +61,11 @@
6961
print(matches_table)
7062

7163
# Now we can use the matches to generate a response
64+
SYSTEM_MESSAGE = """
65+
You are a helpful assistant that answers questions about cars based off a hybrid car data set.
66+
You must use the data set to answer the questions, you should not provide any info that is not in the provided sources.
67+
"""
68+
7269
response = client.chat.completions.create(
7370
model=MODEL_NAME,
7471
temperature=0.7,

0 commit comments

Comments
 (0)