@@ -13,6 +13,7 @@ import {
13
13
} from '../types'
14
14
import {
15
15
alterGeneratedImg ,
16
+ chatCompletion ,
16
17
getChatModel ,
17
18
getDalleModel ,
18
19
getDalleModelPrice ,
@@ -47,6 +48,7 @@ import { AxiosError } from 'axios'
47
48
import { Callbacks } from '../types'
48
49
import { LlmsBot } from '../llms'
49
50
import { type PhotoSize } from 'grammy/types'
51
+ import { responseWithVoice } from '../voice-to-voice-gpt/helpers'
50
52
51
53
const priceAdjustment = config . openAi . chatGpt . priceAdjustment
52
54
export class OpenAIBot implements PayableBot {
@@ -174,16 +176,21 @@ export class OpenAIBot implements PayableBot {
174
176
}
175
177
break
176
178
}
177
- case SupportedCommands . ask : {
179
+ case SupportedCommands . ask :
180
+ case SupportedCommands . talk : {
178
181
if ( this . botSuspended ) {
179
182
ctx . transient . analytics . sessionState = RequestState . Error
180
183
await sendMessage ( ctx , 'The bot is suspended' ) . catch ( async ( e ) => { await this . onError ( ctx , e ) } )
181
184
ctx . transient . analytics . actualResponseTime = now ( )
182
185
return
183
186
}
184
- ctx . session . openAi . chatGpt . requestQueue . push (
185
- await preparePrompt ( ctx , prompt )
186
- )
187
+ const adaptedPrompt = ( SupportedCommands . talk === command
188
+ ? 'Keep it short, like a phone call'
189
+ : '' ) + await preparePrompt ( ctx , prompt )
190
+ ctx . session . openAi . chatGpt . requestQueue . push ( {
191
+ prompt : adaptedPrompt ,
192
+ outputFormat : SupportedCommands . ask === command ? 'text' : 'voice'
193
+ } )
187
194
if ( ! ctx . session . openAi . chatGpt . isProcessingQueue ) {
188
195
ctx . session . openAi . chatGpt . isProcessingQueue = true
189
196
await this . onChatRequestHandler ( ctx ) . then ( ( ) => {
@@ -407,7 +414,7 @@ export class OpenAIBot implements PayableBot {
407
414
)
408
415
}
409
416
410
- private async promptGen ( data : ChatPayload , msgId ?: number ) : Promise < { price : number , chat : ChatConversation [ ] } > {
417
+ private async completionGen ( data : ChatPayload , msgId ?: number , outputFormat = 'text' ) : Promise < { price : number , chat : ChatConversation [ ] } > {
411
418
const { conversation, ctx, model } = data
412
419
try {
413
420
if ( ! msgId ) {
@@ -420,29 +427,42 @@ export class OpenAIBot implements PayableBot {
420
427
} )
421
428
) . message_id
422
429
}
423
- const isTypingEnabled = config . openAi . chatGpt . isTypingEnabled
424
- if ( isTypingEnabled ) {
425
- ctx . chatAction = 'typing'
426
- }
427
- const completion = await streamChatCompletion (
428
- conversation ,
429
- ctx ,
430
- model ,
431
- msgId ,
432
- true // telegram messages has a character limit
433
- )
434
- if ( isTypingEnabled ) {
435
- ctx . chatAction = null
436
- }
437
- if ( completion ) {
438
- ctx . transient . analytics . sessionState = RequestState . Success
439
- ctx . transient . analytics . actualResponseTime = now ( )
440
- const price = getPromptPrice ( completion , data )
441
- this . logger . info (
442
- `streamChatCompletion result = tokens: ${ price . totalTokens } | ${ model } | price: ${ price . price } ¢` // price.promptTokens + price.completionTokens }
430
+ if ( outputFormat === 'text' ) {
431
+ const isTypingEnabled = config . openAi . chatGpt . isTypingEnabled
432
+ if ( isTypingEnabled ) {
433
+ ctx . chatAction = 'typing'
434
+ }
435
+ const completion = await streamChatCompletion (
436
+ conversation ,
437
+ ctx ,
438
+ model ,
439
+ msgId ,
440
+ true // telegram messages has a character limit
443
441
)
442
+ if ( isTypingEnabled ) {
443
+ ctx . chatAction = null
444
+ }
445
+ if ( completion ) {
446
+ ctx . transient . analytics . sessionState = RequestState . Success
447
+ ctx . transient . analytics . actualResponseTime = now ( )
448
+ const price = getPromptPrice ( completion , data )
449
+ this . logger . info (
450
+ `streamChatCompletion result = tokens: ${ price . totalTokens } | ${ model } | price: ${ price . price } ¢` // price.promptTokens + price.completionTokens }
451
+ )
452
+ return {
453
+ price : price . price ,
454
+ chat : conversation
455
+ }
456
+ }
457
+ } else {
458
+ const response = await chatCompletion ( conversation , ChatGPTModelsEnum . GPT_35_TURBO_16K )
459
+ conversation . push ( {
460
+ role : 'system' ,
461
+ content : response . completion
462
+ } )
463
+ await responseWithVoice ( response . completion , ctx as OnMessageContext , msgId )
444
464
return {
445
- price : price . price ,
465
+ price : response . price ,
446
466
chat : conversation
447
467
}
448
468
}
@@ -469,9 +489,10 @@ export class OpenAIBot implements PayableBot {
469
489
}
470
490
const { username } = ctx . me
471
491
const prompt = ctx . message ?. text ?. slice ( username . length + 1 ) ?? '' // @
472
- ctx . session . openAi . chatGpt . requestQueue . push (
473
- await preparePrompt ( ctx , prompt )
474
- )
492
+ ctx . session . openAi . chatGpt . requestQueue . push ( {
493
+ prompt : await preparePrompt ( ctx , prompt ) ,
494
+ outputFormat : 'text'
495
+ } )
475
496
if ( ! ctx . session . openAi . chatGpt . isProcessingQueue ) {
476
497
ctx . session . openAi . chatGpt . isProcessingQueue = true
477
498
await this . onChatRequestHandler ( ctx ) . then ( ( ) => {
@@ -494,9 +515,10 @@ export class OpenAIBot implements PayableBot {
494
515
return
495
516
}
496
517
const prompt = ctx . message ?. text ?. slice ( prefix . length ) ?? ''
497
- ctx . session . openAi . chatGpt . requestQueue . push (
498
- await preparePrompt ( ctx , prompt )
499
- )
518
+ ctx . session . openAi . chatGpt . requestQueue . push ( {
519
+ prompt : await preparePrompt ( ctx , prompt ) ,
520
+ outputFormat : 'text'
521
+ } )
500
522
if ( ! ctx . session . openAi . chatGpt . isProcessingQueue ) {
501
523
ctx . session . openAi . chatGpt . isProcessingQueue = true
502
524
await this . onChatRequestHandler ( ctx ) . then ( ( ) => {
@@ -516,9 +538,10 @@ export class OpenAIBot implements PayableBot {
516
538
ctx . transient . analytics . actualResponseTime = now ( )
517
539
return
518
540
}
519
- ctx . session . openAi . chatGpt . requestQueue . push (
520
- await preparePrompt ( ctx , ctx . message ?. text ?? '' )
521
- )
541
+ ctx . session . openAi . chatGpt . requestQueue . push ( {
542
+ prompt : await preparePrompt ( ctx , ctx . message ?. text ?? '' ) ,
543
+ outputFormat : 'text'
544
+ } )
522
545
if ( ! ctx . session . openAi . chatGpt . isProcessingQueue ) {
523
546
ctx . session . openAi . chatGpt . isProcessingQueue = true
524
547
await this . onChatRequestHandler ( ctx ) . then ( ( ) => {
@@ -556,9 +579,10 @@ export class OpenAIBot implements PayableBot {
556
579
if ( await this . freePromptChatGroup ( ctx , prompt as string ) ) {
557
580
return
558
581
}
559
- ctx . session . openAi . chatGpt . requestQueue . push (
560
- await preparePrompt ( ctx , prompt as string )
561
- )
582
+ ctx . session . openAi . chatGpt . requestQueue . push ( {
583
+ prompt : await preparePrompt ( ctx , prompt as string ) ,
584
+ outputFormat : 'text'
585
+ } )
562
586
if ( ! ctx . session . openAi . chatGpt . isProcessingQueue ) {
563
587
ctx . session . openAi . chatGpt . isProcessingQueue = true
564
588
await this . onChatRequestHandler ( ctx ) . then ( ( ) => {
@@ -588,7 +612,7 @@ export class OpenAIBot implements PayableBot {
588
612
ctx . transient . analytics . actualResponseTime = now ( )
589
613
return
590
614
}
591
- const { url, newPrompt } = hasUrl ( ctx , prompt )
615
+ const { url, newPrompt } = hasUrl ( ctx , prompt . prompt )
592
616
const hasCode = hasCodeSnippet ( ctx )
593
617
if ( chatConversation . length === 0 && ( hasCode || ! url ) ) {
594
618
chatConversation . push ( {
@@ -601,14 +625,14 @@ export class OpenAIBot implements PayableBot {
601
625
} else {
602
626
chatConversation . push ( {
603
627
role : 'user' ,
604
- content : prompt
628
+ content : prompt . prompt
605
629
} )
606
630
const payload = {
607
631
conversation : chatConversation ,
608
632
model : model || config . openAi . chatGpt . model ,
609
633
ctx
610
634
}
611
- const result = await this . promptGen ( payload )
635
+ const result = await this . completionGen ( payload , prompt . msgId , prompt . outputFormat )
612
636
ctx . session . openAi . chatGpt . chatConversation = [ ...result . chat ]
613
637
if (
614
638
! ( await this . payments . pay ( ctx as OnMessageContext , result . price ) )
0 commit comments