.env
파일 등 받아왔다.python-dotenv
패키지를 설치하과, 앱 개발 환경을 준비..env
파일에 엔드포인트, 키값, 모델 이름 등을 설정하는 방법을 제공..env
파일을 다뤄봤어서 반가웠고, 아 이런 구조로 사용되었던 거구나, 깨달아서 즐거웠다. ㅎㅎ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-08-06-preview"
)
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")
# Initial message array
message_array = [{"role": "system", "content": system_message}]
message_array.append({"role": "user", "counter": input_text})
# Add code to send request...
response = client.chat.completions.create(
model=azure_oai_deployment,
temperature=0.7,
max_tokens=1200,
messages=message_array
)
# Receive generated text
generated_text = response.choices[0].message.content
# Add generated text to message array
message_array.append({"role": "assistant", "counter": generated_text})
# Print generated text
print("Answer: " + generated_text+"\n")
except Exception as ex:
print(ex)
if __name__ == '__main__':
main()
이제 수업 가자.
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"
)
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")
# Initial message array
messages_array = [
{"role": "system", "content": system_message},
{"role": "user", "content": input_text}
]
# Add code to send request...
response = client.chat.completions.create(
model = azure_oai_deployment,
max_tokens = 1200,
temperature = 0.7,
messages = messages_array
)
# receive generated text
generated_text = response.choices[0].message.content
# add generated text to message array
messages_array.append({"role": "assistant", "content": generated_text})
# print generated text
print("Answer:" + generated_text+ "\n")
except Exception as ex:
print(ex)
if __name__ == '__main__':
main()