Skip to content

Commit 141edbf

Browse files
Refactor Azure OpenAI integration: replace direct usage with new embedder and client utilities; remove unused web source constructor. (#2851)
1 parent 77b299f commit 141edbf

File tree

5 files changed

+54
-86
lines changed

5 files changed

+54
-86
lines changed

apps/chatbot-server/src/ingest/ingest.config.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
22
makeMongoDbEmbeddedContentStore,
33
makeMongoDbPageStore,
4-
makeOpenAiEmbedder,
54
} from 'mongodb-rag-core';
6-
import { AzureOpenAI } from 'mongodb-rag-core/openai';
75
import { Config, makeIngestMetaStore } from 'mongodb-rag-ingest';
86

97
import { loadEnvVars } from '../utils/loadEnv';
8+
import { makeEmbedder } from '../utils/makeEmbedder';
109

1110
import { leafygreenGithubSourceConstructor } from './sources/github-leafygreen-ui';
1211
import { mongoDbChatbotFrameworkDocsDataSourceConstructor } from './sources/github-mdb-chatbot-framework';
@@ -16,24 +15,11 @@ import { webSourceConstructor } from './utils/webSourceConstructor';
1615
const {
1716
MONGODB_CONNECTION_URI,
1817
MONGODB_DATABASE_NAME,
19-
AZURE_OPENAI_API_KEY,
20-
AZURE_OPENAI_ENDPOINT,
2118
AZURE_OPENAI_DEPLOYMENT,
2219
} = loadEnvVars();
2320

24-
const azureClient = new AzureOpenAI({
25-
endpoint: AZURE_OPENAI_ENDPOINT,
26-
apiKey: AZURE_OPENAI_API_KEY,
27-
apiVersion: '2024-04-01-preview',
28-
deployment: AZURE_OPENAI_DEPLOYMENT,
29-
});
30-
3121
export default {
32-
embedder: () =>
33-
makeOpenAiEmbedder({
34-
openAiClient: azureClient,
35-
deployment: AZURE_OPENAI_DEPLOYMENT,
36-
}),
22+
embedder: () => makeEmbedder(),
3723
embeddedContentStore: () =>
3824
makeMongoDbEmbeddedContentStore({
3925
connectionUri: MONGODB_CONNECTION_URI,

apps/chatbot-server/src/init.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import {
1111
makeMongoDbConversationsService,
1212
makeMongoDbEmbeddedContentStore,
1313
makeOpenAiChatLlm,
14-
makeOpenAiEmbedder,
1514
MongoDbEmbeddedContentStore,
1615
} from 'mongodb-rag-core';
1716
import { MongoClient } from 'mongodb-rag-core/mongodb';
1817
import { AzureOpenAI } from 'mongodb-rag-core/openai';
1918

2019
import { loadEnvVars } from './utils/loadEnv';
20+
import { makeEmbedder } from './utils/makeEmbedder';
2121

2222
export async function initChatBot(): Promise<{
2323
llm: ChatLlm;
@@ -37,13 +37,6 @@ export async function initChatBot(): Promise<{
3737
AZURE_OPENAI_CHAT_COMPLETION_MODEL,
3838
} = loadEnvVars();
3939

40-
const azureOpenAIEmbeddingClient = new AzureOpenAI({
41-
endpoint: AZURE_OPENAI_ENDPOINT,
42-
apiKey: AZURE_OPENAI_API_KEY,
43-
apiVersion: '2024-04-01-preview',
44-
deployment: AZURE_OPENAI_EMBEDDING_MODEL,
45-
});
46-
4740
const azureOpenAIChatClient = new AzureOpenAI({
4841
endpoint: AZURE_OPENAI_ENDPOINT,
4942
apiKey: AZURE_OPENAI_API_KEY,
@@ -62,11 +55,7 @@ export async function initChatBot(): Promise<{
6255

6356
// Creates vector embeddings for user queries to find matching content
6457
// in the embeddedContentStore using Atlas Vector Search.
65-
const embedder: Embedder = makeOpenAiEmbedder({
66-
openAiClient: azureOpenAIEmbeddingClient,
67-
deployment: AZURE_OPENAI_EMBEDDING_MODEL,
68-
backoffOptions: {},
69-
});
58+
const embedder: Embedder = makeEmbedder();
7059

7160
// MongoDB data source for the content used in RAG.
7261
// Generated with the Ingest CLI.
@@ -85,7 +74,7 @@ export async function initChatBot(): Promise<{
8574
store: embeddedContentStore,
8675
findNearestNeighborsOptions: {
8776
k: 5,
88-
path: 'embeddings.text-embedding-3-small',
77+
path: `embeddings.${AZURE_OPENAI_EMBEDDING_MODEL}`,
8978
indexName: VECTOR_SEARCH_INDEX_NAME,
9079
// Note: you may want to adjust the minScore depending
9180
// on the embedding model you use. We've found 0.9 works well
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { makeOpenAiEmbedder } from 'mongodb-rag-core';
2+
3+
import { loadEnvVars } from './loadEnv';
4+
import { makeEmbeddingClient } from './makeEmbeddingClient';
5+
6+
/**
7+
* Returns an consistent Azure OpenAI embedder
8+
* used in the ingest process
9+
* and in the chatbot server.
10+
*/
11+
export const makeEmbedder = () => {
12+
const { AZURE_OPENAI_DEPLOYMENT } = loadEnvVars();
13+
14+
const azureClient = makeEmbeddingClient();
15+
16+
return makeOpenAiEmbedder({
17+
openAiClient: azureClient,
18+
deployment: AZURE_OPENAI_DEPLOYMENT,
19+
backoffOptions: {},
20+
});
21+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { AzureOpenAI } from 'mongodb-rag-core/openai';
2+
3+
import { loadEnvVars } from './loadEnv';
4+
5+
/**
6+
* Returns an consistent Azure OpenAI client
7+
* that can be used to generate embeddings.
8+
*
9+
* Used in the ingest process
10+
* and in the chatbot server.
11+
*/
12+
export const makeEmbeddingClient = () => {
13+
// Load project environment variables
14+
const {
15+
AZURE_OPENAI_API_KEY,
16+
AZURE_OPENAI_ENDPOINT,
17+
AZURE_OPENAI_DEPLOYMENT,
18+
} = loadEnvVars();
19+
20+
const azureClient = new AzureOpenAI({
21+
endpoint: AZURE_OPENAI_ENDPOINT,
22+
apiKey: AZURE_OPENAI_API_KEY,
23+
apiVersion: '2024-04-01-preview',
24+
deployment: AZURE_OPENAI_DEPLOYMENT,
25+
});
26+
27+
return azureClient;
28+
};

apps/chatbot-server/src/utils/webSourceConstructor.ts

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

0 commit comments

Comments
 (0)