Skip to content

(EAI-995): add guardrail #755

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 39 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2e286c3
refactor GenerateRespose
Apr 28, 2025
0be9fe2
Clean up imports
Apr 28, 2025
2556bc2
consolidate generate user prompt to the legacy file
Apr 28, 2025
feee66a
update test config imports
Apr 28, 2025
f6fe862
Fix broken tests
Apr 28, 2025
0fc58bd
get started
Apr 28, 2025
ea40c6d
nominally working generate res w/ search
Apr 28, 2025
c3e69e3
small refactors
Apr 29, 2025
0345453
aint pretty but fully functional
Apr 29, 2025
a4144db
hacky if more functional
Apr 29, 2025
04076d2
more hack
May 2, 2025
8372bd3
tools
May 2, 2025
24d1cf7
functional if not pretty
May 9, 2025
40fa9d1
Add processing
May 12, 2025
31bd0a8
working tool calling
May 22, 2025
653aa59
making progress
May 23, 2025
9d68d50
keepin on
May 23, 2025
57e65a2
Clean config
May 23, 2025
aefa9ca
working e2e
May 27, 2025
c33f64e
update model version
May 27, 2025
c446756
Merge remote-tracking branch 'upstream/retrieval_tool_call' into EAI-990
May 27, 2025
728416f
Remove no longer used stuff
May 27, 2025
479ccb8
decouple search results for references and whats shown to model
May 27, 2025
cc8dd45
fix scripts build errs
May 27, 2025
e768dd6
fix broken tests
May 27, 2025
2bce005
update default ref links
May 27, 2025
b8f3754
fix broken tests
May 27, 2025
d19fdb1
input guardrail refactor
May 28, 2025
09071bc
guardrail works well
May 28, 2025
3fa56f7
simpler validity metric
May 28, 2025
4b2f6c0
add guardrail to server
May 28, 2025
8c1144e
add next step todo
May 28, 2025
3a1c8e8
llm refusal msg
May 29, 2025
c20e38d
remove TODO comment
May 29, 2025
4cb44c2
Merge remote-tracking branch 'upstream/retrieval_tool_call' into EAI-995
Jun 4, 2025
491d237
merge fix
Jun 4, 2025
9d2b11b
fix unnec changes
Jun 6, 2025
49da760
Merge branch 'retrieval_tool_call' into EAI-995
Jun 6, 2025
ac7b733
NL feedback
Jun 6, 2025
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
9 changes: 9 additions & 0 deletions packages/chatbot-server-mongodb-public/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
import { useSegmentIds } from "./middleware/useSegmentIds";
import { createAzure } from "mongodb-rag-core/aiSdk";
import { makeSearchTool } from "./tools/search";
import { makeMongoDbInputGuardrail } from "./processors/mongoDbInputGuardrail";
export const {
MONGODB_CONNECTION_URI,
MONGODB_DATABASE_NAME,
Expand Down Expand Up @@ -178,6 +179,11 @@ const azureOpenAi = createAzure({
});
const languageModel = wrapAISDKModel(azureOpenAi("gpt-4.1"));

const guardrailLanguageModel = wrapAISDKModel(azureOpenAi("gpt-4.1-mini"));
const inputGuardrail = makeMongoDbInputGuardrail({
model: guardrailLanguageModel,
});

export const generateResponse = wrapTraced(
makeVerifiedAnswerGenerateResponse({
findVerifiedAnswer,
Expand All @@ -192,6 +198,9 @@ export const generateResponse = wrapTraced(
languageModel,
systemMessage: systemPrompt,
makeReferenceLinks: makeMongoDbReferences,
inputGuardrail,
llmRefusalMessage:
conversations.conversationConstants.NO_RELEVANT_CONTENT,
filterPreviousMessages: async (conversation) => {
return conversation.messages.filter((message) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export const {
OPENAI_ENDPOINT,
OPENAI_API_VERSION,
OPENAI_CHAT_COMPLETION_DEPLOYMENT,
OPENAI_RESOURCE_NAME,
} = assertEnvVars({
...EVAL_ENV_VARS,
OPENAI_CHAT_COMPLETION_DEPLOYMENT: "",
OPENAI_PREPROCESSOR_CHAT_COMPLETION_DEPLOYMENT: "",
OPENAI_API_KEY: "",
OPENAI_ENDPOINT: "",
OPENAI_API_VERSION: "",
OPENAI_RESOURCE_NAME: "",
});

export const openAiClient = new AzureOpenAI({
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,37 @@ describe("makeMongoDbReferences", () => {
chunkIndex: 0,
},
] satisfies EmbeddedContent[];
const result = makeMongoDbReferences(chunks);
const result = makeMongoDbReferences(
chunks.map((c) => ({
...c,
title: c.metadata?.pageTitle,
}))
);
expect(result).toEqual([
{
url: "https://www.example.com/blog",
title: "Example Blog",
metadata: {
sourceName: "example",
sourceType: "Blog",
sourceName: "example",
tags: ["external", "example"],
},
},
{
url: "https://www.mongodb.com/love-your-developers",
title: "Love Your Developers",
metadata: {
sourceName: "mongodb-dotcom",
sourceType: "Article",
sourceName: "mongodb-dotcom",
tags: ["external", "example"],
},
},
{
url: "https://www.mongodb.com/developer/products/mongodb/best-practices-flask-mongodb",
title: "Best Practices for Using Flask and MongoDB",
metadata: {
sourceName: "devcenter",
sourceType: "Article",
sourceName: "devcenter",
tags: ["devcenter", "example", "python", "flask"],
},
},
Expand All @@ -114,7 +119,12 @@ describe("makeMongoDbReferences", () => {
chunkIndex: 0,
},
];
const result = makeMongoDbReferences(chunks);
const result = makeMongoDbReferences(
chunks.map((c) => ({
...c,
title: c.metadata?.pageTitle,
}))
);
expect(result).toEqual([
{
url: "https://www.example.com/somepage",
Expand All @@ -134,13 +144,11 @@ describe("addReferenceSourceType", () => {
url: "https://mongodb.com/docs/manual/reference/operator/query/eq/",
title: "$eq",
metadata: {
sourceName: "snooty-docs",
tags: ["docs", "manual"],
},
};
const result = addReferenceSourceType(reference);
expect(result.metadata).toEqual({
sourceName: reference.metadata?.sourceName,
tags: reference.metadata?.tags,
sourceType: "Docs",
});
Expand All @@ -151,7 +159,6 @@ describe("addReferenceSourceType", () => {
url: "https://mongodb.com/docs/manual/reference/operator/query/eq/",
title: "$eq",
metadata: {
sourceName: "snooty-docs",
sourceType: "ThinAir",
tags: ["docs", "manual"],
},
Expand All @@ -169,7 +176,6 @@ describe("addReferenceSourceType", () => {
url: "https://example.com/random-thoughts/hotdogs-are-tacos",
title: "Hotdogs Are Just Tacos",
metadata: {
sourceName: "some-random-blog",
tags: ["external"],
},
};
Expand Down
Loading