-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add distinct retrieveRelevantContent module + braintrust tracing * move retrieval conf * retrieval eval working in correct location * Add avg score * remove unused imports
- Loading branch information
Showing
6 changed files
with
216 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/chatbot-server-mongodb-public/src/processors/retrieveRelevantContent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { FindContentFunc, updateFrontMatter } from "mongodb-rag-core"; | ||
import { retrieveRelevantContent } from "./retrieveRelevantContent"; | ||
import { makeMockOpenAIToolCall } from "../test/mockOpenAi"; | ||
import { StepBackUserQueryMongoDbFunction } from "./makeStepBackUserQuery"; | ||
import { OpenAI } from "mongodb-rag-core/openai"; | ||
|
||
jest.mock("mongodb-rag-core/openai", () => | ||
makeMockOpenAIToolCall({ transformedUserQuery: "transformedUserQuery" }) | ||
); | ||
describe("retrieveRelevantContent", () => { | ||
const model = "model"; | ||
const funcRes = { | ||
transformedUserQuery: "transformedUserQuery", | ||
} satisfies StepBackUserQueryMongoDbFunction; | ||
const fakeEmbedding = [1, 2, 3]; | ||
|
||
const fakeContentBase = { | ||
embedding: fakeEmbedding, | ||
score: 1, | ||
url: "url", | ||
tokenCount: 3, | ||
sourceName: "sourceName", | ||
updated: new Date(), | ||
}; | ||
const fakeFindContent: FindContentFunc = async ({ query }) => { | ||
return { | ||
content: [ | ||
{ | ||
text: "all about " + query, | ||
...fakeContentBase, | ||
}, | ||
], | ||
queryEmbedding: fakeEmbedding, | ||
}; | ||
}; | ||
|
||
const mockToolCallOpenAi = new OpenAI({ | ||
apiKey: "apiKey", | ||
}); | ||
const argsBase = { | ||
openAiClient: mockToolCallOpenAi, | ||
model, | ||
userMessageText: "something", | ||
findContent: fakeFindContent, | ||
}; | ||
const metadataForQuery = { | ||
programmingLanguage: "javascript", | ||
mongoDbProduct: "Aggregation Framework", | ||
}; | ||
it("should return content, queryEmbedding, transformedUserQuery, searchQuery with metadata", async () => { | ||
const res = await retrieveRelevantContent({ | ||
...argsBase, | ||
metadataForQuery, | ||
}); | ||
expect(res).toEqual({ | ||
content: [ | ||
{ | ||
text: expect.any(String), | ||
...fakeContentBase, | ||
}, | ||
], | ||
queryEmbedding: fakeEmbedding, | ||
transformedUserQuery: funcRes.transformedUserQuery, | ||
searchQuery: updateFrontMatter( | ||
funcRes.transformedUserQuery, | ||
metadataForQuery | ||
), | ||
}); | ||
}); | ||
it("should return content, queryEmbedding, transformedUserQuery, searchQuery without", async () => { | ||
const res = await retrieveRelevantContent(argsBase); | ||
expect(res).toEqual({ | ||
content: [ | ||
{ | ||
text: expect.any(String), | ||
...fakeContentBase, | ||
}, | ||
], | ||
queryEmbedding: fakeEmbedding, | ||
transformedUserQuery: funcRes.transformedUserQuery, | ||
searchQuery: funcRes.transformedUserQuery, | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/chatbot-server-mongodb-public/src/processors/retrieveRelevantContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { makeStepBackUserQuery } from "./makeStepBackUserQuery"; | ||
import { FindContentFunc, Message } from "mongodb-rag-core"; | ||
import { updateFrontMatter } from "mongodb-rag-core"; | ||
import { OpenAI } from "mongodb-rag-core/openai"; | ||
|
||
export const retrieveRelevantContent = async function ({ | ||
openAiClient, | ||
model, | ||
precedingMessagesToInclude, | ||
userMessageText, | ||
metadataForQuery, | ||
findContent, | ||
}: { | ||
openAiClient: OpenAI; | ||
model: string; | ||
precedingMessagesToInclude?: Message[]; | ||
userMessageText: string; | ||
metadataForQuery?: Record<string, unknown>; | ||
findContent: FindContentFunc; | ||
}) { | ||
const { transformedUserQuery } = await makeStepBackUserQuery({ | ||
openAiClient, | ||
model, | ||
messages: precedingMessagesToInclude, | ||
userMessageText: metadataForQuery | ||
? updateFrontMatter(userMessageText, metadataForQuery) | ||
: userMessageText, | ||
}); | ||
|
||
const searchQuery = metadataForQuery | ||
? updateFrontMatter(transformedUserQuery, metadataForQuery) | ||
: transformedUserQuery; | ||
|
||
const { content, queryEmbedding } = await findContent({ | ||
query: searchQuery, | ||
}); | ||
|
||
return { content, queryEmbedding, transformedUserQuery, searchQuery }; | ||
}; |