-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-openai-model.py
68 lines (52 loc) · 2.31 KB
/
test-openai-model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from dotenv import load_dotenv
# Add Azure OpenAI package
from openai import AzureOpenAI
def main():
try:
# Get configuration settings
load_dotenv()
azure_oai_endpoint = os.getenv("AZURE_OAI_ENDPOINT")
azure_oai_key = os.getenv("AZURE_OAI_KEY")
azure_oai_deployment = os.getenv("AZURE_OAI_DEPLOYMENT")
# Initialize the Azure OpenAI client
client = AzureOpenAI(
azure_endpoint = azure_oai_endpoint,
api_key=azure_oai_key,
api_version="2024-02-15-preview"
)
# Create a system message
system_message = """I am a hiking enthusiast named Forest who helps people discover hikes in their area.
If no area is specified, I will default to near Rainier National Park.
I will then provide three suggestions for nearby hikes that vary in length.
I will also share an interesting fact about the local nature on the hikes when making a recommendation.
"""
# Initialize messages array
messages_array = [{"role": "system", "content": system_message}]
while True:
# Get input text
input_text = input("Enter the prompt (or type 'quit' to exit): ")
if input_text.lower() == "quit":
break
if len(input_text) == 0:
print("Please enter a prompt.")
continue
print("\nSending request for summary to Azure OpenAI endpoint...\n\n")
# Add code to send request...
# Send request to Azure OpenAI model
messages_array.append({"role": "user", "content": input_text})
response = client.chat.completions.create(
model=azure_oai_deployment,
temperature=0.7,
max_tokens=1200,
messages=messages_array
)
generated_text = response.choices[0].message.content
# Add generated text to messages array
messages_array.append({"role": "assistant", "content": generated_text})
# Print generated text
print("Summary: " + generated_text + "\n")
except Exception as ex:
print(ex)
if __name__ == '__main__':
main()