-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathazure_openai.py
31 lines (24 loc) · 901 Bytes
/
azure_openai.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
from dotenv import load_dotenv
import os
import openai
load_dotenv()
openai.api_type = "azure"
openai.api_base = "<YOUR AZURE OPENAI ENDPOINT>"
openai.api_version = "2023-03-15-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")
def get_completion_from_messages(system_message, user_message, model="gpt-4", temperature=0, max_tokens=500) -> str:
messages = [
{'role': 'system', 'content': system_message},
{'role': 'user', 'content': f"{user_message}"}
]
response = openai.ChatCompletion.create(
engine=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
return response.choices[0].message["content"]
if __name__ == "__main__":
system_message = "You are a helpful assistant"
user_message = "Hello, how are you?"
print(get_completion_from_messages(system_message, user_message))