NestJS) ChatGPT API 사용후기
다빈치라는 GPT3 모델을 사용하여 챗봇인듯 챗봇 아닌걸 만들었다.
npm i openai
import { Body, Controller, Post } from '@nestjs/common';
import { Configuration, OpenAIApi } from 'openai';
@Post('openai')
async chatGPT(@Body('question') question: string) {
try {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `${question}`,
temperature: 0.9,
max_tokens: 100,
n: 1,
top_p: 1,
stream: false,
logprobs: null,
// stop: "\n",
// frequency_penalty: 0.0,
// presence_penalty: 0.0,
});
return response.data.choices[0].text;
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
throw new Error();
}
}
참고)
https://platform.openai.com/docs/api-reference/completions
OpenAI API
An API for accessing new AI models developed by OpenAI
platform.openai.com
Api reference를 보고 글자를 입력하고 이미지를 만들거나 이미지를 수정하는 코드도 작성해봤다.
답변은 썩 만족스럽진 않았지만 AI가 이미지를 가져와주는게 굉장히 신기했다.
@Post('openai/img')
async createGptImage() {
try {
const instance = axios.create({
baseURL: 'https://api.openai.com/v1/',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`
}
});
const result = await instance.post('images/generations', {
prompt: "A pretty, glamorous and gorgeous animate girl like mai of 'king of fighters'",
n: 2,
size: "1024x1024"
});
return result.data;
} catch (error) {
console.log(error)
throw new Error();
}
}
sexy를 넣으면 액세스를 거부당한다 ㅠㅠ ㅋㅋㅋ...
이 정도 질문이면 정말 잘 했다고 생각했는데,
결과물이 상당히 충격적이다...
model list를 api 호출로 직접 확인할 수 있었는데 이미 공개된 AI list와 해당 id가 존재하여 이런 과정은 불필요했다.
https://platform.openai.com/docs/api-reference/models/list
OpenAI API
An API for accessing new AI models developed by OpenAI
platform.openai.com
중요한건 무료 버전을 사용하다보니 답변을 받을 수 있는 길이가 굉장히 짧았다.
금액을 보니 아마 돈을 쓸어담을 듯 하다. 굉장히 부럽다...
https://openai.com/api/pricing/
Pricing
OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity.
openai.com
디스코드 봇에 gpt3를 연결하고 심심풀이로 활용하면 재밌을 것 같다.