2023-03-02 13:52:41 -05:00
const { Configuration , OpenAIApi } = require ( 'openai' ) ;
2023-03-15 12:47:30 -04:00
const _ = require ( 'lodash' ) ;
2023-05-10 23:47:26 -04:00
const { genAzureEndpoint } = require ( '../utils/genAzureEndpoints' ) ;
2023-03-02 13:52:41 -05:00
2023-05-10 23:47:26 -04:00
const proxyEnvToAxiosProxy = ( proxyString ) => {
2023-03-13 15:55:18 +08:00
if ( ! proxyString ) return null ;
const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/ ;
const [ , protocol , username , password , host , port ] = proxyString . match ( regex ) ;
const proxyConfig = {
protocol ,
host ,
port : port ? parseInt ( port ) : undefined ,
auth : username && password ? { username , password } : undefined
} ;
2023-03-15 12:47:30 -04:00
return proxyConfig ;
} ;
2023-03-13 15:55:18 +08:00
2023-05-17 21:58:56 -04:00
const titleConvo = async ( { endpoint , text , response , oaiApiKey } ) => {
2023-03-15 15:21:04 -04:00
let title = 'New Chat' ;
2023-04-08 00:36:58 +08:00
const ChatGPTClient = ( await import ( '@waylaidwanderer/chatgpt-api' ) ) . default ;
2023-03-28 19:50:37 -04:00
2023-04-08 00:36:58 +08:00
try {
const instructionsPayload = {
role : 'system' ,
content : ` Detect user language and write in the same language an extremely concise title for this conversation, which you must accurately detect. Write in the detected language. Title in 5 Words or Less. No Punctuation or Quotation. All first letters of every word should be capitalized and complete only the title in User Language only.
2023-03-28 19:50:37 -04:00
2023-04-08 00:36:58 +08:00
|| > User :
"${text}"
|| > Response :
"${JSON.stringify(response?.text)}"
|| > Title : `
} ;
2023-03-22 09:38:38 -04:00
2023-05-10 23:56:24 -04:00
const azure = process . env . AZURE _OPENAI _API _KEY ? true : false ;
2023-04-08 00:36:58 +08:00
const options = {
2023-05-10 23:56:24 -04:00
azure ,
2023-04-08 00:36:58 +08:00
reverseProxyUrl : process . env . OPENAI _REVERSE _PROXY || null ,
proxy : process . env . PROXY || null
} ;
2023-03-22 09:38:38 -04:00
2023-04-08 00:36:58 +08:00
const titleGenClientOptions = JSON . parse ( JSON . stringify ( options ) ) ;
2023-03-22 09:38:38 -04:00
2023-04-08 00:36:58 +08:00
titleGenClientOptions . modelOptions = {
model : 'gpt-3.5-turbo' ,
temperature : 0 ,
presence _penalty : 0 ,
frequency _penalty : 0
} ;
2023-03-15 15:21:04 -04:00
2023-05-17 21:58:56 -04:00
let apiKey = oaiApiKey || process . env . OPENAI _KEY ;
2023-05-10 23:47:26 -04:00
if ( azure ) {
apiKey = process . env . AZURE _OPENAI _API _KEY ;
titleGenClientOptions . reverseProxyUrl = genAzureEndpoint ( {
azureOpenAIApiInstanceName : process . env . AZURE _OPENAI _API _INSTANCE _NAME ,
azureOpenAIApiDeploymentName : process . env . AZURE _OPENAI _API _DEPLOYMENT _NAME ,
azureOpenAIApiVersion : process . env . AZURE _OPENAI _API _VERSION
} ) ;
}
const titleGenClient = new ChatGPTClient ( apiKey , titleGenClientOptions ) ;
2023-04-08 00:36:58 +08:00
const result = await titleGenClient . getCompletion ( [ instructionsPayload ] , null ) ;
2023-05-10 23:47:26 -04:00
title = result . choices [ 0 ] . message . content . replace ( /\s+/g , ' ' ) . replaceAll ( '"' , '' ) . trim ( ) ;
2023-03-15 15:21:04 -04:00
} catch ( e ) {
console . error ( e ) ;
console . log ( 'There was an issue generating title, see error above' ) ;
}
console . log ( 'CONVERSATION TITLE' , title ) ;
return title ;
2023-03-02 13:52:41 -05:00
} ;
2023-03-15 15:21:04 -04:00
const throttledTitleConvo = _ . throttle ( titleConvo , 1000 ) ;
2023-03-15 12:47:30 -04:00
2023-03-15 15:21:04 -04:00
module . exports = throttledTitleConvo ;