api 발급

환경 세팅 

conda create -n gpt python=3.9
conda activate gpt
pip install openai

 

예제 코드

import openai
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model", default="turbo", type=str)
args = parser.parse_args()

openai.api_key = "" # 긱지 개인 키 입력


def davinci_model_run():
  print("text-davinci-003")
  prompt = input("질문의 내용을 입력하세요 : ")
  response = openai.Completion.create(
    model="text-davinci-003",
    prompt=prompt,
    temperature=0.9,
    max_tokens=2000,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.6,
  )
  generated_text = response.choices[0].text
  print("답변 :", generated_text)


def turbo_model_run():
  # 모델 - GPT 3.5 Turbo 선택
  model = "gpt-3.5-turbo"
  print("gpt-3.5-turbo api")

  # 질문 작성하기
  query = input("질문의 내용을 입력하세요 : ")


  # 메시지 설정하기
  messages = [
          {"role": "system", "content": "You are a helpful assistant."}, # chatGPT에게 원하는
          {"role": "user", "content": query}
  ]
  # ChatGPT API 호출하기
  response = openai.ChatCompletion.create(
    model=model,
    messages=messages,
    temperature=1,
    top_p=1,
    n=1,
    stream=False,
    stop=None,
    presence_penalty=0,
    frequency_penalty=0,
  )
  answer = response['choices'][0]['message']['content']
  print("답변 : " + answer)


def main():
  if args.model == "turbo":
    turbo_model_run()
  else:
    davinci_model_run()

if __name__ == '__main__':
     main()

 

 

'ai > chatGPT' 카테고리의 다른 글

ChatGPT-api 정리  (0) 2023.03.22

model : gpt-3.5-turbo

https://openai.com/blog/introducing-chatgpt-and-whisper-apis

 

Introducing ChatGPT and Whisper APIs

Developers can now integrate ChatGPT and Whisper models into their apps and products through our API.

openai.com

 

파라미터 

messages = [
        {"role": "system, user, assistant 중 선택", "content": "role에 알맞게 메세지 작성"},
        {"role": "system, user, assistant 중 선택", "content": "role에 알맞게 메세지 작성"},
]
  • temperature
    • Optional
    • type : float(0.0 ~ 2.0) 
    • default : 1
    • 높을 수록 random, 낮을수록 deterministic 답변을 줌(테스트 해봐야 알 것 같음)
    • top_p과 같이 변경하는 것은 권장하지 않음
  • top_p 
    • Optional
    • type : float(0.0 ~ 1.0)
    • default : 1
    • model이 생성한 token 결과를 sampling 함(테스트 해봐야 알 것 같음)
    • temperature과 같이 변경하는 것은 권장하지 않음
  • n
    • Optional
    • type : float
    • default : 1
    • 유저 메세지에 대해 생성할 대화 개수 
  • stream
    • Optional
    • type : boolean
    • default : false
    • 보류
  • stop
    • Optional
    • type : string or array(최대 4개)
    • default : null
    • token을 생성을 종료하는 키워드 지정. 
  • max_tokens
    • Optional
    • type : integer
    • default : inf
    • 최대로 생성할 수 있는 token 길이로 보통 4096개이다.
  • presence_penalty
    • Optional
    • type : float(-2.0 ~ 2.0)
    • default : 0
    • token을 만들 때 sampling 한 결과를 기억해 페널티를 주는 방식. 양수는 새로운 주제를, 음수로 갈수록 기존의 주제를 반환할 확률이 올라감.
  • frequency_penalty
    • Optional
    • type : float(-2.0 ~ 2.0)
    • default : 0
    • token을 만들 때 sampling 한 결과를 기억해 페널티를 주는 방식. 한번의 답변에 대해 양수는 동일한 말을, 음수로 갈수록 새로운 말을 반환할 확률이 올라감. 
  • logit_bias
    • Optional
    • type : map
    • default : null
    • 보류
  • user
    • Optional
    • type : string
    • 최종 사용자를 나타내는 고유 식별자 

 

참조 

https://platform.openai.com/docs/api-reference/chat/create

 

OpenAI API

An API for accessing new AI models developed by OpenAI

platform.openai.com

https://wooiljeong.github.io/python/chatgpt-api/

 

ChatGPT API Python 사용법 (feat.DALL-E, Karlo)

ChatGPT API Python 사용법 (feat.DALL-E, Karlo)

wooiljeong.github.io

 

'ai > chatGPT' 카테고리의 다른 글

chatGPT-api 예제 코드  (0) 2023.03.26

+ Recent posts