Skip to content

(EAI-990): Refactor search as a tool #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
432 changes: 138 additions & 294 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
NODE_ENV: production
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT: gpt-4o-mini
OPENAI_API_VERSION: "2024-06-01"
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4o
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4.1
OPENAI_VERIFIED_ANSWER_EMBEDDING_DEPLOYMENT: "docs-chatbot-embedding-ada-002"
OPENAI_RETRIEVAL_EMBEDDING_DEPLOYMENT: "text-embedding-3-small"
JUDGE_LLM: "gpt-4o-mini"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
NODE_ENV: staging
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT: gpt-4o-mini
OPENAI_API_VERSION: "2024-06-01"
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4o
OPENAI_CHAT_COMPLETION_DEPLOYMENT: gpt-4.1
OPENAI_VERIFIED_ANSWER_EMBEDDING_DEPLOYMENT: "docs-chatbot-embedding-ada-002"
OPENAI_RETRIEVAL_EMBEDDING_DEPLOYMENT: "text-embedding-3-small"
BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME: "chatbot-responses-staging"
Expand Down
73 changes: 39 additions & 34 deletions packages/chatbot-server-mongodb-public/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
makeMongoDbVerifiedAnswerStore,
makeOpenAiEmbedder,
makeMongoDbConversationsService,
makeOpenAiChatLlm,
AppConfig,
CORE_ENV_VARS,
assertEnvVars,
Expand All @@ -19,19 +18,25 @@ import {
makeDefaultFindVerifiedAnswer,
defaultCreateConversationCustomData,
defaultAddMessageToConversationCustomData,
makeLegacyGenerateResponse,
makeGenerateResponseWithSearchTool,
makeVerifiedAnswerGenerateResponse,
} from "mongodb-chatbot-server";
import cookieParser from "cookie-parser";
import { makeStepBackRagGenerateUserPrompt } from "./processors/makeStepBackRagGenerateUserPrompt";
import { blockGetRequests } from "./middleware/blockGetRequests";
import { getRequestId, logRequest } from "./utils";
import { systemPrompt } from "./systemPrompt";
import { addReferenceSourceType } from "./processors/makeMongoDbReferences";
import {
addReferenceSourceType,
makeMongoDbReferences,
} from "./processors/makeMongoDbReferences";
import { redactConnectionUri } from "./middleware/redactConnectionUri";
import path from "path";
import express from "express";
import { wrapOpenAI, wrapTraced } from "mongodb-rag-core/braintrust";
import {
wrapOpenAI,
wrapTraced,
wrapAISDKModel,
} from "mongodb-rag-core/braintrust";
import { AzureOpenAI } from "mongodb-rag-core/openai";
import { MongoClient } from "mongodb-rag-core/mongodb";
import { TRACING_ENV_VARS } from "./EnvVars";
Expand All @@ -41,6 +46,8 @@ import {
makeRateMessageUpdateTrace,
} from "./tracing/routesUpdateTraceHandlers";
import { useSegmentIds } from "./middleware/useSegmentIds";
import { createAzure } from "mongodb-rag-core/aiSdk";
import { makeSearchTool } from "./tools/search";
export const {
MONGODB_CONNECTION_URI,
MONGODB_DATABASE_NAME,
Expand Down Expand Up @@ -79,19 +86,6 @@ export const openAiClient = wrapOpenAI(
})
);

export const llm = makeOpenAiChatLlm({
openAiClient,
deployment: OPENAI_CHAT_COMPLETION_DEPLOYMENT,
openAiLmmConfigOptions: {
temperature: 0,
max_tokens: 1000,
},
});

llm.answerQuestionAwaited = wrapTraced(llm.answerQuestionAwaited, {
name: "answerQuestionAwaited",
});

export const embeddedContentStore = makeMongoDbEmbeddedContentStore({
connectionUri: MONGODB_CONNECTION_URI,
databaseName: MONGODB_DATABASE_NAME,
Expand Down Expand Up @@ -173,6 +167,16 @@ export const preprocessorOpenAiClient = wrapOpenAI(
apiVersion: OPENAI_API_VERSION,
})
);
export const mongodb = new MongoClient(MONGODB_CONNECTION_URI);

export const conversations = makeMongoDbConversationsService(
mongodb.db(MONGODB_DATABASE_NAME)
);
const azureOpenAi = createAzure({
apiKey: OPENAI_API_KEY,
resourceName: process.env.OPENAI_RESOURCE_NAME,
});
const languageModel = wrapAISDKModel(azureOpenAi("gpt-4.1"));

export const generateResponse = wrapTraced(
makeVerifiedAnswerGenerateResponse({
Expand All @@ -184,17 +188,24 @@ export const generateResponse = wrapTraced(
};
},
onNoVerifiedAnswerFound: wrapTraced(
makeLegacyGenerateResponse({
llm,
generateUserPrompt: makeStepBackRagGenerateUserPrompt({
openAiClient: preprocessorOpenAiClient,
model: retrievalConfig.preprocessorLlm,
findContent,
numPrecedingMessagesToInclude: 6,
}),
makeGenerateResponseWithSearchTool({
languageModel,
systemMessage: systemPrompt,
llmNotWorkingMessage: "LLM not working. Sad!",
noRelevantContentMessage: "No relevant content found. Sad!",
makeReferenceLinks: makeMongoDbReferences,
filterPreviousMessages: async (conversation) => {
return conversation.messages.filter((message) => {
return (
message.role === "user" ||
// Only include assistant messages that are not tool calls
(message.role === "assistant" && !message.toolCall)
);
});
},
llmNotWorkingMessage:
conversations.conversationConstants.LLM_NOT_WORKING,
searchTool: makeSearchTool(findContent),
toolChoice: "auto",
maxSteps: 5,
}),
{ name: "makeStepBackRagGenerateUserPrompt" }
),
Expand All @@ -204,12 +215,6 @@ export const generateResponse = wrapTraced(
}
);

export const mongodb = new MongoClient(MONGODB_CONNECTION_URI);

export const conversations = makeMongoDbConversationsService(
mongodb.db(MONGODB_DATABASE_NAME)
);

export const createConversationCustomDataWithAuthUser: AddCustomDataFunc =
async (req, res) => {
const customData = await defaultCreateConversationCustomData(req, res);
Expand Down

This file was deleted.

Loading