ChatGPT API๋ฅผ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์จ๋ณด๊ธฐ

2023. 4. 10. 23:10ยทDevelop/API

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

 

OpenAI API

An API for accessing new AI models developed by OpenAI

platform.openai.com

 

API Key ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ

 

 

 

๋ฐœ๊ธ‰ ๋ฐ›์€ ํ‚ค๋Š” ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์—†๋‹ค. 

๋‹ค๋ฅธ๊ณณ์— ๋ณด๊ด€ํ•˜์ž 

APIํ‚ค๋ฅผ ํฌํ•จํ•ด์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ๊นƒํ—™์— ์—…๋กœ๋“œํ•˜๋ฉด ๋‹ค์‹œ ์ƒ์„ฑํ•ด์•ผ๋œ๋‹ค. 

 

 

 

๋ชจ๋ธ ์„ ํƒ

text-davinci-003 ์„ ์„ ํƒํ–ˆ๋‹ค. 

 

 

 

RestTemplate์œผ๋กœ API ์š”์ฒญํ•˜๊ธฐ

@Service
public class ChatGptService {
    
    private String API_KEY = "";
    private static final String ENDPOINT = "https://api.openai.com/v1/completions";
    
    public ResponseEntity<String> generateText(String prompt, float temperature, int maxTokens) {

        //HttpHeader ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type", "application/json");
        headers.add("Authorization", "Bearer " + API_KEY);

        //HttpBody ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", "text-davinci-003");
        requestBody.put("prompt", prompt);
        requestBody.put("max_tokens", maxTokens);
        requestBody.put("temperature", temperature);

        // HttpHeader์™€ HttpBody๋ฅผ ํ•˜๋‚˜์˜ ์˜ค๋ธŒ์ ํŠธ์— ๋‹ด๊ธฐ
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
        
        RestTemplate rt = new RestTemplate();

        //Http POST ๋ฐฉ์‹ ์š”์ฒญ - response ๋ณ€์ˆ˜ ์‘๋‹ต
        ResponseEntity<String> response = rt.exchange(
                ENDPOINT,   // ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ์ฃผ์†Œ
                HttpMethod.POST,
                requestEntity,
                String.class
        );
        
        return response;
    }
}

๋ฐœ๊ธ‰ ๋ฐ›์€ API ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

ํŒŒ๋ผ๋ฏธํ„ฐ 

  • model : ์–ด๋–ค GPT ๋ชจ๋ธ์„ ์‚ฌ์šฉํ• ์ง€ ์ง€์ •
  • prompt : ์งˆ๋ฌธํ•  ๋ฌธ์žฅ
  • max_tokens : ์‘๋‹ต์˜ ์ปจํ…์ŠคํŠธ ๊ธธ์ด
  • temerature : ์–ผ๋งˆ๋‚˜ ์ฐฝ์˜์ ์ธ ๋‹ต์„ ์ž‘์„ฑํ•˜๋„๋ก ํ• ์ง€ ์ง€์ •ํ•˜๋Š” ๊ฐ’. ํด์ˆ˜๋ก ์ฐฝ์˜์ ์ด๊ณ  ์ž‘์„์ˆ˜๋ก ์ž˜ ์ •์˜๋œ ๋‹ต์„ ์ œ์ถœํ•œ๋‹ค.

 

 

@RequestMapping("/chatGpt")
@ResponseBody
public ResponseEntity<String> chatGpt() throws JsonProcessingException {
    log.info("ok");
    ResponseEntity<String> response = chatGptService.generateText("ChatGPT๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?", 1.0f, 1000);
    return response;
}

 

 

 

 

Response ๋ฐ์ดํ„ฐ

 

JSON ํ˜•์‹์œผ๋กœ ์‘๋‹ต์ด ์˜จ๋‹ค. 

 

 

 

JSON ํŒŒ์ผ์„ Java ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ž  

 

 

https://www.jsonschema2pojo.org/

 

jsonschema2pojo

Reference properties For each property present in the 'properties' definition, we add a property to a given Java class according to the JavaBeans spec. A private field is added to the parent class, along with accompanying accessor methods (getter and sette

www.jsonschema2pojo.org

 

jsonschema2pojo๋Š” JSON ์Šคํ‚ค๋งˆ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋ฐ” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

 

 

 

ObjectMapper objectMapper = new ObjectMapper();
ResponseData responseData = null;
try {
    responseData = objectMapper.readValue(response.getBody(), ResponseData.class);
} catch (JsonProcessingException e) {
    throw new RuntimeException(e);
}

JSON ํŒŒ์ผ์„ jsonschema2pojo ํ†ตํ•ด์„œ ์ƒ์„ฑํ•œ Java ํด๋ž˜์Šค ๊ฐ์ฒด๋กœ deserialization ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

 

ObjectMapper์˜ readValue() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค.

 

 

 

 

๋Š” ์—๋Ÿฌ

 

๋‹ค๋ฅธ๊ฑฐ ๋‹ค ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ ํ•ด๋ดค๋Š”๋ฐ 

์ž๋ฐ” ํด๋ž˜์Šค๋ฅผ ์ž˜๋ชป๋งŒ๋“ค์—ˆ๋Š”์ง€ ์•„๋‹ˆ๋ฉด deserialization ์ž‘์—…์ด ์ž˜๋ชป๋œ ๊ฒƒ ๊ฐ™๋‹ค. 

๋‹ค์Œ์— ํ•ด๊ฒฐํ•˜์ž...

 

 

 

 

4/14(๊ธˆ)

๊ต์ˆ˜๋‹˜๊ป˜ ๋ฌผ์–ด๋ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 jsonschema2pojo ํ†ตํ•ด์„œ ์ƒ์„ฑํ•œ Java ํด๋ž˜์Šค์— ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

 

์›์ธ์€ ๋ฐ”๋กœ Inner Class๊ฐ€ static(์ •์ )์œผ๋กœ ์„ ์–ธ๋˜์ง€ ์•Š๋Š” ํ•œ ๋‹จ๋…(Outer ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ )์œผ๋กœ Inner Class์˜ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์œ„์™€ ๊ฐ™์€ ์˜ˆ์™ธ๋ฅผ ํ”ผํ•˜๋ ค๋ฉด Inner Class๋ฅผ ๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•˜๋˜๊ฐ€, ์•„๋‹ˆ๋ฉด static Inner Class๋กœ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

@Data
public class ResponseData {
    public String id;
    public String object;
    public Integer created;
    public String model;
    public List<Choice> choices;
    public Usage usage;
}

 

Choice ํด๋ž˜์Šค๋ฅผ  ResponseData ์— Inner Class ๋กœ ์ƒ์„ฑํ–ˆ์—ˆ๋‹ค.

 

@Data
public class Choice {
    public String text;
    public Integer index;
    public Object logprobs;
    public String finish_reason;
}

 

๋ณ„๋„์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. 

 

 

 

@RequestMapping("/chatGpt")
@ResponseBody
public  String chatGpt() throws JsonProcessingException {
    log.info("ok");
    ResponseData responseData = chatGptService.generateText("chatGPT๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?", 1.0f, 1000);
    return responseData.getChoices().get(0).getText();
}

 

์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด์ž 

 

 

 

text ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ ! 

 

 

 

 

 

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'Develop > API' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์นด์นด์˜ค ๋กœ๊ทธ์ธ API ๊ตฌํ˜„ ํ•˜๊ธฐ (REST API ๋ฐฉ์‹)  (0) 2023.03.26
'Develop/API' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • ์นด์นด์˜ค ๋กœ๊ทธ์ธ API ๊ตฌํ˜„ ํ•˜๊ธฐ (REST API ๋ฐฉ์‹)
hello_u
hello_u
  • hello_u
    ๐Ÿ˜œ
    hello_u
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๐Ÿ˜œ (345)
      • Hardware (2)
        • BMC (2)
      • Spring (109)
        • Spring ์ž…๋ฌธ (20)
        • Spring ๊ธฐ๋ณธ (27)
        • Spring MVC (18)
        • Spring DB (22)
        • Spring JPA ๊ธฐ๋ณธ (16)
        • Spring JPA ํ™œ์šฉ (6)
      • Develop (27)
        • DB (8)
        • JAVA (4)
        • Web (2)
        • Python (7)
        • OSS (2)
        • Git (2)
        • API (2)
      • Algorithm (155)
        • CodeUp ๊ธฐ์ดˆ (44)
        • ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (64)
        • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (4)
        • SWEA (30)
        • Softeer (10)
        • BOJ (2)
      • CS (9)
        • ์ปดํ“จํ„ฐ์ผ๋ฐ˜ (3)
        • ์šด์˜์ฒด์ œ (3)
        • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค (0)
        • ์ •๋ณดํ†ต์‹  (1)
        • ์ž๋ฃŒ๊ตฌ์กฐ (1)
        • ์†Œํ”„ํŠธ์›จ์–ด ๊ณตํ•™ (1)
        • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (0)
        • ์ตœ์‹  ๋””์ง€ํ„ธ, ์ผ๋ฐ˜์ƒ์‹ (0)
      • ์ž๊ฒฉ์ฆ (41)
        • ์ •๋ณด๋ณด์•ˆ๊ธฐ์‚ฌ (9)
        • ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ (22)
        • ๋ฆฌ๋ˆ…์Šค๋งˆ์Šคํ„ฐ 1๊ธ‰ (3)
        • SQLD (7)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
hello_u
ChatGPT API๋ฅผ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์จ๋ณด๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”