mirror of
https://github.com/syuilo/ai.git
synced 2025-03-25 21:12:56 +00:00
Add ChatGPT
This commit is contained in:
parent
42c4d43f5f
commit
16f7c47419
6 changed files with 90 additions and 0 deletions
|
@ -14,6 +14,7 @@ type Config = {
|
||||||
mecabDic?: string;
|
mecabDic?: string;
|
||||||
memoryDir?: string;
|
memoryDir?: string;
|
||||||
earthQuakeMonitorPort?: number;
|
earthQuakeMonitorPort?: number;
|
||||||
|
openAiApiKey?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const config = require('../config.json');
|
const config = require('../config.json');
|
||||||
|
|
5
src/constants/ENDPOINTS.ts
Normal file
5
src/constants/ENDPOINTS.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
const ENDPOINTS = {
|
||||||
|
OPEN_AI: 'https://api.openai.com/v1',
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export default ENDPOINTS;
|
11
src/constants/GPT3_MODELS.ts
Normal file
11
src/constants/GPT3_MODELS.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/**
|
||||||
|
* @see https://beta.openai.com/docs/models/overview
|
||||||
|
*/
|
||||||
|
const GPT3_MODELS = {
|
||||||
|
davinci: 'text-davinci-003',
|
||||||
|
curie: 'text-curie-001',
|
||||||
|
babbage: 'text-babbage-001',
|
||||||
|
ada: 'text-ada-001',
|
||||||
|
};
|
||||||
|
|
||||||
|
export default GPT3_MODELS;
|
|
@ -39,6 +39,7 @@ import Earthquake from './modules/earthquake';
|
||||||
import DicModule from './modules/dic';
|
import DicModule from './modules/dic';
|
||||||
import MenuModule from './modules/menu';
|
import MenuModule from './modules/menu';
|
||||||
import GetColorModule from './modules/color';
|
import GetColorModule from './modules/color';
|
||||||
|
import ChatGPT from './modules/chatGPT';
|
||||||
|
|
||||||
console.log(' __ ____ _____ ___ ');
|
console.log(' __ ____ _____ ___ ');
|
||||||
console.log(' /__\\ (_ _)( _ )/ __)');
|
console.log(' /__\\ (_ _)( _ )/ __)');
|
||||||
|
@ -72,6 +73,7 @@ promiseRetry((retry) => {
|
||||||
new 藍(account, [
|
new 藍(account, [
|
||||||
new CoreModule(),
|
new CoreModule(),
|
||||||
new ReminderModule(),
|
new ReminderModule(),
|
||||||
|
new ChatGPT(),
|
||||||
new SummonCat(),
|
new SummonCat(),
|
||||||
new EmojiModule(),
|
new EmojiModule(),
|
||||||
new EmojiReactModule(),
|
new EmojiReactModule(),
|
||||||
|
|
29
src/modules/chatGPT/index.ts
Normal file
29
src/modules/chatGPT/index.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import autobind from 'autobind-decorator';
|
||||||
|
import Module from '@/module';
|
||||||
|
import Message from '@/message';
|
||||||
|
import fetchChatGPT from '@/utils/fetchChatGPT';
|
||||||
|
|
||||||
|
class ChatGPT extends Module {
|
||||||
|
public readonly name = 'ChatGPT';
|
||||||
|
|
||||||
|
@autobind
|
||||||
|
public install() {
|
||||||
|
return {
|
||||||
|
mentionHook: this.mentionHook,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@autobind
|
||||||
|
private async mentionHook(msg: Message) {
|
||||||
|
if (msg.text && (msg.text.includes('ChatGPT'))) {
|
||||||
|
// msg.textから"ChatGPT "を削除
|
||||||
|
const prompt = msg.text.replace('ChatGPT ', '');
|
||||||
|
msg.reply(await fetchChatGPT({prompt}));
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ChatGPT;
|
42
src/utils/fetchChatGPT.ts
Normal file
42
src/utils/fetchChatGPT.ts
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
import {isNil} from 'lodash';
|
||||||
|
import ENDPOINTS from '@/constants/ENDPOINTS.js';
|
||||||
|
import GPT3_MODELS from '@/constants/GPT3_MODELS.js';
|
||||||
|
import config from '@/config';
|
||||||
|
|
||||||
|
const COMPLETION_ENDPOINT = `${ENDPOINTS.OPEN_AI}/completions`;
|
||||||
|
|
||||||
|
// 0~1
|
||||||
|
const TEMPERATURE = 0.9;
|
||||||
|
// 2048 or 4000
|
||||||
|
const MAX_TOKENS = 4000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://beta.openai.com/docs/api-reference/completions/create
|
||||||
|
*/
|
||||||
|
async function fetchChatGPT({prompt}: {prompt: string}): Promise<string> {
|
||||||
|
if (isNil(config.openAiApiKey)) {
|
||||||
|
return '利用する準備ができていないようです...?';
|
||||||
|
}
|
||||||
|
const options = {
|
||||||
|
model: GPT3_MODELS.davinci,
|
||||||
|
max_tokens: MAX_TOKENS,
|
||||||
|
temperature: TEMPERATURE,
|
||||||
|
prompt,
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch(COMPLETION_ENDPOINT, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${config.openAiApiKey}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(options),
|
||||||
|
});
|
||||||
|
|
||||||
|
const results = await response.json();
|
||||||
|
// results.choicesの中からランダムに返す
|
||||||
|
const randomIndex = Math.floor(Math.random() * results.choices.length);
|
||||||
|
return results.choices[randomIndex].text;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default fetchChatGPT;
|
Loading…
Reference in a new issue