1- import { Command } from '../types' ;
21import { Configuration , OpenAIApi } from 'openai' ;
3- import Fsp from 'fs/promises' ;
4- import Path from 'path' ;
52import Natural from 'natural' ;
63
7- const wait = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
8-
94const configuration = new Configuration ( {
105 apiKey : process . env . OPENAI_API_KEY ,
116} ) ;
127const openai = new OpenAIApi ( configuration ) ;
138
149const MAX_TOKENS = 2049 ;
1510const RESPONSE_TOKENS = 64 ;
11+
1612const MAX_GENERATED_CONTENT_LEN = Math . floor ( ( MAX_TOKENS - RESPONSE_TOKENS ) / 2 ) ; // make it cheaper
1713const BANNED_PATTERNS = / [ ` \[ \] { } \( \) ] | h t t p / g;
18- const COOLDOWN = 20 ;
19- const GRZESIU_DELAY = 1500 ;
20- const GRZESIU_NAME = 'grzegorz' ;
21-
22- const grzesiu : Command = {
23- name : 'grzesiu' ,
24- description : 'Użyj tego, gdy tęsknisz za Grzesiem' ,
25- args : 'optional' ,
26- cooldown : COOLDOWN ,
27- async execute ( msg , args ) {
28- const username = ( msg . member ?. displayName || msg . author . username ) . trim ( ) . split ( / \s / ) [ 0 ] ;
29- const question = args . join ( ' ' ) ;
30-
31- const grzesJson = JSON . parse (
32- await Fsp . readFile ( Path . join ( __dirname , '..' , 'grzes.json' ) , 'utf-8' ) ,
33- ) as string [ ] ;
34-
35- const generators = {
36- getGrzesiuAnswerFromOpenAI,
37- getRandomGrzesiuAnswers,
38- } ;
39-
40- const messages = await generators . getRandomGrzesiuAnswers ( username , question , grzesJson ) ;
41-
42- if ( ! messages . length ) {
43- return msg . channel . send ( `Niestety, Grzesiu nie miał nic do powiedzenia!` ) ;
44- }
45-
46- return messages . reduce ( async ( acc , message ) => {
47- await acc ;
48- await msg . channel . send ( `_${ message } _` ) ;
49- await wait ( 200 + Math . random ( ) * GRZESIU_DELAY ) ;
50- return null ;
51- } , Promise . resolve ( null ) ) ;
52- } ,
53- } ;
54-
55- export default grzesiu ;
5614
5715const getRandomInt = ( len : number ) => Math . floor ( Math . random ( ) * len ) ;
5816
59- interface GrzesiuGenerator {
60- ( username : string , question : string , grzesJson : string [ ] ) : Promise < string [ ] > ;
17+ interface KocopolyGenerator {
18+ ( username : string , question : string , kocopolyJson : string [ ] , kocopolyName : string ) : Promise <
19+ string [ ]
20+ > ;
6121}
6222
6323// random
64- const getRandomGrzesiuAnswers : GrzesiuGenerator = async ( _username , question , grzesJson ) => {
65- const g = grzesJson
24+ export const getRandomKocopolyAnswers : KocopolyGenerator = async (
25+ _username ,
26+ question ,
27+ kocopolyJson ,
28+ _kocopolyName ,
29+ ) => {
30+ const g = kocopolyJson
6631 . map ( ( l ) => l . trim ( ) )
6732 . filter ( ( line ) => ! BANNED_PATTERNS . test ( line ) && line . length > 0 ) ;
6833
@@ -92,8 +57,13 @@ const getRandomGrzesiuAnswers: GrzesiuGenerator = async (_username, question, gr
9257} ;
9358
9459// openAI
95- const getGrzesiuAnswerFromOpenAI : GrzesiuGenerator = async ( username , question , grzesJson ) => {
96- const prompt = await generateGrzesiuPrompt ( username , question , grzesJson ) ;
60+ export const getKocopolyAnswerFromOpenAI : KocopolyGenerator = async (
61+ username ,
62+ question ,
63+ kocopolyJson ,
64+ kocopolyName ,
65+ ) => {
66+ const prompt = await generateKocopolyPrompt ( username , question , kocopolyJson , kocopolyName ) ;
9767
9868 const engine = 'text-davinci-001' ;
9969 // const engine = 'text-babbage-001';
@@ -116,8 +86,8 @@ const getGrzesiuAnswerFromOpenAI: GrzesiuGenerator = async (username, question,
11686 const messages = response . data . choices [ 0 ] . text
11787 . split ( '\n' )
11888 . map ( ( l ) => l . trim ( ) )
119- . filter ( ( l ) => l . startsWith ( `${ GRZESIU_NAME } :` ) )
120- . flatMap ( ( l ) => l . split ( `${ GRZESIU_NAME } :` ) )
89+ . filter ( ( l ) => l . startsWith ( `${ kocopolyName } :` ) )
90+ . flatMap ( ( l ) => l . split ( `${ kocopolyName } :` ) )
12191 . filter ( ( l ) => l . trim ( ) . length > 0 ) ;
12292 return messages ;
12393} ;
@@ -128,21 +98,26 @@ const getRandomIndices = (num: number, max: number) => {
12898 return [ ...set ] ;
12999} ;
130100
131- const generateGrzesiuPrompt = async ( username : string , question : string , grzesJson : string [ ] ) => {
132- const indices = getRandomIndices ( 100 , grzesJson . length ) ;
133- const uniqueLines = [ ...new Set ( indices . map ( ( idx ) => grzesJson [ idx ] . trim ( ) ) ) ] . filter (
101+ const generateKocopolyPrompt = async (
102+ username : string ,
103+ question : string ,
104+ kocopolyJson : string [ ] ,
105+ kocopolyName : string ,
106+ ) => {
107+ const indices = getRandomIndices ( 100 , kocopolyJson . length ) ;
108+ const uniqueLines = [ ...new Set ( indices . map ( ( idx ) => kocopolyJson [ idx ] . trim ( ) ) ) ] . filter (
134109 ( line ) => ! BANNED_PATTERNS . test ( line ) && line . length > 0 ,
135110 ) ;
136111
137112 const getFullConvo = ( txt : string , username : string , question : string ) => {
138113 if ( question ) {
139- return `${ txt . trim ( ) } \n${ username } : ${ question } \n${ GRZESIU_NAME } :` ;
114+ return `${ txt . trim ( ) } \n${ username } : ${ question } \n${ kocopolyName } :` ;
140115 }
141- return `${ txt . trim ( ) } \n${ GRZESIU_NAME } :` ;
116+ return `${ txt . trim ( ) } \n${ kocopolyName } :` ;
142117 } ;
143118
144119 const txt = uniqueLines . reduce ( ( txt , line ) => {
145- const newTxt = txt + `${ GRZESIU_NAME } : ` + line + '\n' ;
120+ const newTxt = txt + `${ kocopolyName } : ` + line + '\n' ;
146121 const fullConvo = getFullConvo ( newTxt , username , question ) ;
147122 return fullConvo . length <= MAX_GENERATED_CONTENT_LEN ? newTxt : txt ;
148123 } , '' ) ;
0 commit comments