11import { Configuration , ConfigurationParameters , OpenAIApi } from 'openai'
2-
2+ import { OpenAI , OpenAIChat } from 'langchain/llms/openai'
33import FormData from 'form-data'
44import ChatGPTClient from 'src/lib/ai/ChatGPTClient'
5+ import { defaultUnstructuredUrl } from '../myfirebase'
56
6- type OpenAIEngine = 'text-ada-001' | 'text-davinci-003'
7+ type OpenAIEngine = 'text-ada-001' | 'text-davinci-003'
78
89const engineId : OpenAIEngine = 'text-davinci-003'
910export const Config = {
@@ -14,24 +15,25 @@ export const Config = {
1415
1516class CustomFormData extends FormData {
1617 getHeaders ( ) : Record < string , string > {
17- return { } ;
18+ return { }
1819 }
1920}
2021
2122
2223let aiUserSettings : Partial < AiUserSettings > = {
2324 server : 'openai' ,
24- openaiSettings : { apiKey : process . env . OPENAPI_KEY ?? '' } ,
25+ openaiSettings : { apiKey : process . env . OPENAPI_KEY ?? '' } ,
2526 unstructuredSettings : { endpoint : process . env . UNSTRUCTURED_URL ?? '' } ,
2627}
28+
2729export interface AiUserSettings {
2830 server : 'openai' | 'azure' | 'hosted'
29- azureSettings : { apiKey :string , basePath :string } , // only applicable if server is 'azure
30- openaiSettings : { apiKey : string } , // only applicable if server is 'openai'
31- unstructuredSettings : { apiKey ?: string , endpoint : string } ,
31+ azureSettings : { apiKey : string , basePath : string } , // only applicable if server is 'azure
32+ openaiSettings : { apiKey : string } , // only applicable if server is 'openai'
33+ unstructuredSettings : { apiKey ?: string , endpoint : string } ,
3234}
3335
34- export function applyAiUserSettings ( settings :AiUserSettings ) {
36+ export function applyAiUserSettings ( settings : AiUserSettings ) {
3537 aiUserSettings = { ...settings }
3638}
3739
@@ -60,8 +62,9 @@ export function getLangchainConfig() {
6062 return {
6163 azureOpenAIApiKey : aiUserSettings . azureSettings ?. apiKey ,
6264 azureOpenAIApiInstanceName : 'kevin-test-openai-1' , // FIXME: configurable
63- azureOpenAIApiDeploymentName : Config . embedModel ,
64- azureOpenAIApiVersion : '2023-03-15-preview'
65+ azureOpenAIApiVersion : '2023-03-15-preview' ,
66+ azureOpenAIApiDeploymentName : Config . chatModel . replaceAll ( / \. / g, '' ) ,
67+ azureOpenAIApiEmbeddingsDeploymentName : Config . embedModel . replaceAll ( / \. / g, '' ) ,
6568 }
6669 } else {
6770 throw new Error ( 'unsupported' )
@@ -73,7 +76,7 @@ export function getOpenAIConfig(deployment?: string) {
7376 const params = { ...getOpenAIParams ( ) }
7477 if ( params . basePath ) {
7578 if ( deployment ) {
76- const d2 = deployment . replaceAll ( / \. / g, '' )
79+ const d2 = deployment . replaceAll ( / \. / g, '' )
7780 params . basePath = `${ params . basePath } /openai/deployments/${ d2 } `
7881 params . baseOptions = {
7982 headers : { 'api-key' : params . apiKey } ,
@@ -91,7 +94,7 @@ export function getOpenAIConfig(deployment?: string) {
9194
9295export const getOpenAIAPI = ( deployment ?: string ) => {
9396 const config = getOpenAIConfig ( deployment )
94- return new OpenAIApi ( config ) ;
97+ return new OpenAIApi ( config )
9598}
9699
97100export function getSettingsFromLocalStorage ( ) {
@@ -105,27 +108,36 @@ export function getSettingsFromLocalStorage() {
105108}
106109
107110
108-
109- function getChatGPTClientOptions ( clientOptions ?:any ) {
111+ function getChatGPTClientOptions ( clientOptions ?: any ) {
110112 const cfg = getOpenAIConfig ( Config . chatModel )
111113 if ( cfg . basePath ) {
112114 // Set up azure mode
113115 // https://kevin-test-openai-1.openai.azure.com//openai/deployments/gpt-35-turbo/chat/completions?api-version=2023-03-15-preview
114116 const opts = { azure : true , reverseProxyUrl : `${ cfg . basePath } /chat/completions?api-version=2023-03-15-preview` }
115- return { ...( clientOptions ?? { } ) , ...opts }
117+ return { ...( clientOptions ?? { } ) , ...opts }
116118 } else {
117119 return clientOptions ?? { }
118120 }
119121
120122}
121123
122- export function getChatGPTClient ( cache :any ) {
124+ export function getChatGPTClient ( cache : any ) {
123125 const clientOptions = getChatGPTClientOptions ( )
124126 const client = new ChatGPTClient ( getOpenAIParams ( ) . apiKey , clientOptions , cache )
125127 return client
126128}
127129
128130export function getUnstructuredEndpoint ( ) {
129- const endpoint = aiUserSettings ?. unstructuredSettings ?. endpoint ?? ''
131+ let endpoint = aiUserSettings ?. unstructuredSettings ?. endpoint
132+ if ( ! endpoint ) {
133+ endpoint = process . env . UNSTRUCTURED_URL
134+ }
135+ if ( ! endpoint ) {
136+ endpoint = defaultUnstructuredUrl
137+ }
130138 return `${ endpoint } /general/v0/general`
131139}
140+
141+ export function getOpenAIChat ( ) {
142+ return new OpenAIChat ( getLangchainConfig ( ) )
143+ }
0 commit comments