You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.6 KiB

11 months ago
  1. const { v4: uuidv4 } = require("uuid");
  2. const { WorkspaceChats } = require("../../models/workspaceChats");
  3. const { resetMemory } = require("./commands/reset");
  4. const { convertToPromptHistory } = require("../helpers/chat/responses");
  5. const { SlashCommandPresets } = require("../../models/slashCommandsPresets");
  6. const VALID_COMMANDS = {
  7. "/reset": resetMemory,
  8. };
  9. async function grepCommand(message, user = null) {
  10. const userPresets = await SlashCommandPresets.getUserPresets(user?.id);
  11. const availableCommands = Object.keys(VALID_COMMANDS);
  12. // Check if the message starts with any built-in command
  13. for (let i = 0; i < availableCommands.length; i++) {
  14. const cmd = availableCommands[i];
  15. const re = new RegExp(`^(${cmd})`, "i");
  16. if (re.test(message)) {
  17. return cmd;
  18. }
  19. }
  20. // Replace all preset commands with their corresponding prompts
  21. // Allows multiple commands in one message
  22. let updatedMessage = message;
  23. for (const preset of userPresets) {
  24. const regex = new RegExp(
  25. `(?:\\b\\s|^)(${preset.command})(?:\\b\\s|$)`,
  26. "g"
  27. );
  28. updatedMessage = updatedMessage.replace(regex, preset.prompt);
  29. }
  30. return updatedMessage;
  31. }
  32. async function recentChatHistory({
  33. user = null,
  34. workspace,
  35. thread = null,
  36. messageLimit = 20,
  37. apiSessionId = null,
  38. }) {
  39. const rawHistory = (
  40. await WorkspaceChats.where(
  41. {
  42. workspaceId: workspace.id,
  43. user_id: user?.id || null,
  44. thread_id: thread?.id || null,
  45. api_session_id: apiSessionId || null,
  46. include: true,
  47. },
  48. messageLimit,
  49. { id: "desc" }
  50. )
  51. ).reverse();
  52. return { rawHistory, chatHistory: convertToPromptHistory(rawHistory) };
  53. }
  54. function chatPrompt(workspace) {
  55. return (
  56. workspace?.openAiPrompt ??
  57. "Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed."
  58. );
  59. }
  60. // We use this util function to deduplicate sources from similarity searching
  61. // if the document is already pinned.
  62. // Eg: You pin a csv, if we RAG + full-text that you will get the same data
  63. // points both in the full-text and possibly from RAG - result in bad results
  64. // even if the LLM was not even going to hallucinate.
  65. function sourceIdentifier(sourceDocument) {
  66. if (!sourceDocument?.title || !sourceDocument?.published) return uuidv4();
  67. return `title:${sourceDocument.title}-timestamp:${sourceDocument.published}`;
  68. }
  69. module.exports = {
  70. sourceIdentifier,
  71. recentChatHistory,
  72. chatPrompt,
  73. grepCommand,
  74. VALID_COMMANDS,
  75. };