본문 바로가기
웹개발

chatgpt api를 이용해보다

by 철없는민물장어 2023. 7. 20.
728x90

https://platform.openai.com/docs/libraries/community-libraries

 

OpenAI Platform

Explore developer resources, tutorials, API docs, and dynamic examples to get the most out of OpenAI's platform.

platform.openai.com

여기 openAI 홈페이지에서 api사용법이랑 다 나와있다.

라이브러리도 많이 나와있는데,

https://github.com/TheoKanning/openai-java

 

GitHub - TheoKanning/openai-java: OpenAI Api Client in Java

OpenAI Api Client in Java. Contribute to TheoKanning/openai-java development by creating an account on GitHub.

github.com

이것을 사용하면 됨.

 

(근데 구글링하면서 쉬워보이는걸로 따라하다보니까.... 내가쓴 라이브러리는 저게 아니었다. 그래도 작동은 잘 돼서 일단 올려본다. 다음에 쓸땐 홈페이지에서 소개한 저 라이브러리를 써봐야겠다.)

 

https://github.com/flashvayne/chatgpt-spring-boot-starter

 

GitHub - flashvayne/chatgpt-spring-boot-starter: a chatgpt starter based on Openai Official Apis.

a chatgpt starter based on Openai Official Apis. Contribute to flashvayne/chatgpt-spring-boot-starter development by creating an account on GitHub.

github.com

내가 사용한 라이브러리는 이것

 

 

    implementation 'io.github.flashvayne:chatgpt-spring-boot-starter:1.0.4'

build.gradle에 의존성 추가.

chatgpt.api-key=sk-뭐시기뭐시기

application.properties에 api 키값 추가하면 끝..

 

@Controller
@RequiredArgsConstructor
public class WeatherController {

    private final WeatherService weatherService;

    private final ChatgptService chatgptService;

    @GetMapping("/weather")
    public String currentWeather(Model model) throws IOException {
        Weather weather = weatherService.getWeather();
        model.addAttribute("weather",weather);
        String res = chatgptService.sendMessage("현재 온도는" + weather.getTemperature() + "'C이고, 날씨는 " + weather.getCondition() + "이야. " +
                "어떤 옷차림이 좋을지 알려주고, 어떻게 생활하면 좋을지 추천해줘. 모든 답변은 100자 이내로 해");

        model.addAttribute("msg",res);
        return "weather";
    }
}

동작이 되는지 보기 위해 ChatgptService를 autowire 해주고.. 간단하게 사용해봤다.

뭔가 나사가 빠진것같지만 잘 작동된다.

 

https://platform.openai.com/docs/guides/gpt/chat-completions-api

 

OpenAI Platform

Explore developer resources, tutorials, API docs, and dynamic examples to get the most out of OpenAI's platform.

platform.openai.com

 

이 문서를 잘 살펴보면 여러가지 role로 메세지를 전달할수도 있고..

더 많은 기능이 있으니 api기능과 라이브러리 사용법을 숙지해서 사용하면 좋을듯하다.

728x90

댓글