Skip to content

(EAI-950) Segment - Send data feed to Segment #679

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 11 commits into from
Apr 22, 2025
98 changes: 97 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions packages/chatbot-server-mongodb-public/.env.example
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
PORT=3000
OPENAI_ENDPOINT=https://<resource_name>.openai.azure.com/
OPENAI_API_KEY=<api_key>
OPENAI_ENDPOINT="https://<resource_name>.openai.azure.com/"
OPENAI_API_KEY="<api_key>"
OPENAI_VERIFIED_ANSWER_EMBEDDING_DEPLOYMENT="docs-chatbot-embedding-ada-002"
OPENAI_RETRIEVAL_EMBEDDING_DEPLOYMENT="text-embedding-3-small"
OPENAI_EMBEDDING_MODEL=text-embedding-ada-002
OPENAI_EMBEDDING_MODEL_VERSION=2023-03-15-preview
OPENAI_CHAT_COMPLETION_DEPLOYMENT=<deployment_name>
OPENAI_CHAT_COMPLETION_MODEL_VERSION=2023-05-15
MONGODB_CONNECTION_URI=<connection_uri>
MONGODB_DATABASE_NAME=<docs-chatbot-ENV>
VECTOR_SEARCH_INDEX_NAME=<index_name>
NODE_ENV=development
ALLOWED_ORIGINS=http://example.com,http://localhost:5173
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT=<deployment name>
OPENAI_EMBEDDING_MODEL="text-embedding-ada-002"
OPENAI_EMBEDDING_MODEL_VERSION="2023-03-15-preview"
OPENAI_CHAT_COMPLETION_DEPLOYMENT="<deployment_name>"
OPENAI_CHAT_COMPLETION_MODEL_VERSION="2023-05-15"
MONGODB_CONNECTION_URI="<connection_uri>"
MONGODB_DATABASE_NAME="<docs-chatbot-ENV>"
VECTOR_SEARCH_INDEX_NAME="<index_name>"
NODE_ENV="development"
ALLOWED_ORIGINS="http://example.com,http://localhost:5173"
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT="<deployment name>"
OPENAI_API_VERSION="2024-06-01"
JUDGE_EMBEDDING_MODEL="text-embedding-3-small"
JUDGE_LLM="gpt-4o-mini"
BRAINTRUST_TRACING_API_KEY="<some api key>"
BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME="chatbot-responses-dev"
SEGMENT_WRITE_KEY="<your segment write key>"
3 changes: 3 additions & 0 deletions packages/chatbot-server-mongodb-public/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"generate-eval-cases": "ts-node src/eval/bin/generateEvalCasesYamlFromCSV.ts"
},
"dependencies": {
"@segment/analytics-node": "^2.2.1",
"@slack/web-api": "^7.8.0",
"common-tags": "^1.8.2",
"cookie-parser": "^1.4.6",
Expand All @@ -49,6 +50,7 @@
"@types/common-tags": "^1.8.1",
"@types/cookie-parser": "^1.4.6",
"@types/express": "^4.17.21",
"@types/express-serve-static-core": "^5.0.6",
"@types/jest": "^29.5.2",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
Expand All @@ -58,6 +60,7 @@
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-jest": "^27.2.1",
"jest": "^29.6.1",
"node-mocks-http": "^1.16.2",
"nodemon": "^3.0.1",
"prettier": "^2.8.7",
"readline": "^1.3.0",
Expand Down
73 changes: 52 additions & 21 deletions packages/chatbot-server-mongodb-public/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
makeVerifiedAnswerGenerateUserPrompt,
makeDefaultFindVerifiedAnswer,
defaultCreateConversationCustomData,
defaultAddMessageToConversationCustomData,
} from "mongodb-chatbot-server";
import cookieParser from "cookie-parser";
import { makeStepBackRagGenerateUserPrompt } from "./processors/makeStepBackRagGenerateUserPrompt";
Expand All @@ -37,6 +38,7 @@ import {
makeCommentMessageUpdateTrace,
makeRateMessageUpdateTrace,
} from "./tracing/routesUpdateTraceHandlers";
import { useSegmentIds } from "./middleware/useSegmentIds";
export const {
MONGODB_CONNECTION_URI,
MONGODB_DATABASE_NAME,
Expand All @@ -62,6 +64,7 @@ const {
BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME,
SLACK_BOT_TOKEN,
SLACK_COMMENT_CONVERSATION_ID,
SEGMENT_WRITE_KEY,
} = process.env;

const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(",") || [];
Expand Down Expand Up @@ -225,42 +228,70 @@ const llmAsAJudgeConfig = {
},
};

const segmentConfig = SEGMENT_WRITE_KEY
? {
writeKey: SEGMENT_WRITE_KEY,
}
: undefined;

export const config: AppConfig = {
conversationsRouterConfig: {
llm,
middleware: [
blockGetRequests,
requireValidIpAddress(),
requireRequestOrigin(),
useSegmentIds(),
cookieParser(),
],
createConversationCustomData: !isProduction
? createConversationCustomDataWithAuthUser
: undefined,
addMessageToConversationCustomData: async (req, res) => {
const defaultCustomData = await defaultAddMessageToConversationCustomData(
req,
res
);
return {
...defaultCustomData,
segmentUserId: res.locals.customData.segmentUserId ?? undefined,
segmentAnonymousId:
res.locals.customData.segmentAnonymousId ?? undefined,
};
},
addMessageToConversationUpdateTrace:
makeAddMessageToConversationUpdateTrace(
retrievalConfig.findNearestNeighborsOptions.k,
{ ...llmAsAJudgeConfig, percentToJudge: isProduction ? 0.1 : 1 }
),
rateMessageUpdateTrace: makeRateMessageUpdateTrace(llmAsAJudgeConfig),
commentMessageUpdateTrace: makeCommentMessageUpdateTrace(
makeAddMessageToConversationUpdateTrace({
k: retrievalConfig.findNearestNeighborsOptions.k,
llmAsAJudge: {
...llmAsAJudgeConfig,
percentToJudge: isProduction ? 0.1 : 1,
},
segment: segmentConfig,
}),
rateMessageUpdateTrace: makeRateMessageUpdateTrace({
llmAsAJudge: llmAsAJudgeConfig,
segment: segmentConfig,
}),
commentMessageUpdateTrace: makeCommentMessageUpdateTrace({
openAiClient,
JUDGE_LLM,
SLACK_BOT_TOKEN !== undefined &&
judgeLlm: JUDGE_LLM,
slack:
SLACK_BOT_TOKEN !== undefined &&
SLACK_COMMENT_CONVERSATION_ID !== undefined
? {
token: SLACK_BOT_TOKEN,
conversationId: SLACK_COMMENT_CONVERSATION_ID,
llmAsAJudge: llmAsAJudgeConfig,
braintrust: BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME
? {
orgName: "mongodb-education-ai",
projectName: BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME,
}
: undefined,
}
: undefined
),
? {
token: SLACK_BOT_TOKEN,
conversationId: SLACK_COMMENT_CONVERSATION_ID,
llmAsAJudge: llmAsAJudgeConfig,
braintrust: BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME
? {
orgName: "mongodb-education-ai",
projectName: BRAINTRUST_CHATBOT_TRACING_PROJECT_NAME,
}
: undefined,
}
: undefined,
segment: segmentConfig,
}),
generateUserPrompt,
systemPrompt,
maxUserMessagesInConversation: 50,
Expand Down
Loading