openAPI 를 사용하여 chatGPT 이용하기 with nodejs.
1. openai API를 사용하기 위한 회원가입
OpenAI API
OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity.
openai.com
위에 페이지에서 openAI 회원가입
2. API Key 생성하기
로그인 후 View API Keys 클릭
Create new secret key를 통해 new key 생성
3. openai package 설치
openai를 사용하기 위한 openai package 설치하기
https://www.npmjs.com/package/openai
openai
Node.js library for the OpenAI API. Latest version: 3.1.0, last published: 3 months ago. Start using openai in your project by running `npm i openai`. There are 136 other projects in the npm registry using openai.
www.npmjs.com
$ $ npm install openai
4. openai API 호출 함수 작성
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: 'sk-*****',
});
const openai = new OpenAIApi(configuration);
export class ChatGPTService {
async chatGPT(reqText: string): Promise<string> {
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: `${reqText}`,
temperature: 0.9,
max_tokens: 4000,
});
const reply = response.data.choices[0].text;
return reply;
}
}
5. Slack web api 를 통한 message post
slack api package 설치
https://www.npmjs.com/package/@slack/web-api
@slack/web-api
Official library for using the Slack Platform's Web API. Latest version: 6.8.1, last published: 12 days ago. Start using @slack/web-api in your project by running `npm i @slack/web-api`. There are 424 other projects in the npm registry using @slack/web-api
www.npmjs.com
$ $ npm install @slack/web-api
slack으로 message post
import { WebClient } from '@slack/web-api';
export class EventService {
private webBotClient: WebClient;
constructor() {
this.webBotClient = new WebClient(
configService.get<string>('slack.tokens.botToken'),
);
}
private async chatGPT(reqText: string, channelId, parentTs): Promise<void> {
const reply = await this.chatGPTService.chatGPT(reqText);
await this.webBotClient.chat.postMessage({
text: '```' + reply + '```',
channel: channelId,
thread_ts: parentTs,
});
}
}
끝.
'Programming > Etc' 카테고리의 다른 글
서버 <-> 클라이언트 인증을 위한 방법 3가지 (0) | 2023.04.24 |
---|---|
백엔드 개발자라면 알아야 할 개념들 (0) | 2023.04.24 |
[Composer] composer install (0) | 2022.05.17 |
빗썸에서 카이카스 지갑으로 클레이튼 전송 하는 방법 (64) | 2022.04.22 |
[Go] Go 설치 for Mac (0) | 2021.12.09 |
댓글