2023-02-12 22:24:36 -05:00
|
|
|
require('dotenv').config();
|
|
|
|
|
const { KeyvFile } = require('keyv-file');
|
2023-05-10 23:47:26 -04:00
|
|
|
const { genAzureEndpoint } = require('../../utils/genAzureEndpoints');
|
2023-05-18 11:09:31 -07:00
|
|
|
const tiktoken = require('@dqbd/tiktoken');
|
2023-05-18 04:52:35 +05:30
|
|
|
const encoding_for_model = tiktoken.encoding_for_model;
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
const askClient = async ({
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
model,
|
2023-05-18 04:51:30 +05:30
|
|
|
oaiApiKey,
|
2023-03-31 03:22:57 +08:00
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix,
|
|
|
|
|
temperature,
|
|
|
|
|
top_p,
|
|
|
|
|
presence_penalty,
|
2023-04-01 23:27:44 +08:00
|
|
|
frequency_penalty,
|
2023-03-31 03:22:57 +08:00
|
|
|
onProgress,
|
feat: Auth and User System (#205)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* fix: issue with api error watcher (#12)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Update README.md
* Update LOCAL_INSTALL structure
* Add local testing instructions
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Fixup: LOCAL_INSTALL.md PS instructions (#200) (#30)
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
* feat: send user with completion to protect against abuse (#31)
* Fixup: LOCAL_INSTALL.md PS instructions (#200)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Send user id to openai to protect against abuse
* add meilisearch to gitignore
* Remove webpack
---------
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
---------
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
Co-authored-by: Alfredo Fomitchenko <alfredo.fomitchenko@mail.polimi.it>
2023-05-07 10:04:51 -07:00
|
|
|
abortController,
|
|
|
|
|
userId
|
2023-03-31 03:22:57 +08:00
|
|
|
}) => {
|
2023-05-09 17:42:55 -04:00
|
|
|
const { ChatGPTClient } = await import('@waylaidwanderer/chatgpt-api');
|
2023-02-21 21:30:56 -05:00
|
|
|
const store = {
|
2023-03-07 13:53:23 -05:00
|
|
|
store: new KeyvFile({ filename: './data/cache.json' })
|
2023-02-21 21:30:56 -05:00
|
|
|
};
|
|
|
|
|
|
2023-05-09 17:42:55 -04:00
|
|
|
const azure = process.env.AZURE_OPENAI_API_KEY ? true : false;
|
2023-05-18 04:52:35 +05:30
|
|
|
if (promptPrefix == null) {
|
2023-05-18 11:09:31 -07:00
|
|
|
promptText = 'You are ChatGPT, a large language model trained by OpenAI.';
|
2023-05-18 04:52:35 +05:30
|
|
|
} else {
|
|
|
|
|
promptText = promptPrefix;
|
|
|
|
|
}
|
2023-05-14 17:26:21 -04:00
|
|
|
const maxContextTokens = model === 'gpt-4' ? 8191 : model === 'gpt-4-32k' ? 32767 : 4095; // 1 less than maximum
|
2023-03-31 03:22:57 +08:00
|
|
|
const clientOptions = {
|
2023-04-05 21:21:02 +08:00
|
|
|
reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null,
|
2023-05-09 17:42:55 -04:00
|
|
|
azure,
|
2023-05-14 17:26:21 -04:00
|
|
|
maxContextTokens,
|
2023-03-31 03:22:57 +08:00
|
|
|
modelOptions: {
|
2023-05-14 17:26:21 -04:00
|
|
|
model,
|
2023-03-31 03:22:57 +08:00
|
|
|
temperature,
|
|
|
|
|
top_p,
|
2023-04-01 23:27:44 +08:00
|
|
|
presence_penalty,
|
|
|
|
|
frequency_penalty
|
2023-03-31 03:22:57 +08:00
|
|
|
},
|
|
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix,
|
2023-05-18 11:09:31 -07:00
|
|
|
proxy: process.env.PROXY || null
|
2023-05-14 17:26:21 -04:00
|
|
|
// debug: true
|
2023-03-31 03:22:57 +08:00
|
|
|
};
|
|
|
|
|
|
2023-05-18 04:51:30 +05:30
|
|
|
let apiKey = oaiApiKey ? oaiApiKey : process.env.OPENAI_KEY || null;
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-05-09 17:42:55 -04:00
|
|
|
if (azure) {
|
2023-05-18 04:51:30 +05:30
|
|
|
apiKey = oaiApiKey ? oaiApiKey : process.env.AZURE_OPENAI_API_KEY || null;
|
2023-05-14 17:26:21 -04:00
|
|
|
clientOptions.reverseProxyUrl = genAzureEndpoint({
|
|
|
|
|
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
|
|
|
|
|
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
2023-05-10 23:47:26 -04:00
|
|
|
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION
|
|
|
|
|
});
|
2023-02-12 22:24:36 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-09 17:42:55 -04:00
|
|
|
const client = new ChatGPTClient(apiKey, clientOptions, store);
|
2023-05-14 17:26:21 -04:00
|
|
|
|
2023-05-09 17:42:55 -04:00
|
|
|
const options = {
|
|
|
|
|
onProgress,
|
|
|
|
|
abortController,
|
|
|
|
|
...(parentMessageId && conversationId ? { parentMessageId, conversationId } : {})
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-18 04:52:35 +05:30
|
|
|
const enc = encoding_for_model(model);
|
|
|
|
|
const text_tokens = enc.encode(text);
|
|
|
|
|
const prompt_tokens = enc.encode(promptText);
|
|
|
|
|
// console.log("Prompt tokens = ", prompt_tokens.length);
|
|
|
|
|
// console.log("Message Tokens = ", text_tokens.length);
|
|
|
|
|
|
2023-05-09 17:42:55 -04:00
|
|
|
const res = await client.sendMessage(text, { ...options, userId });
|
2023-05-18 04:52:35 +05:30
|
|
|
// return res;
|
|
|
|
|
// create a new response object that includes the token counts
|
2023-05-18 11:09:31 -07:00
|
|
|
const newRes = {
|
|
|
|
|
...res,
|
|
|
|
|
usage: {
|
|
|
|
|
prompt_tokens: prompt_tokens.length,
|
|
|
|
|
completion_tokens: text_tokens.length,
|
|
|
|
|
total_tokens: prompt_tokens.length + text_tokens.length
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-05-18 04:52:35 +05:30
|
|
|
|
2023-05-18 11:09:31 -07:00
|
|
|
return newRes;
|
2023-02-12 22:24:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = { askClient };
|