Skip to content

Commit 9862bc9

Browse files
chore: use $literal instead of serializing the tool calls
1 parent 562a2cb commit 9862bc9

File tree

1 file changed

+11
-48
lines changed

1 file changed

+11
-48
lines changed

tests/accuracy/sdk/accuracy-result-storage/mongodb-storage.ts

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,13 @@ import {
1313
// Omitting these as they might contain large chunk of texts
1414
const OMITTED_MODEL_RESPONSE_FIELDS: (keyof ModelResponse)[] = ["messages", "text"];
1515

16-
// The LLMToolCalls and ExpectedToolCalls are expected to have mongodb operators
17-
// nested in the objects. This interferes with the update operation that we do
18-
// on the accuracy result document to save the model responses which is why we
19-
// serialize them before saving and deserialize them on fetch.
20-
type SavedAccuracyResult = Omit<AccuracyResult, "promptResults"> & {
21-
promptResults: SavedPromptResult[];
22-
};
23-
24-
type SavedPromptResult = Omit<PromptResult, "expectedToolCalls" | "modelResponses"> & {
25-
expectedToolCalls: string;
26-
modelResponses: SavedModelResponse[];
27-
};
28-
29-
type SavedModelResponse = Omit<ModelResponse, "llmToolCalls"> & {
30-
llmToolCalls: string;
31-
};
32-
3316
export class MongoDBBasedResultStorage implements AccuracyResultStorage {
3417
private client: MongoClient;
35-
private resultCollection: Collection<SavedAccuracyResult>;
18+
private resultCollection: Collection<AccuracyResult>;
3619

3720
constructor(connectionString: string, database: string, collection: string) {
3821
this.client = new MongoClient(connectionString);
39-
this.resultCollection = this.client.db(database).collection<SavedAccuracyResult>(collection);
22+
this.resultCollection = this.client.db(database).collection<AccuracyResult>(collection);
4023
}
4124

4225
async getAccuracyResult(commitSHA: string, runId?: string): Promise<AccuracyResult | null> {
@@ -48,13 +31,11 @@ export class MongoDBBasedResultStorage implements AccuracyResultStorage {
4831
// particular commit.
4932
{ commitSHA, runStatus: AccuracyRunStatus.Done };
5033

51-
const result = await this.resultCollection.findOne(filters, {
34+
return await this.resultCollection.findOne(filters, {
5235
sort: {
5336
createdOn: -1,
5437
},
5538
});
56-
57-
return result ? this.deserializeSavedResult(result) : result;
5839
}
5940

6041
async updateRunStatus(commitSHA: string, runId: string, status: AccuracyRunStatuses): Promise<void> {
@@ -81,10 +62,8 @@ export class MongoDBBasedResultStorage implements AccuracyResultStorage {
8162
expectedToolCalls: ExpectedToolCall[];
8263
modelResponse: ModelResponse;
8364
}): Promise<void> {
84-
const expectedToolCallsToSave = JSON.stringify(expectedToolCalls);
85-
const modelResponseToSave: SavedModelResponse = {
65+
const modelResponseToSave: ModelResponse = {
8666
...modelResponse,
87-
llmToolCalls: JSON.stringify(modelResponse.llmToolCalls),
8867
};
8968

9069
for (const field of OMITTED_MODEL_RESPONSE_FIELDS) {
@@ -122,9 +101,11 @@ export class MongoDBBasedResultStorage implements AccuracyResultStorage {
122101
"$promptResults",
123102
[
124103
{
125-
prompt,
126-
expectedToolCalls: expectedToolCallsToSave,
127-
modelResponses: [modelResponseToSave],
104+
$literal: {
105+
prompt,
106+
expectedToolCalls,
107+
modelResponses: [modelResponseToSave],
108+
},
128109
},
129110
],
130111
],
@@ -138,11 +119,11 @@ export class MongoDBBasedResultStorage implements AccuracyResultStorage {
138119
{ $eq: ["$$promptResult.prompt", prompt] },
139120
{
140121
prompt: "$$promptResult.prompt",
141-
expectedToolCalls: expectedToolCallsToSave,
122+
expectedToolCalls,
142123
modelResponses: {
143124
$concatArrays: [
144125
"$$promptResult.modelResponses",
145-
[modelResponseToSave],
126+
[{ $literal: modelResponseToSave }],
146127
],
147128
},
148129
},
@@ -162,24 +143,6 @@ export class MongoDBBasedResultStorage implements AccuracyResultStorage {
162143
);
163144
}
164145

165-
private deserializeSavedResult(result: SavedAccuracyResult): AccuracyResult {
166-
return {
167-
...result,
168-
promptResults: result.promptResults.map<PromptResult>((result) => {
169-
return {
170-
...result,
171-
expectedToolCalls: JSON.parse(result.expectedToolCalls) as ExpectedToolCall[],
172-
modelResponses: result.modelResponses.map<ModelResponse>((response) => {
173-
return {
174-
...response,
175-
llmToolCalls: JSON.parse(response.llmToolCalls) as LLMToolCall[],
176-
};
177-
}),
178-
};
179-
}),
180-
};
181-
}
182-
183146
async close(): Promise<void> {
184147
await this.client.close();
185148
}

0 commit comments

Comments
 (0)