forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Encrypt & Expire User Provided Keys, feat: Rate Limiting (d…
…anny-avila#874) * docs: make_your_own.md formatting fix for mkdocs * feat: add express-mongo-sanitize feat: add login/registration rate limiting * chore: remove unnecessary console log * wip: remove token handling from localStorage to encrypted DB solution * refactor: minor change to UserService * fix mongo query and add keys route to server * fix backend controllers and simplify schema/crud * refactor: rename token to key to separate from access/refresh tokens, setTokenDialog -> setKeyDialog * refactor(schemas): TEndpointOption token -> key * refactor(api): use new encrypted key retrieval system * fix(SetKeyDialog): fix key prop error * fix(abortMiddleware): pass random UUID if messageId is not generated yet for proper error display on frontend * fix(getUserKey): wrong prop passed in arg, adds error handling * fix: prevent message without conversationId from saving to DB, prevents branching on the frontend to a new top-level branch * refactor: change wording of multiple display messages * refactor(checkExpiry -> checkUserKeyExpiry): move to UserService file * fix: type imports from common * refactor(SubmitButton): convert to TS * refactor(key.ts): change localStorage map key name * refactor: add new custom tailwind classes to better match openAI colors * chore: remove unnecessary warning and catch ScreenShot error * refactor: move userKey frontend logic to hooks and remove use of localStorage and instead query the DB * refactor: invalidate correct query key, memoize userKey hook, conditionally render SetKeyDialog to avoid unnecessary calls, refactor SubmitButton props and useEffect for showing 'provide key first' * fix(SetKeyDialog): use enum-like object for expiry values feat(Dropdown): add optionsClassName to dynamically change dropdown options container classes * fix: handle edge case where user had provided a key but the server changes to env variable for keys * refactor(OpenAI/titleConvo): move titling to client to retain authorized credentials in message lifecycle for titling * fix(azure): handle user_provided keys correctly for azure * feat: send user Id to OpenAI to differentiate users in completion requests * refactor(OpenAI/titleConvo): adding tokens helps minimize LLM from using the language in title response * feat: add delete endpoint for keys * chore: remove throttling of title * feat: add 'Data controls' to Settings, add 'Revoke' keys feature in Key Dialog and Data controls * refactor: reorganize PluginsClient files in langchain format * feat: use langchain for titling convos * chore: cleanup titling convo, with fallback to original method, escape braces, use only snippet for language detection * refactor: move helper functions to appropriate langchain folders for reusability * fix: userProvidesKey handling for gptPlugins * fix: frontend handling of plugins key * chore: cleanup logging and ts-ignore SSE * fix: forwardRef misuse in DangerButton * fix(GoogleConfig/FileUpload): localize errors and simplify validation with zod * fix: cleanup google logging and fix user provided key handling * chore: remove titling from google * chore: removing logging from browser endpoint * wip: fix menu flicker * feat: useLocalStorage hook * feat: add Tooltip for UI * refactor(EndpointMenu): utilize Tooltip and useLocalStorage, remove old 'New Chat' slide-over * fix(e2e): use testId for endpoint menu trigger * chore: final touches to EndpointMenu before future refactor to declutter component * refactor(localization): change select endpoint to open menu and add translations * chore: add final prop to error message response * ci: minor edits to facilitate testing * ci: new e2e test which tests for new key setting/revoking features
- Loading branch information
1 parent
64f1557
commit 4ca43fb
Showing
122 changed files
with
1,930 additions
and
963 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const runTitleChain = require('./runTitleChain'); | ||
|
||
module.exports = { | ||
runTitleChain, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const { z } = require('zod'); | ||
const { langPrompt, createTitlePrompt } = require('../prompts'); | ||
const { escapeBraces, getSnippet } = require('../output_parsers'); | ||
const { createStructuredOutputChainFromZod } = require('langchain/chains/openai_functions'); | ||
|
||
const langSchema = z.object({ | ||
language: z.string().describe('The language of the input text (full noun, no abbreviations).'), | ||
}); | ||
|
||
const createLanguageChain = ({ llm }) => | ||
createStructuredOutputChainFromZod(langSchema, { | ||
prompt: langPrompt, | ||
llm, | ||
// verbose: true, | ||
}); | ||
|
||
const titleSchema = z.object({ | ||
title: z.string().describe('The title-cased title of the conversation in the given language.'), | ||
}); | ||
const createTitleChain = ({ llm, convo }) => { | ||
const titlePrompt = createTitlePrompt({ convo }); | ||
return createStructuredOutputChainFromZod(titleSchema, { | ||
prompt: titlePrompt, | ||
llm, | ||
// verbose: true, | ||
}); | ||
}; | ||
|
||
const runTitleChain = async ({ llm, text, convo }) => { | ||
let snippet = text; | ||
try { | ||
snippet = getSnippet(text); | ||
} catch (e) { | ||
console.log('Error getting snippet of text for titleChain'); | ||
console.log(e); | ||
} | ||
const languageChain = createLanguageChain({ llm }); | ||
const titleChain = createTitleChain({ llm, convo: escapeBraces(convo) }); | ||
const { language } = await languageChain.run(snippet); | ||
return (await titleChain.run(language)).title; | ||
}; | ||
|
||
module.exports = runTitleChain; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const createLLM = require('./createLLM'); | ||
|
||
module.exports = { | ||
createLLM, | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Escaping curly braces is necessary for LangChain to correctly process the prompt | ||
function escapeBraces(str) { | ||
return str | ||
.replace(/({{2,})|(}{2,})/g, (match) => `${match[0]}`) | ||
.replace(/{|}/g, (match) => `${match}${match}`); | ||
} | ||
|
||
function getSnippet(text) { | ||
let limit = 50; | ||
let splitText = escapeBraces(text).split(' '); | ||
|
||
if (splitText.length === 1 && splitText[0].length > limit) { | ||
return splitText[0].substring(0, limit); | ||
} | ||
|
||
let result = ''; | ||
let spaceCount = 0; | ||
|
||
for (let i = 0; i < splitText.length; i++) { | ||
if (result.length + splitText[i].length <= limit) { | ||
result += splitText[i] + ' '; | ||
spaceCount++; | ||
} else { | ||
break; | ||
} | ||
|
||
if (spaceCount == 10) { | ||
break; | ||
} | ||
} | ||
|
||
return result.trim(); | ||
} | ||
|
||
module.exports = { | ||
escapeBraces, | ||
getSnippet, | ||
}; |
Oops, something went wrong.