
Azure에서 지원하는 OpenAI API로 GPT를 사용하는 방법입니다.
OpenAI에서 제공하는 Python 클라이언트 패키지를 설치해서 똑같이 사용하면 됩니다.
단, openai의 API를 사용할 때와 다른 점은
- openai를 처음 초기화 할 때 설정값을 넣어줘야 하는 것이 있음
- completion을 생성할 때 파라미터가 조금 다름
자세한 내용은 애저(Azure)를 문서를 참고하면 됩니다.
아래의 소스 코드를 참고하세요.
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 69 70 |
import os import openai import toml import base64 openai.api_key = " ***** 본인의 API키 ***** " openai.api_base = "https://instance-openai-0.openai.azure.com/" openai.api_type = "azure" openai.api_version = "2023-07-01-preview" deployment_id: str = "deployment-gpt-4-32k" # 본인의 배포 ID로 변경 instructions: list[dict] = [ {"role": "system", "content": "you are an assistant for software engineers"}, ] messages: list[dict] = [ # {"role": "user", "content": "What is ticket price for cruise ship"}, # for function_call {"role": "user", "content": "샌프란시스코의 관광명소를 알려주세요."}, # for normal reply ] response: openai.ChatCompletion = openai.ChatCompletion.create( # model="gpt-4", # this is for the OpenAI API, not for the Azure API deployment_id=deployment_id, # this is for the Azure API, not for the OpenAI API # engine=deployment_id, # same as above messages=instructions + messages, functions=[ { "name": "get_ticket_price", "description": "Get ticket price of cruise ship", "parameters": { "type": "object", "properties": { "start_location": { "type": "string", "description": "Start location of the cruise ship", }, "end_location": { "type": "string", "description": "End location of the cruise ship", }, }, }, "required": ["start_location", "end_location"], }, ], function_call="auto", temperature=0.90, max_tokens=800, top_p=0.99, frequency_penalty=0, presence_penalty=0, stop=None) if len(response.choices) > 0: if response.choices[0].finish_reason == "function_call": print("=====================================") print("function_call is called") print(response.choices[0].message) print("=====================================") else: print("=====================================") print("reply is returned") print("role: ", response.choices[0].message.role) print("message: ", response.choices[0].message.content) print("=====================================") else: print("No response.") |
Github에도 소스를 올려놨습니다.
https://github.com/euriion/llm-materials/blob/main/azure-openai-api/azure-openai-api-test.ipynb