Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

database.ts 4.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  1. import path from 'node:path';
  2. import { relations, sql } from 'drizzle-orm';
  3. import { text, integer, real, sqliteTable, primaryKey } from 'drizzle-orm/sqlite-core';
  4. import { drizzle } from 'drizzle-orm/better-sqlite3';
  5. import Database from 'better-sqlite3';
  6. import { getConfigDirectoryPath } from './util';
  7. import type { EvaluateSummary, UnifiedConfig } from './types';
  8. // ------------ Prompts ------------
  9. export const prompts = sqliteTable('prompts', {
  10. id: text('id').primaryKey(),
  11. createdAt: integer('created_at')
  12. .notNull()
  13. .default(sql`CURRENT_TIMESTAMP`),
  14. prompt: text('prompt').notNull(),
  15. });
  16. export const promptsRelations = relations(prompts, ({ many }) => ({
  17. evalsToPrompts: many(evalsToPrompts),
  18. }));
  19. // ------------ Datasets ------------
  20. export const datasets = sqliteTable('datasets', {
  21. id: text('id').primaryKey(),
  22. tests: text('tests', { mode: 'json' }).$type<UnifiedConfig['tests']>(),
  23. createdAt: integer('created_at')
  24. .notNull()
  25. .default(sql`CURRENT_TIMESTAMP`),
  26. });
  27. export const datasetsRelations = relations(datasets, ({ many }) => ({
  28. evalsToDatasets: many(evalsToDatasets),
  29. }));
  30. // ------------ Evals ------------
  31. export const evals = sqliteTable('evals', {
  32. id: text('id').primaryKey(),
  33. createdAt: integer('created_at')
  34. .notNull()
  35. .default(sql`CURRENT_TIMESTAMP`),
  36. description: text('description'),
  37. results: text('results', { mode: 'json' }).$type<EvaluateSummary>().notNull(),
  38. config: text('config', { mode: 'json' }).$type<Partial<UnifiedConfig>>().notNull(),
  39. });
  40. export const evalsRelations = relations(evals, ({ many }) => ({
  41. evalsToPrompts: many(evalsToPrompts),
  42. evalsToDatasets: many(evalsToDatasets),
  43. }));
  44. export const evalsToPrompts = sqliteTable(
  45. 'evals_to_prompts',
  46. {
  47. evalId: text('eval_id')
  48. .notNull()
  49. .references(() => evals.id),
  50. // Drizzle doesn't support this migration for sqlite, so we remove foreign keys manually.
  51. //.references(() => evals.id, { onDelete: 'cascade' }),
  52. promptId: text('prompt_id')
  53. .notNull()
  54. .references(() => prompts.id),
  55. },
  56. (t) => ({
  57. pk: primaryKey({ columns: [t.evalId, t.promptId] }),
  58. }),
  59. );
  60. export const evalsToPromptsRelations = relations(evalsToPrompts, ({ one }) => ({
  61. eval: one(evals, {
  62. fields: [evalsToPrompts.evalId],
  63. references: [evals.id],
  64. }),
  65. prompt: one(prompts, {
  66. fields: [evalsToPrompts.promptId],
  67. references: [prompts.id],
  68. }),
  69. }));
  70. export const evalsToDatasets = sqliteTable(
  71. 'evals_to_datasets',
  72. {
  73. evalId: text('eval_id')
  74. .notNull()
  75. .references(() => evals.id),
  76. // Drizzle doesn't support this migration for sqlite, so we remove foreign keys manually.
  77. //.references(() => evals.id, { onDelete: 'cascade' }),
  78. datasetId: text('dataset_id')
  79. .notNull()
  80. .references(() => datasets.id),
  81. },
  82. (t) => ({
  83. pk: primaryKey({ columns: [t.evalId, t.datasetId] }),
  84. }),
  85. );
  86. export const evalsToDatasetsRelations = relations(evalsToDatasets, ({ one }) => ({
  87. eval: one(evals, {
  88. fields: [evalsToDatasets.evalId],
  89. references: [evals.id],
  90. }),
  91. dataset: one(datasets, {
  92. fields: [evalsToDatasets.datasetId],
  93. references: [datasets.id],
  94. }),
  95. }));
  96. // ------------ Outputs ------------
  97. // We're just recording these on eval.results for now...
  98. /*
  99. export const llmOutputs = sqliteTable(
  100. 'llm_outputs',
  101. {
  102. id: text('id')
  103. .notNull()
  104. .unique(),
  105. createdAt: integer('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
  106. evalId: text('eval_id')
  107. .notNull()
  108. .references(() => evals.id),
  109. promptId: text('prompt_id')
  110. .notNull()
  111. .references(() => prompts.id),
  112. providerId: text('provider_id').notNull(),
  113. vars: text('vars', {mode: 'json'}),
  114. response: text('response', {mode: 'json'}),
  115. error: text('error'),
  116. latencyMs: integer('latency_ms'),
  117. gradingResult: text('grading_result', {mode: 'json'}),
  118. namedScores: text('named_scores', {mode: 'json'}),
  119. cost: real('cost'),
  120. },
  121. (t) => ({
  122. pk: primaryKey({ columns: [t.id] }),
  123. }),
  124. );
  125. export const llmOutputsRelations = relations(llmOutputs, ({ one }) => ({
  126. eval: one(evals, {
  127. fields: [llmOutputs.evalId],
  128. references: [evals.id],
  129. }),
  130. prompt: one(prompts, {
  131. fields: [llmOutputs.promptId],
  132. references: [prompts.id],
  133. }),
  134. }));
  135. */
  136. let dbInstance: ReturnType<typeof drizzle> | null = null;
  137. export function getDbPath() {
  138. return path.resolve(getConfigDirectoryPath(), 'promptfoo.db');
  139. }
  140. export function getDbSignalPath() {
  141. return path.resolve(getConfigDirectoryPath(), 'evalLastWritten');
  142. }
  143. export function getDb() {
  144. if (!dbInstance) {
  145. const sqlite = new Database(getDbPath());
  146. dbInstance = drizzle(sqlite);
  147. }
  148. return dbInstance;
  149. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...