@lobehub/chat 1.15.6 → 1.15.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +6 -6
  3. package/README.zh-CN.md +6 -6
  4. package/docs/self-hosting/server-database/docker-compose.mdx +2 -2
  5. package/docs/self-hosting/server-database/docker-compose.zh-CN.mdx +2 -2
  6. package/locales/ar/knowledgeBase.json +1 -0
  7. package/locales/ar/ragEval.json +91 -0
  8. package/locales/bg-BG/knowledgeBase.json +1 -0
  9. package/locales/bg-BG/ragEval.json +91 -0
  10. package/locales/de-DE/knowledgeBase.json +1 -0
  11. package/locales/de-DE/ragEval.json +91 -0
  12. package/locales/en-US/knowledgeBase.json +1 -0
  13. package/locales/en-US/ragEval.json +91 -0
  14. package/locales/es-ES/knowledgeBase.json +1 -0
  15. package/locales/es-ES/ragEval.json +91 -0
  16. package/locales/fr-FR/knowledgeBase.json +1 -0
  17. package/locales/fr-FR/ragEval.json +91 -0
  18. package/locales/it-IT/knowledgeBase.json +1 -0
  19. package/locales/it-IT/ragEval.json +91 -0
  20. package/locales/ja-JP/knowledgeBase.json +1 -0
  21. package/locales/ja-JP/ragEval.json +91 -0
  22. package/locales/ko-KR/knowledgeBase.json +1 -0
  23. package/locales/ko-KR/ragEval.json +91 -0
  24. package/locales/nl-NL/knowledgeBase.json +1 -0
  25. package/locales/nl-NL/ragEval.json +91 -0
  26. package/locales/pl-PL/knowledgeBase.json +1 -0
  27. package/locales/pl-PL/ragEval.json +91 -0
  28. package/locales/pt-BR/knowledgeBase.json +1 -0
  29. package/locales/pt-BR/ragEval.json +91 -0
  30. package/locales/ru-RU/knowledgeBase.json +1 -0
  31. package/locales/ru-RU/ragEval.json +91 -0
  32. package/locales/tr-TR/knowledgeBase.json +1 -0
  33. package/locales/tr-TR/ragEval.json +91 -0
  34. package/locales/vi-VN/knowledgeBase.json +1 -0
  35. package/locales/vi-VN/ragEval.json +91 -0
  36. package/locales/zh-CN/knowledgeBase.json +1 -0
  37. package/locales/zh-CN/ragEval.json +91 -0
  38. package/locales/zh-TW/knowledgeBase.json +1 -0
  39. package/locales/zh-TW/ragEval.json +91 -0
  40. package/package.json +2 -1
  41. package/src/app/(main)/repos/[id]/@menu/Head/index.tsx +4 -13
  42. package/src/app/(main)/repos/[id]/@menu/Menu/index.tsx +30 -21
  43. package/src/app/(main)/repos/[id]/@menu/default.tsx +8 -2
  44. package/src/app/(main)/repos/[id]/evals/components/Container.tsx +25 -0
  45. package/src/app/(main)/repos/[id]/evals/components/Tabs.tsx +35 -0
  46. package/src/app/(main)/repos/[id]/evals/dataset/CreateDataset/CreateForm.tsx +72 -0
  47. package/src/app/(main)/repos/[id]/evals/dataset/CreateDataset/index.tsx +37 -0
  48. package/src/app/(main)/repos/[id]/evals/dataset/DatasetDetail/index.tsx +126 -0
  49. package/src/app/(main)/repos/[id]/evals/dataset/DatasetList/Item.tsx +59 -0
  50. package/src/app/(main)/repos/[id]/evals/dataset/DatasetList/index.tsx +32 -0
  51. package/src/app/(main)/repos/[id]/evals/dataset/EmptyGuide/index.tsx +33 -0
  52. package/src/app/(main)/repos/[id]/evals/dataset/page.tsx +47 -0
  53. package/src/app/(main)/repos/[id]/evals/evaluation/CreateEvaluation/CreateForm.tsx +93 -0
  54. package/src/app/(main)/repos/[id]/evals/evaluation/CreateEvaluation/index.tsx +28 -0
  55. package/src/app/(main)/repos/[id]/evals/evaluation/CreateEvaluation/useModal.tsx +39 -0
  56. package/src/app/(main)/repos/[id]/evals/evaluation/EmptyGuide/index.tsx +25 -0
  57. package/src/app/(main)/repos/[id]/evals/evaluation/EvaluationList/index.tsx +209 -0
  58. package/src/app/(main)/repos/[id]/evals/evaluation/page.tsx +32 -0
  59. package/src/app/(main)/repos/[id]/evals/layout.tsx +22 -0
  60. package/src/app/(main)/repos/[id]/evals/page.tsx +9 -0
  61. package/src/app/(main)/repos/[id]/evals/type.ts +5 -0
  62. package/src/app/(main)/repos/[id]/not-found.tsx +3 -0
  63. package/src/app/(main)/settings/llm/components/ProviderConfig/index.tsx +2 -2
  64. package/src/components/FileIcon/index.tsx +2 -2
  65. package/src/config/featureFlags/schema.ts +3 -1
  66. package/src/database/server/migrations/0008_add_rag_evals.sql +120 -0
  67. package/src/database/server/migrations/meta/0008_snapshot.json +3463 -0
  68. package/src/database/server/migrations/meta/_journal.json +7 -0
  69. package/src/database/server/models/file.ts +11 -2
  70. package/src/database/server/models/ragEval/dataset.ts +59 -0
  71. package/src/database/server/models/ragEval/datasetRecord.ts +87 -0
  72. package/src/database/server/models/ragEval/evaluation.ts +96 -0
  73. package/src/database/server/models/ragEval/evaluationRecord.ts +64 -0
  74. package/src/database/server/models/ragEval/index.ts +4 -0
  75. package/src/database/server/schemas/lobechat/asyncTask.ts +24 -0
  76. package/src/database/server/schemas/lobechat/file.ts +2 -18
  77. package/src/database/server/schemas/lobechat/index.ts +2 -0
  78. package/src/database/server/schemas/lobechat/ragEvals.ts +105 -0
  79. package/src/database/server/schemas/lobechat/relations.ts +2 -1
  80. package/src/libs/agent-runtime/types/chat.ts +3 -0
  81. package/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.ts +3 -1
  82. package/src/libs/langchain/loaders/index.ts +1 -1
  83. package/src/locales/default/index.ts +2 -0
  84. package/src/locales/default/knowledgeBase.ts +1 -0
  85. package/src/locales/default/ragEval.ts +93 -0
  86. package/src/server/modules/S3/index.ts +11 -0
  87. package/src/server/routers/async/index.ts +2 -0
  88. package/src/server/routers/async/ragEval.ts +138 -0
  89. package/src/server/routers/lambda/index.ts +2 -1
  90. package/src/server/routers/lambda/ragEval.ts +296 -0
  91. package/src/services/ragEval.ts +67 -0
  92. package/src/services/upload.ts +11 -4
  93. package/src/store/file/slices/upload/action.ts +8 -6
  94. package/src/store/knowledgeBase/initialState.ts +3 -1
  95. package/src/store/knowledgeBase/slices/ragEval/actions/dataset.ts +88 -0
  96. package/src/store/knowledgeBase/slices/ragEval/actions/evaluation.ts +62 -0
  97. package/src/store/knowledgeBase/slices/ragEval/actions/index.ts +20 -0
  98. package/src/store/knowledgeBase/slices/ragEval/index.ts +2 -0
  99. package/src/store/knowledgeBase/slices/ragEval/initialState.ts +7 -0
  100. package/src/store/knowledgeBase/store.ts +9 -3
  101. package/src/store/serverConfig/selectors.test.ts +1 -0
  102. package/src/types/eval/dataset.ts +47 -0
  103. package/src/types/eval/evaluation.ts +53 -0
  104. package/src/types/eval/index.ts +3 -0
  105. package/src/types/eval/ragas.ts +9 -0
@@ -106,12 +106,14 @@ export const createFileUploadSlice: StateCreator<
106
106
  });
107
107
  } else {
108
108
  // 2. if file don't exist, need upload files
109
- metadata = await uploadService.uploadWithProgress(file, (status, upload) => {
110
- onStatusUpdate?.({
111
- id: file.name,
112
- type: 'updateFile',
113
- value: { status: status === 'success' ? 'processing' : status, uploadState: upload },
114
- });
109
+ metadata = await uploadService.uploadWithProgress(file, {
110
+ onProgress: (status, upload) => {
111
+ onStatusUpdate?.({
112
+ id: file.name,
113
+ type: 'updateFile',
114
+ value: { status: status === 'success' ? 'processing' : status, uploadState: upload },
115
+ });
116
+ },
115
117
  });
116
118
  }
117
119
 
@@ -1,7 +1,9 @@
1
1
  import { KnowledgeBaseState, initialKnowledgeBaseState } from '../knowledgeBase/slices/crud';
2
+ import { RAGEvalState, initialDatasetState } from '../knowledgeBase/slices/ragEval';
2
3
 
3
- export type KnowledgeBaseStoreState = KnowledgeBaseState;
4
+ export type KnowledgeBaseStoreState = KnowledgeBaseState & RAGEvalState;
4
5
 
5
6
  export const initialState: KnowledgeBaseStoreState = {
6
7
  ...initialKnowledgeBaseState,
8
+ ...initialDatasetState,
7
9
  };
@@ -0,0 +1,88 @@
1
+ import { SWRResponse, mutate } from 'swr';
2
+ import { StateCreator } from 'zustand/vanilla';
3
+
4
+ import { notification } from '@/components/AntdStaticMethods';
5
+ import { useClientDataSWR } from '@/libs/swr';
6
+ import { ragEvalService } from '@/services/ragEval';
7
+ import { KnowledgeBaseStore } from '@/store/knowledgeBase/store';
8
+ import {
9
+ CreateNewEvalDatasets,
10
+ EvalDatasetRecord,
11
+ RAGEvalDataSetItem,
12
+ insertEvalDatasetRecordSchema,
13
+ } from '@/types/eval';
14
+
15
+ const FETCH_DATASET_LIST_KEY = 'FETCH_DATASET_LIST';
16
+ const FETCH_DATASET_RECORD_KEY = 'FETCH_DATASET_RECORD_KEY';
17
+
18
+ export interface RAGEvalDatasetAction {
19
+ createNewDataset: (params: CreateNewEvalDatasets) => Promise<void>;
20
+
21
+ importDataset: (file: File, datasetId: number) => Promise<void>;
22
+ refreshDatasetList: () => Promise<void>;
23
+ removeDataset: (id: number) => Promise<void>;
24
+ useFetchDatasetRecords: (datasetId: number | null) => SWRResponse<EvalDatasetRecord[]>;
25
+ useFetchDatasets: (knowledgeBaseId: string) => SWRResponse<RAGEvalDataSetItem[]>;
26
+ }
27
+
28
+ export const createRagEvalDatasetSlice: StateCreator<
29
+ KnowledgeBaseStore,
30
+ [['zustand/devtools', never]],
31
+ [],
32
+ RAGEvalDatasetAction
33
+ > = (set, get) => ({
34
+ createNewDataset: async (params) => {
35
+ await ragEvalService.createDataset(params);
36
+ await get().refreshDatasetList();
37
+ },
38
+
39
+ importDataset: async (file, datasetId) => {
40
+ if (!datasetId) return;
41
+ const fileType = file.name.split('.').pop();
42
+
43
+ if (fileType === 'jsonl') {
44
+ // jsonl 文件 需要拆分成单个条,然后逐一校验格式
45
+ const jsonl = await file.text();
46
+ const { default: JSONL } = await import('jsonl-parse-stringify');
47
+
48
+ try {
49
+ const items = JSONL.parse(jsonl);
50
+
51
+ // check if the items are valid
52
+ insertEvalDatasetRecordSchema.array().parse(items);
53
+
54
+ // if valid, send to backend
55
+ await ragEvalService.importDatasetRecords(datasetId, file);
56
+ } catch (e) {
57
+ notification.error({ description: (e as Error).message, message: '文件格式错误' });
58
+ }
59
+ }
60
+
61
+ await get().refreshDatasetList();
62
+ },
63
+ refreshDatasetList: async () => {
64
+ await mutate(FETCH_DATASET_LIST_KEY);
65
+ },
66
+
67
+ removeDataset: async (id) => {
68
+ await ragEvalService.removeDataset(id);
69
+ await get().refreshDatasetList();
70
+ },
71
+ useFetchDatasetRecords: (datasetId) =>
72
+ useClientDataSWR<EvalDatasetRecord[]>(
73
+ !!datasetId ? [FETCH_DATASET_RECORD_KEY, datasetId] : null,
74
+ () => ragEvalService.getDatasetRecords(datasetId!),
75
+ ),
76
+ useFetchDatasets: (knowledgeBaseId) =>
77
+ useClientDataSWR<RAGEvalDataSetItem[]>(
78
+ [FETCH_DATASET_LIST_KEY, knowledgeBaseId],
79
+ () => ragEvalService.getDatasets(knowledgeBaseId),
80
+ {
81
+ fallbackData: [],
82
+ onSuccess: () => {
83
+ if (!get().initDatasetList)
84
+ set({ initDatasetList: true }, false, 'useFetchDatasets/init');
85
+ },
86
+ },
87
+ ),
88
+ });
@@ -0,0 +1,62 @@
1
+ import { SWRResponse, mutate } from 'swr';
2
+ import { StateCreator } from 'zustand/vanilla';
3
+
4
+ import { useClientDataSWR } from '@/libs/swr';
5
+ import { ragEvalService } from '@/services/ragEval';
6
+ import { KnowledgeBaseStore } from '@/store/knowledgeBase/store';
7
+ import { CreateNewEvalEvaluation, RAGEvalDataSetItem } from '@/types/eval';
8
+
9
+ const FETCH_EVALUATION_LIST_KEY = 'FETCH_EVALUATION_LIST_KEY';
10
+
11
+ export interface RAGEvalEvaluationAction {
12
+ checkEvaluationStatus: (id: number) => Promise<void>;
13
+
14
+ createNewEvaluation: (params: CreateNewEvalEvaluation) => Promise<void>;
15
+ refreshEvaluationList: () => Promise<void>;
16
+
17
+ removeEvaluation: (id: number) => Promise<void>;
18
+ runEvaluation: (id: number) => Promise<void>;
19
+
20
+ useFetchEvaluationList: (knowledgeBaseId: string) => SWRResponse<RAGEvalDataSetItem[]>;
21
+ }
22
+
23
+ export const createRagEvalEvaluationSlice: StateCreator<
24
+ KnowledgeBaseStore,
25
+ [['zustand/devtools', never]],
26
+ [],
27
+ RAGEvalEvaluationAction
28
+ > = (set, get) => ({
29
+ checkEvaluationStatus: async (id) => {
30
+ await ragEvalService.checkEvaluationStatus(id);
31
+ },
32
+
33
+ createNewEvaluation: async (params) => {
34
+ await ragEvalService.createEvaluation(params);
35
+ await get().refreshEvaluationList();
36
+ },
37
+ refreshEvaluationList: async () => {
38
+ await mutate(FETCH_EVALUATION_LIST_KEY);
39
+ },
40
+
41
+ removeEvaluation: async (id) => {
42
+ await ragEvalService.removeEvaluation(id);
43
+ // await get().refreshEvaluationList();
44
+ },
45
+
46
+ runEvaluation: async (id) => {
47
+ await ragEvalService.startEvaluationTask(id);
48
+ },
49
+
50
+ useFetchEvaluationList: (knowledgeBaseId) =>
51
+ useClientDataSWR<RAGEvalDataSetItem[]>(
52
+ [FETCH_EVALUATION_LIST_KEY, knowledgeBaseId],
53
+ () => ragEvalService.getEvaluationList(knowledgeBaseId),
54
+ {
55
+ fallbackData: [],
56
+ onSuccess: () => {
57
+ if (!get().initDatasetList)
58
+ set({ initDatasetList: true }, false, 'useFetchDatasets/init');
59
+ },
60
+ },
61
+ ),
62
+ });
@@ -0,0 +1,20 @@
1
+ import { StateCreator } from 'zustand/vanilla';
2
+
3
+ import { KnowledgeBaseStore } from '@/store/knowledgeBase/store';
4
+
5
+ import { RAGEvalDatasetAction, createRagEvalDatasetSlice } from './dataset';
6
+ import { RAGEvalEvaluationAction, createRagEvalEvaluationSlice } from './evaluation';
7
+
8
+ export interface RAGEvalAction extends RAGEvalDatasetAction, RAGEvalEvaluationAction {
9
+ // empty
10
+ }
11
+
12
+ export const createRagEvalSlice: StateCreator<
13
+ KnowledgeBaseStore,
14
+ [['zustand/devtools', never]],
15
+ [],
16
+ RAGEvalAction
17
+ > = (...params) => ({
18
+ ...createRagEvalDatasetSlice(...params),
19
+ ...createRagEvalEvaluationSlice(...params),
20
+ });
@@ -0,0 +1,2 @@
1
+ export * from './actions';
2
+ export * from './initialState';
@@ -0,0 +1,7 @@
1
+ export interface RAGEvalState {
2
+ initDatasetList: boolean;
3
+ }
4
+
5
+ export const initialDatasetState: RAGEvalState = {
6
+ initDatasetList: false,
7
+ };
@@ -6,12 +6,17 @@ import { createDevtools } from '../middleware/createDevtools';
6
6
  import { KnowledgeBaseStoreState, initialState } from './initialState';
7
7
  import { KnowledgeBaseContentAction, createContentSlice } from './slices/content';
8
8
  import { KnowledgeBaseCrudAction, createCrudSlice } from './slices/crud';
9
+ import { RAGEvalAction, createRagEvalSlice } from './slices/ragEval';
9
10
 
10
11
  // =============== 聚合 createStoreFn ============ //
11
12
 
12
- export type KnowledgeBaseStore = KnowledgeBaseStoreState &
13
- KnowledgeBaseCrudAction &
14
- KnowledgeBaseContentAction;
13
+ export interface KnowledgeBaseStore
14
+ extends KnowledgeBaseStoreState,
15
+ KnowledgeBaseCrudAction,
16
+ KnowledgeBaseContentAction,
17
+ RAGEvalAction {
18
+ // empty
19
+ }
15
20
 
16
21
  const createStore: StateCreator<KnowledgeBaseStore, [['zustand/devtools', never]]> = (
17
22
  ...parameters
@@ -19,6 +24,7 @@ const createStore: StateCreator<KnowledgeBaseStore, [['zustand/devtools', never]
19
24
  ...initialState,
20
25
  ...createCrudSlice(...parameters),
21
26
  ...createContentSlice(...parameters),
27
+ ...createRagEvalSlice(...parameters),
22
28
  });
23
29
 
24
30
  // =============== 实装 useStore ============ //
@@ -20,6 +20,7 @@ describe('featureFlagsSelectors', () => {
20
20
  enableWebrtc: false,
21
21
  isAgentEditable: false,
22
22
  showCreateSession: true,
23
+ enableRAGEval: false,
23
24
  showDalle: true,
24
25
  enableKnowledgeBase: true,
25
26
  showLLM: false,
@@ -0,0 +1,47 @@
1
+ import { z } from 'zod';
2
+
3
+ export interface EvalDatasetRecordRefFile {
4
+ fileType: string;
5
+ id: string;
6
+ name: string;
7
+ }
8
+ export interface EvalDatasetRecord {
9
+ createdAt: Date;
10
+ id: number;
11
+ ideal?: string | null;
12
+
13
+ metadata: any;
14
+
15
+ question?: string | null;
16
+
17
+ /**
18
+ * The reference files for the question
19
+ */
20
+ referenceFiles?: EvalDatasetRecordRefFile[] | null;
21
+ }
22
+
23
+ export const insertEvalDatasetRecordSchema = z.object({
24
+ ideal: z.string().optional(),
25
+
26
+ question: z.string(),
27
+
28
+ referenceFiles: z.string().or(z.array(z.string())).optional(),
29
+ });
30
+
31
+ export type InsertEvalDatasetRecord = z.infer<typeof insertEvalDatasetRecordSchema>;
32
+
33
+ export interface RAGEvalDataSetItem {
34
+ createdAt: Date;
35
+ description?: string | null;
36
+ id: number;
37
+ name: string;
38
+ updatedAt: Date;
39
+ }
40
+
41
+ export const insertEvalDatasetsSchema = z.object({
42
+ description: z.string().optional(),
43
+ knowledgeBaseId: z.string(),
44
+ name: z.string(),
45
+ });
46
+
47
+ export type CreateNewEvalDatasets = z.infer<typeof insertEvalDatasetsSchema>;
@@ -0,0 +1,53 @@
1
+ import { z } from 'zod';
2
+
3
+ export enum EvalEvaluationStatus {
4
+ Error = 'Error',
5
+ Pending = 'Pending',
6
+ Processing = 'Processing',
7
+ Success = 'Success',
8
+ }
9
+
10
+ export interface EvaluationRecord {
11
+ answer: string;
12
+ context: string[];
13
+ createdAt: Date;
14
+ id: number;
15
+ ideal: string;
16
+ question: string;
17
+ }
18
+
19
+ export const insertEvaluationSchema = z.object({
20
+ ideal: z.string().optional(),
21
+
22
+ question: z.string(),
23
+
24
+ referenceFiles: z.string().or(z.array(z.string())).optional(),
25
+ });
26
+
27
+ export type InsertEvaluationRecord = z.infer<typeof insertEvaluationSchema>;
28
+
29
+ export interface RAGEvalEvaluationItem {
30
+ createdAt: Date;
31
+ dataset: {
32
+ id: number;
33
+ name: string;
34
+ };
35
+ evalRecordsUrl?: string;
36
+ id: number;
37
+ name: string;
38
+ recordsStats: {
39
+ success: number;
40
+ total: number;
41
+ };
42
+ status: EvalEvaluationStatus;
43
+ updatedAt: Date;
44
+ }
45
+
46
+ export const insertEvalEvaluationSchema = z.object({
47
+ datasetId: z.number(),
48
+ description: z.string().optional(),
49
+ knowledgeBaseId: z.string(),
50
+ name: z.string(),
51
+ });
52
+
53
+ export type CreateNewEvalEvaluation = z.infer<typeof insertEvalEvaluationSchema>;
@@ -0,0 +1,3 @@
1
+ export * from './dataset';
2
+ export * from './evaluation';
3
+ export * from './ragas';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The data set for the RAGAS benchmark
3
+ */
4
+ export interface RAGASDataSetItem {
5
+ answer: string;
6
+ context: string;
7
+ ground_truth: string;
8
+ question: string;
9
+ }