GPT-3.5 in spring boot

qkrrnjswo·2023년 5월 6일
0

공부 정리

목록 보기
1/24

1. URL에 따라 사용할 수 있는 모델 종류

  • /v1/chat/completions
    gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301

  • /v1/completions
    text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001

  • /v1/edits
    text-davinci-edit-001, code-davinci-edit-001

  • /v1/audio/transcriptions
    whisper-1

  • /v1/audio/translations
    whisper-1

  • /v1/fine-tunes
    davinci, curie, babbage, ada

  • /v1/embeddings
    text-embedding-ada-002, text-search-ada-doc-001

  • /v1/moderations
    text-moderation-stable, text-moderation-latest

모델 종류 정보: https://platform.openai.com/docs/models/gpt-4


2. 사용을 고려한 모델들

GPT-3.5

  • gpt-3.5-turbo (코스트가 다빈치의 1/10)
  • text-davinci-003

3. Chat 모델

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

3-1. 사용 모델

gpt-3.5-turbo

3-2. 통신 형태

URL: https://api.openai.com/v1/chat/completions
헤더: "Content-Type: application/json"
헤더: "Authorization: Bearer $OPENAI_API_KEY"


//Request(body)
{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}

//Response
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

4. Completions 모델

:https://platform.openai.com/docs/api-reference/completions

4-1. 사용 모델

text-davinci-edit-001

4-2. 통신 형태

URL: https://api.openai.com/v1/edits
헤더: "Content-Type: application/json"
헤더: "Authorization: Bearer $OPENAI_API_KEY"


//Request(body)
{
  "model": "text-davinci-edit-001",
  "input": "What day of the wek is it?",
  "instruction": "Fix the spelling mistakes",
}

//Response
{
  "object": "edit",
  "created": 1589478378,
  "choices": [
    {
      "text": "What day of the week is it?",
      "index": 0,
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 32,
    "total_tokens": 57
  }
}

5. API와 통신

RestTemplate를 이용하여 통신
https://velog.io/@seongwon97/Spring-Boot-Rest-Template

public HttpEntity<GptRequestDto> buildHttpEntity(GptRequestDto requestDto) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(GptConfig.MEDIA_TYPE));
        headers.add(GptConfig.AUTHORIZATION, GptConfig.BEARER + gptConfig.getApiKey());
        return new HttpEntity<>(requestDto, headers);
}


    
public GptResponseDto getResponse(HttpEntity<GptRequestDto> chatGptRequestDtoHttpEntity) {
        ResponseEntity<GptResponseDto> responseEntity = restTemplate.postForEntity(
                GptConfig.CHAT_URL,
                chatGptRequestDtoHttpEntity,
                GptResponseDto.class);

        return responseEntity.getBody();
}



public ResponseDto<AnswerResponseDto> askQuestion(QuestionRequestDto requestDto) {
        //모델 종류 설정
        GptConfig.setMODEL(GptConfig.CHAT_MODEL);

        List<Messages> messages = new ArrayList<>();
        messages.add(new Messages(requestDto.getQuestion()+" 어울리는 노래 추천 좀 해줘", "user"));
        GptResponseDto gptResponseDto = this.getResponse(this.buildHttpEntity(new GptRequestDto(GptConfig.MODEL, messages)));
        List<Choice> choices = gptResponseDto.getChoices();
        StringBuilder answer = new StringBuilder();

        for (Choice ch : choices) { answer.append(ch.getMessage().getContent());}

        return ResponseDto.setSuccess("success", new 
}

텍스트가 만들어지는 과정 보여주기

https://tecoble.techcourse.co.kr/post/2022-10-11-server-sent-events/
https://jsonobject.tistory.com/558

0개의 댓글