Skip to content

Commit 50d3f44

Browse files
mongodbenBen Perlmutternlarew
authored
(EAI-990): Refactor search as a tool (#705)
* refactor GenerateRespose * Clean up imports * consolidate generate user prompt to the legacy file * update test config imports * Fix broken tests * get started * nominally working generate res w/ search * small refactors * aint pretty but fully functional * hacky if more functional * more hack * tools * functional if not pretty * Add processing * working tool calling * making progress * keepin on * Clean config * working e2e * update model version * Remove no longer used stuff * decouple search results for references and whats shown to model * fix scripts build errs * fix broken tests * update default ref links * fix broken tests * Apply suggestions from code review Co-authored-by: Nick Larew <nick.larew@mongodb.com> * revert default reference links * adding missing test --------- Co-authored-by: Ben Perlmutter <mongodben@mongodb.com> Co-authored-by: Nick Larew <nick.larew@mongodb.com>
1 parent c87dc74 commit 50d3f44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1325
-3035
lines changed

package-lock.json

Lines changed: 138 additions & 294 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/chatbot-server-mongodb-public/environments/production.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
NODE_ENV: production
1111
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT: gpt-4o-mini
1212
OPENAI_API_VERSION: "2024-06-01"
13-
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4o
13+
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4.1
1414
OPENAI_VERIFIED_ANSWER_EMBEDDING_DEPLOYMENT: "docs-chatbot-embedding-ada-002"
1515
OPENAI_RETRIEVAL_EMBEDDING_DEPLOYMENT: "text-embedding-3-small"
1616
JUDGE_LLM: "gpt-4o-mini"

packages/chatbot-server-mongodb-public/environments/staging.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
NODE_ENV: staging
1111
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT: gpt-4o-mini
1212
OPENAI_API_VERSION: "2024-06-01"
13-
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4o
13+
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4.1
1414
OPENAI_VERIFIED_ANSWER_EMBEDDING_DEPLOYMENT: "docs-chatbot-embedding-ada-002"
1515
OPENAI_RETRIEVAL_EMBEDDING_DEPLOYMENT: "text-embedding-3-small"
1616
BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME: "chatbot-responses-staging"

packages/chatbot-server-mongodb-public/src/config.ts

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
makeMongoDbVerifiedAnswerStore,
99
makeOpenAiEmbedder,
1010
makeMongoDbConversationsService,
11-
makeOpenAiChatLlm,
1211
AppConfig,
1312
CORE_ENV_VARS,
1413
assertEnvVars,
@@ -19,19 +18,25 @@ import {
1918
makeDefaultFindVerifiedAnswer,
2019
defaultCreateConversationCustomData,
2120
defaultAddMessageToConversationCustomData,
22-
makeLegacyGenerateResponse,
21+
makeGenerateResponseWithSearchTool,
2322
makeVerifiedAnswerGenerateResponse,
2423
} from "mongodb-chatbot-server";
2524
import cookieParser from "cookie-parser";
26-
import { makeStepBackRagGenerateUserPrompt } from "./processors/makeStepBackRagGenerateUserPrompt";
2725
import { blockGetRequests } from "./middleware/blockGetRequests";
2826
import { getRequestId, logRequest } from "./utils";
2927
import { systemPrompt } from "./systemPrompt";
30-
import { addReferenceSourceType } from "./processors/makeMongoDbReferences";
28+
import {
29+
addReferenceSourceType,
30+
makeMongoDbReferences,
31+
} from "./processors/makeMongoDbReferences";
3132
import { redactConnectionUri } from "./middleware/redactConnectionUri";
3233
import path from "path";
3334
import express from "express";
34-
import { wrapOpenAI, wrapTraced } from "mongodb-rag-core/braintrust";
35+
import {
36+
wrapOpenAI,
37+
wrapTraced,
38+
wrapAISDKModel,
39+
} from "mongodb-rag-core/braintrust";
3540
import { AzureOpenAI } from "mongodb-rag-core/openai";
3641
import { MongoClient } from "mongodb-rag-core/mongodb";
3742
import { TRACING_ENV_VARS } from "./EnvVars";
@@ -41,6 +46,8 @@ import {
4146
makeRateMessageUpdateTrace,
4247
} from "./tracing/routesUpdateTraceHandlers";
4348
import { useSegmentIds } from "./middleware/useSegmentIds";
49+
import { createAzure } from "mongodb-rag-core/aiSdk";
50+
import { makeSearchTool } from "./tools/search";
4451
export const {
4552
MONGODB_CONNECTION_URI,
4653
MONGODB_DATABASE_NAME,
@@ -79,19 +86,6 @@ export const openAiClient = wrapOpenAI(
7986
})
8087
);
8188

82-
export const llm = makeOpenAiChatLlm({
83-
openAiClient,
84-
deployment: OPENAI_CHAT_COMPLETION_DEPLOYMENT,
85-
openAiLmmConfigOptions: {
86-
temperature: 0,
87-
max_tokens: 1000,
88-
},
89-
});
90-
91-
llm.answerQuestionAwaited = wrapTraced(llm.answerQuestionAwaited, {
92-
name: "answerQuestionAwaited",
93-
});
94-
9589
export const embeddedContentStore = makeMongoDbEmbeddedContentStore({
9690
connectionUri: MONGODB_CONNECTION_URI,
9791
databaseName: MONGODB_DATABASE_NAME,
@@ -173,6 +167,16 @@ export const preprocessorOpenAiClient = wrapOpenAI(
173167
apiVersion: OPENAI_API_VERSION,
174168
})
175169
);
170+
export const mongodb = new MongoClient(MONGODB_CONNECTION_URI);
171+
172+
export const conversations = makeMongoDbConversationsService(
173+
mongodb.db(MONGODB_DATABASE_NAME)
174+
);
175+
const azureOpenAi = createAzure({
176+
apiKey: OPENAI_API_KEY,
177+
resourceName: process.env.OPENAI_RESOURCE_NAME,
178+
});
179+
const languageModel = wrapAISDKModel(azureOpenAi("gpt-4.1"));
176180

177181
export const generateResponse = wrapTraced(
178182
makeVerifiedAnswerGenerateResponse({
@@ -184,17 +188,24 @@ export const generateResponse = wrapTraced(
184188
};
185189
},
186190
onNoVerifiedAnswerFound: wrapTraced(
187-
makeLegacyGenerateResponse({
188-
llm,
189-
generateUserPrompt: makeStepBackRagGenerateUserPrompt({
190-
openAiClient: preprocessorOpenAiClient,
191-
model: retrievalConfig.preprocessorLlm,
192-
findContent,
193-
numPrecedingMessagesToInclude: 6,
194-
}),
191+
makeGenerateResponseWithSearchTool({
192+
languageModel,
195193
systemMessage: systemPrompt,
196-
llmNotWorkingMessage: "LLM not working. Sad!",
197-
noRelevantContentMessage: "No relevant content found. Sad!",
194+
makeReferenceLinks: makeMongoDbReferences,
195+
filterPreviousMessages: async (conversation) => {
196+
return conversation.messages.filter((message) => {
197+
return (
198+
message.role === "user" ||
199+
// Only include assistant messages that are not tool calls
200+
(message.role === "assistant" && !message.toolCall)
201+
);
202+
});
203+
},
204+
llmNotWorkingMessage:
205+
conversations.conversationConstants.LLM_NOT_WORKING,
206+
searchTool: makeSearchTool(findContent),
207+
toolChoice: "auto",
208+
maxSteps: 5,
198209
}),
199210
{ name: "makeStepBackRagGenerateUserPrompt" }
200211
),
@@ -204,12 +215,6 @@ export const generateResponse = wrapTraced(
204215
}
205216
);
206217

207-
export const mongodb = new MongoClient(MONGODB_CONNECTION_URI);
208-
209-
export const conversations = makeMongoDbConversationsService(
210-
mongodb.db(MONGODB_DATABASE_NAME)
211-
);
212-
213218
export const createConversationCustomDataWithAuthUser: AddCustomDataFunc =
214219
async (req, res) => {
215220
const customData = await defaultCreateConversationCustomData(req, res);

packages/chatbot-server-mongodb-public/src/processors/extractMongoDbMetadataFromUserMessage.eval.ts

Lines changed: 0 additions & 232 deletions
This file was deleted.

0 commit comments

Comments
 (0)