11import { Command } from '../types' ;
22import { Configuration , OpenAIApi } from 'openai' ;
3- import grzesJson from '../../grzes.json' ;
3+ import Fsp from 'fs/promises' ;
4+ import Path from 'path' ;
45import Natural from 'natural' ;
56
6- const tokenizer = new Natural . AggressiveTokenizerPl ( ) ;
7- const tfidf = grzesJson . reduce ( ( tfidf , line ) => {
8- tfidf . addDocument ( tokenizer . tokenize ( line ) ) ;
9- return tfidf ;
10- } , new Natural . TfIdf ( ) ) ;
11-
127const wait = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
138
149const configuration = new Configuration ( {
@@ -33,12 +28,16 @@ const grzesiu: Command = {
3328 const username = ( msg . member ?. displayName || msg . author . username ) . trim ( ) . split ( / \s / ) [ 0 ] ;
3429 const question = args . join ( ' ' ) ;
3530
31+ const grzesJson = JSON . parse (
32+ await Fsp . readFile ( Path . join ( __dirname , '..' , '..' , 'grzes.json' ) , 'utf-8' ) ,
33+ ) as string [ ] ;
34+
3635 const generators = {
3736 getGrzesiuAnswerFromOpenAI,
3837 getRandomGrzesiuAnswers,
3938 } ;
4039
41- const messages = await generators . getRandomGrzesiuAnswers ( username , question ) ;
40+ const messages = await generators . getRandomGrzesiuAnswers ( username , question , grzesJson ) ;
4241
4342 if ( ! messages . length ) {
4443 return msg . channel . send ( `Niestety, Grzesiu nie miał nic do powiedzenia!` ) ;
@@ -58,22 +57,17 @@ export default grzesiu;
5857const getRandomInt = ( len : number ) => Math . floor ( Math . random ( ) * len ) ;
5958
6059interface GrzesiuGenerator {
61- ( username : string , question : string ) : Promise < string [ ] > ;
60+ ( username : string , question : string , grzesJson : string [ ] ) : Promise < string [ ] > ;
6261}
6362
6463// random
65- const getRandomGrzesiuAnswers : GrzesiuGenerator = async ( _username , question ) => {
64+ const getRandomGrzesiuAnswers : GrzesiuGenerator = async ( _username , question , grzesJson ) => {
6665 const g = grzesJson
6766 . map ( ( l ) => l . trim ( ) )
6867 . filter ( ( line ) => ! BANNED_PATTERNS . test ( line ) && line . length > 0 ) ;
6968
7069 const potentialItems =
71- question . trim ( ) . length > 0
72- ? g . filter ( ( l , idx ) => {
73- const coeff = ( tfidf . tfidf ( question , idx ) as unknown as number ) || 1 ;
74- return coeff * Natural . DiceCoefficient ( question , l ) > 0.35 ;
75- } )
76- : [ ] ;
70+ question . trim ( ) . length > 0 ? g . filter ( ( l ) => Natural . DiceCoefficient ( question , l ) > 0.35 ) : [ ] ;
7771
7872 const initialIndex =
7973 potentialItems . length > 0 ? g . indexOf ( potentialItems [ getRandomInt ( potentialItems . length ) ] ) : - 1 ;
@@ -98,8 +92,8 @@ const getRandomGrzesiuAnswers: GrzesiuGenerator = async (_username, question) =>
9892} ;
9993
10094// openAI
101- const getGrzesiuAnswerFromOpenAI : GrzesiuGenerator = async ( username , question ) => {
102- const prompt = await generateGrzesiuPrompt ( username , question ) ;
95+ const getGrzesiuAnswerFromOpenAI : GrzesiuGenerator = async ( username , question , grzesJson ) => {
96+ const prompt = await generateGrzesiuPrompt ( username , question , grzesJson ) ;
10397
10498 const engine = 'text-davinci-001' ;
10599 // const engine = 'text-babbage-001';
@@ -134,7 +128,7 @@ const getRandomIndices = (num: number, max: number) => {
134128 return [ ...set ] ;
135129} ;
136130
137- const generateGrzesiuPrompt = async ( username : string , question : string ) => {
131+ const generateGrzesiuPrompt = async ( username : string , question : string , grzesJson : string [ ] ) => {
138132 const indices = getRandomIndices ( 100 , grzesJson . length ) ;
139133 const uniqueLines = [ ...new Set ( indices . map ( ( idx ) => grzesJson [ idx ] . trim ( ) ) ) ] . filter (
140134 ( line ) => ! BANNED_PATTERNS . test ( line ) && line . length > 0 ,
0 commit comments