MCPcopy
hub / github.com/promptfoo/promptfoo / writeResultsToDatabase

Function writeResultsToDatabase

src/util/database.ts:47–167  ·  view source on GitHub ↗
(
  results: EvaluateSummaryV2,
  config: Partial<UnifiedConfig>,
  createdAt: Date = new Date(),
)

Source from the content-addressed store, hash-verified

45export type { StandaloneEval };
46
47export async function writeResultsToDatabase(
48 results: EvaluateSummaryV2,
49 config: Partial<UnifiedConfig>,
50 createdAt: Date = new Date(),
51): Promise<string> {
52 createdAt = createdAt || (results.timestamp ? new Date(results.timestamp) : new Date());
53 const evalId = createEvalId(createdAt);
54 const db = await getDb();
55
56 await db.transaction(async (tx) => {
57 await tx
58 .insert(evalsTable)
59 .values({
60 id: evalId,
61 createdAt: createdAt.getTime(),
62 author: getAuthor(),
63 description: config.description,
64 config,
65 results,
66 isRedteam: config.redteam !== undefined,
67 })
68 .onConflictDoNothing()
69 .run();
70
71 logger.debug(`Inserting eval ${evalId}`);
72
73 // Record prompt relation
74 invariant(results.table, 'Table is required');
75
76 for (const prompt of results.table.head.prompts) {
77 const label = prompt.label || prompt.display || prompt.raw;
78 const promptId = generateIdFromPrompt(prompt);
79
80 await tx
81 .insert(promptsTable)
82 .values({
83 id: promptId,
84 prompt: label,
85 })
86 .onConflictDoNothing()
87 .run();
88
89 await tx
90 .insert(evalsToPromptsTable)
91 .values({
92 evalId,
93 promptId,
94 })
95 .onConflictDoNothing()
96 .run();
97
98 logger.debug(`Inserting prompt ${promptId}`);
99 }
100
101 // Record dataset relation
102 const datasetId = sha256(JSON.stringify(config.tests || []));
103 const testsForStorage = Array.isArray(config.tests) ? config.tests : [];
104

Callers 3

eval.test.tsFile · 0.90
eval.tsFile · 0.90

Calls 9

createEvalIdFunction · 0.90
getDbFunction · 0.90
getAuthorFunction · 0.90
generateIdFromPromptFunction · 0.90
sha256Function · 0.90
notifyEvaluationChangedFunction · 0.90
invariantFunction · 0.85
valuesMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…