Atlas Vector Search による検索拡張生成(RAG)
検索拡張生成(RAG)は、大規模な言語モデル(llm)を追加のデータで強化して、より正確な応答を生成できるようにするために使用されるアーキテクチャです。 RAGAILLMを基盤とした検索システムとAtlas Vector Search を組み合わせて、生成系 アプリケーションに実装できます。
RAG を使用する理由
LLMを扱う場合、次の制限が発生する可能性があります。
古いデータ: LLMは一定時点まで静的データセットで訓練されます。 つまり、知識ベースが限られており、古いデータを使用する可能性があります。
ローカル データへのアクセスなし: LLMはローカル データまたはパーソナライズ データへのアクセスがありません。 そのため、特定のドメインに関する知識が不足することがあります。
説明: 訓練データが不完全または古くなっている場合、 LLMは不正確な情報を生成する可能性があります。
RAGを実装するには次の手順を持つことで、これらの制限に対処できます。
取り込み: MongoDB Atlas などのベクトルデータベースにカスタム データをベクトル埋め込みとして保存します。 これにより、最新のパーソナライズされたデータの知識ベースを作成できます。
検索: Atlas Vector Search などの検索ソリューションを使用して、ユーザーの質問に基づいてデータベースからセマンティックに類似したドキュメントを検索します。 これらのドキュメントは、 LLMに関連するデータを追加します。
生成: LLMを要求します。 LLMは検索されたドキュメントをコンテキストとして使用して、より正確で関連性の高い応答を生成し、プロ認証を削減します。
RAGは質問応答やテキスト生成などのタスクを実行できるため、パーソナライズされたドメイン固有の応答を提供する AI チャットボットを構築するための効果的なアーキテクチャになります。 本番環境に対応したチャットボットを作成するには、リクエストをルーティングするようにサーバーを設定し、 RAG実装上にユーザー インターフェースを構築する必要があります。
RAG と Atlas Vector Search
RAGでAtlas Vector Search を実装するには、 にデータを取り込みAtlas 、Atlas Vector Search を使用してドキュメントを検索し、LLM を使用して応答を生成します。このセクションでは、Atlas Vector Search を使用した基本的な、またはネイティブのRAG実装のコンポーネントについて説明します。 ステップ別手順については、「 を使用する 」を参照してください。
取り込み
RAGのデータ取り込みには、カスタム データを処理し、ベクトル データベースに保存して取得に準備する方法が含まれます。 Atlas をベクトル データベースとして基本的な取り込みパイプラインを作成するには、次の手順を実行します。
データをロードします。
このチュートリアルではPDF Pig を使用して、PDFからデータを読み込みます。
データをチャンクに分割します。
データを処理またはチャンクします。 チャンク では、パフォーマンスを向上させるためにデータを小さな部分に分割する必要があります。
データをベクトル埋め込みに変換します。
埋め込みモデル を使用してデータを ベクトル埋め込み に変換します。詳細については、「ベクトル埋め込みの作成方法」を参照してください。
データと埋め込みを Atlas に保存します。
これらの埋め込みを Atlas に保存します。 埋め込みは、コレクション内の他のデータと一緒にフィールドとして保存します。
データをロードします。
ドキュメント ローダーなどのツールを使用して、さまざまなデータ形式とロケーションからデータをロードします。
データをチャンクに分割します。
データを処理またはチャンクします。 チャンク では、パフォーマンスを向上させるためにデータを小さな部分に分割する必要があります。
データをベクトル埋め込みに変換します。
埋め込みモデル を使用してデータを ベクトル埋め込み に変換します。詳細については、「ベクトル埋め込みの作成方法」を参照してください。
データと埋め込みを Atlas に保存します。
これらの埋め込みを Atlas に保存します。 埋め込みは、コレクション内の他のデータと一緒にフィールドとして保存します。
データをロードします。
ドキュメントローダーおよびパーサーなどのツールを使用して、さまざまなデータ形式と場所からデータを読み込みます。
解析されたデータをチャンクに分割してください。
データを処理またはチャンクします。 チャンク では、パフォーマンスを向上させるためにデータを小さな部分に分割する必要があります。
データをベクトル埋め込みに変換します。
埋め込みモデル を使用してデータを ベクトル埋め込み に変換します。詳細については、「ベクトル埋め込みの作成方法」を参照してください。
データと埋め込みを Atlas に保存します。
これらの埋め込みを Atlas に保存します。 埋め込みは、コレクション内の他のデータと一緒にフィールドとして保存します。
データをロードします。
ドキュメントローダー などのツールを使用する または データ コネクタ を使用して、さまざまなデータ形式と場所からデータをロードします。
データをチャンクに分割します。
データを処理またはチャンクします。 チャンク では、パフォーマンスを向上させるためにデータを小さな部分に分割する必要があります。
データをベクトル埋め込みに変換します。
埋め込みモデル を使用してデータを ベクトル埋め込み に変換します。詳細については、「ベクトル埋め込みの作成方法」を参照してください。
データと埋め込みを Atlas に保存します。
これらの埋め込みを Atlas に保存します。 埋め込みは、コレクション内の他のデータと一緒にフィールドとして保存します。
データをロードします。
ドキュメントローダー などのツールを使用する または データ コネクタ を使用して、さまざまなデータ形式と場所からデータをロードします。
データをチャンクに分割します。
データを処理またはチャンクします。 チャンク では、パフォーマンスを向上させるためにデータを小さな部分に分割する必要があります。
データをベクトル埋め込みに変換します。
埋め込みモデル を使用してデータを ベクトル埋め込み に変換します。詳細については、「ベクトル埋め込みの作成方法」を参照してください。
データと埋め込みを Atlas に保存します。
これらの埋め込みを Atlas に保存します。 埋め込みは、コレクション内の他のデータと一緒にフィールドとして保存します。
Retrieval
取得システムを構築するには、ベクトル データベースから最も関連するドキュメントを検索して返すことが含まれ、 LLMで増加します。 Atlas Vector Search で関連するドキュメントを検索するには、ユーザーの質問をベクトル埋め込みに変換し、Atlas のデータに対してベクトル検索クエリを実行し、最も類似した埋め込みを持つドキュメントを検索します。
Atlas Vector Search で基本的な検索を実行するには、次の手順を実行します。
ベクトル埋め込みを含むコレクションにAtlas Vector Search インデックスを定義します。
ユーザーの質問に基づいてドキュメントを取得するには、次のいずれかの方法を選択します。
一般的なフレームワークまたはサービスとAtlas Vector Search 統合を使用します。 これらの統合には、Atlas Vector Search で検索システムを簡単に構築できる組み込みのライブラリとツールが含まれています。
独自の検索システムを構築します。 独自の関数とパイプラインを定義して、ユースケースに固有のAtlas Vector Search クエリを実行できます。
Atlas Vector Search を使用して基本的な検索システムを構築する方法については、「始める」を参照してください。
生成
応答を生成するには、検索システムとLLMを組み合わせます。 ベクトル検索を実行して関連するドキュメントを検索した後、より正確な応答を生成できるように、ユーザーの質問と関連するドキュメントをコンテキストとしてLLMに提供します。
LLMに接続するには、次のいずれかの方法を選択します。
一般的なフレームワークまたはサービスとAtlas Vector Search 統合を使用します。 これらの統合には組み込みのライブラリとツールが含まれており、最小限の設定でLLMに接続するのに役立ちます。
LLMのAPI呼び出します。 ほとんどの AI プロバイダーは、応答を生成するために使用できる生成モデルへのAPIを提供しています。
オープンソースのLLM をロードします。APIキーやクレジットをお持ちでない場合は、アプリケーションからローカルにロードして、オープンソースのLLMを使用することができます。実装例については、「Atlas Vector Searchを使用してローカル RAG 実装をビルドする」チュートリアルをご覧ください。
はじめる
次の例は、Atlas Vector Search を基盤とした検索システムで RAG を実装する方法を示しています。
➤ [言語の選択] ドロップダウン メニューを使用して、このページの例の言語を設定します。
前提条件
この例を完了するには、次のものが必要です。
Atlas アカウントで MongoDB バージョン 6.0.11 を実行中のクラスター、7.0.2以降(RCsを含む)。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認する。詳細については、「 クラスターの作成 」を参照してください。
OpenAI APIキー。
.NET プロジェクトを実行するためのターミナルとコードエディター。
.NET バージョン8.0以上 がインストールされています。
Atlas アカウントで MongoDB バージョン 6.0.11 を実行中のクラスター、7.0.2以降(RCsを含む)。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認する。詳細については、「 クラスターの作成 」を参照してください。
はと文字のアクセス トークン 読み取りアクセス権を持つ。
Go プロジェクトを実行するためのターミナルとコード エディター。
Go がインストールされました。
Atlas アカウントで MongoDB バージョン 6.0.11 を実行中のクラスター、7.0.2以降(RCsを含む)。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認する。詳細については、「 クラスターの作成 」を参照してください。
Java 開発キット(JDK) バージョン8 以降。
Javaアプリケーション を設定して実行する環境。 Maven または Gradle を構成してプロジェクトを構築および実行するようにするには、IntelliJ IDEA や Eclipse IDE などの統合開発環境(IDE)を使用することをお勧めします。
はと文字のアクセス トークン 読み取りアクセス権を持つ。
Atlas アカウントで MongoDB バージョン 6.0.11 を実行中のクラスター、7.0.2以降(RCsを含む)。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認する。詳細については、「 クラスターの作成 」を参照してください。
はと文字のアクセス トークン 読み取りアクセス権を持つ。
Node.js プロジェクトを実行するためのターミナルとコード エディター。
npm と Node.js インストール済み。
Atlas アカウントで MongoDB バージョン 6.0.11 を実行中のクラスター、7.0.2以降(RCsを含む)。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認する。詳細については、「 クラスターの作成 」を参照してください。
はと文字のアクセス トークン 読み取りアクセス権を持つ。
Comb などのインタラクティブ Python ノートを実行するための環境。
手順
環境を設定します。
.NET プロジェクトを初期化します。
ターミナルで次のコマンドを実行して、
MyCompany.RAG
という名前の新しいディレクトリを作成し、プロジェクトを初期化します。dotnet new console -o MyCompany.RAG cd MyCompany.RAG 依存関係をインストールしてインポートします。
次のコマンドを実行します。
dotnet add package MongoDB.Driver --version 3.1.0 dotnet add package PdfPig dotnet add package OpenAI 環境変数を設定します。
次の環境変数を
set
PowerShell でエクスポートするか、IDE の環境変数マネージャーを使用して、これらの変数をプロジェクトで利用可能にしてください。export OPENAI_API_KEY="<api-key>" export ATLAS_CONNECTION_STRING="<connection-string>"
<api-key>
プレースホルダー値を OpenAI APIキーに置き換えます。
<connection-string>
プレースホルダー値を、Atlas クラスター SRV 接続文字列に置き換えます。
接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
ベクトル埋め込みを生成する関数を作成します。
次のコードを貼り付けて、OpenAIService
という名前の新しいクラスを同じ名前のファイルに作成してください。このコードは、GetEmbeddingsAsync
という名前の非同期タスクを定義して、指定されたstring入力の配列に対する埋め込みの配列を生成します。この関数は、OpenAI の text-embedding-3-small
モデルを使用して、指定された入力の埋め込みを生成します。
namespace MyCompany.RAG; using OpenAI.Embeddings; using System; using System.Threading.Tasks; public class OpenAIService { private static readonly string? OpenAIApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY"); private static readonly string EmbeddingModelName = "text-embedding-3-small"; private static readonly EmbeddingClient EmbeddingClient = new(model: EmbeddingModelName, apiKey: OpenAIApiKey); public async Task<Dictionary<string, float[]>> GetEmbeddingsAsync(string[] texts) { Dictionary<string, float[]> documentData = new Dictionary<string, float[]>(); try { var result = await EmbeddingClient.GenerateEmbeddingsAsync(texts); var embeddingCount = result.Value.Count; foreach (var index in Enumerable.Range(0, embeddingCount)) { // Pair each embedding with the text used to generate it. documentData[texts[index]] = result.Value[index].ToFloats().ToArray(); } } catch (Exception e) { throw new ApplicationException(e.Message); } return documentData; } }
Atlas にデータを取り込みます。
このセクションでは、取り込みますサンプルデータを Atlas に、LLM がアクセスできない。
データをロードして分割します。
次のコードを貼り付けて、
PdfIngester
という名前の新しいクラスを同じ名前のファイルに作成してください。このコードには、次の処理を実行するいくつかの関数が含まれています。MongoDB の収益レポート を含む PDF をロードします。
PdfPigを使用して PDF をテキストに解析します。
チャンクサイズ(文字数)とチャンクのオーバーラップ(連続するチャンク間で重なり合う文字数)を指定して、テキストをチャンクに分割します。
PdfIngester.csnamespace MyCompany.RAG; using System; using System.Net.Http; using System.IO; using System.Threading.Tasks; using System.Collections.Generic; using System.Text; using UglyToad.PdfPig; using UglyToad.PdfPig.Content; public class PdfIngester { public async Task<String> DownloadPdf(string url, string path, string fileName) { using (HttpClient client = new HttpClient()) { try { byte[] pdfBytes = await client.GetByteArrayAsync(url); await File.WriteAllBytesAsync(path + fileName, pdfBytes); return "PDF downloaded and saved to " + path + fileName; } catch (HttpRequestException e) { throw new ApplicationException("Error downloading the PDF: " + e.Message); } catch (IOException e) { throw new ApplicationException("Error writing the file to disk: " + e.Message); } } } public List<string> ConvertPdfToChunkedText(string filePath) { List<string> textChunks; using (var document = PdfDocument.Open(filePath)) { StringBuilder fullText = new StringBuilder(); foreach (Page page in document.GetPages()) { fullText.Append(page.Text + "\n"); } textChunks = ChunkText(fullText.ToString(), 400, 20); } var chunkCount = textChunks.Count; if (chunkCount == 0) { throw new ApplicationException("Unable to chunk PDF contents into text."); } Console.WriteLine($"Successfully chunked the PDF text into {chunkCount} chunks."); return textChunks; } static List<string> ChunkText(string text, int chunkSize, int overlap) { List<string> chunks = new List<string>(); int start = 0; int textLength = text.Length; while (start < textLength) { int end = start + chunkSize; if (end > textLength) { end = textLength; } string chunk = text.Substring(start, end - start); chunks.Add(chunk); // Increment starting point, considering the overlap start += chunkSize - overlap; if (start >= textLength) break; } return chunks; } } データと埋め込みを Atlas に保存する準備をしてください。
次のコードを貼り付けて、
MongoDBDataService
という名前の新しいクラスを同じ名前のファイルに作成してください。このコードは、Atlasにドキュメントを追加するためのAddDocumentsAsync
という名前の非同期タスクを定義しています。この関数は、Collection.InsertManyAsync() C# ドライバー メソッドを使用して、BsonDocument
型のリストを挿入します。このコードは、埋め込みデータをチャンク化されたデータと共に、Atlas クラスターのrag_db.test
コレクションに保存します。MongoDBDataService.csnamespace MyCompany.RAG; using MongoDB.Driver; using MongoDB.Bson; public class MongoDBDataService { private static readonly string? ConnectionString = Environment.GetEnvironmentVariable("ATLAS_CONNECTION_STRING"); private static readonly MongoClient Client = new MongoClient(ConnectionString); private static readonly IMongoDatabase Database = Client.GetDatabase("rag_db"); private static readonly IMongoCollection<BsonDocument> Collection = Database.GetCollection<BsonDocument>("test"); public async Task<string> AddDocumentsAsync(Dictionary<string, float[]> embeddings) { var documents = new List<BsonDocument>(); foreach( KeyValuePair<string, float[]> var in embeddings ) { var document = new BsonDocument { { "text", var.Key }, { "embedding", new BsonArray(var.Value) } }; documents.Add(document); } await Collection.InsertManyAsync(documents); return $"Successfully inserted {embeddings.Count} documents into Atlas."; } } データをベクトル埋め込みに変換します。
次のコードを貼り付けて、
EmbeddingGenerator
という名前の新しいクラスを同じ名前のファイルに作成してください。このコードは、対応するベクトル埋め込みを持つドキュメントのリストを作成することで、チャンク化されたドキュメントの取り込みを準備します。これらの埋め込みは、以前に定義したGetEmbeddingsAsync
関数を使用して生成します。EmbeddingGenerator.csnamespace MyCompany.RAG; public class EmbeddingGenerator { private readonly MongoDBDataService _dataService = new(); private readonly OpenAIService _openAiService = new(); public async Task<string> GenerateEmbeddings(List<string> textChunks) { Console.WriteLine("Generating embeddings."); Dictionary<string, float[]> docs = new Dictionary<string, float[]>(); try { // Pass the text chunks to OpenAI to generate vector embeddings var embeddings = await _openAiService.GetEmbeddingsAsync(textChunks.ToArray()); // Pair each embedding with the text chunk used to generate it int index = 0; foreach (var embedding in embeddings) { docs[textChunks[index]] = embedding.Value; index++; } } catch (Exception e) { throw new ApplicationException("Error creating embeddings for text chunks: " + e.Message); } // Add a new document to the MongoDB collection for each text and vector embedding pair var result = await _dataService.AddDocumentsAsync(docs); return result; } } Program.cs
ファイルを更新してください。Program.cs
にこのコードを貼り付けてください。Program.csusing MyCompany.RAG; const string pdfUrl = "https://investors.mongodb.com/node/12236/pdf"; const string savePath = "<path-name>"; const string fileName = "investor-report.pdf"; var pdfIngester = new PdfIngester(); var pdfDownloadResult = await pdfIngester.DownloadPdf(pdfUrl, savePath, fileName); Console.WriteLine(pdfDownloadResult); var textChunks = pdfIngester.ConvertPdfToChunkedText(savePath + fileName); if (textChunks.Any()) { var embeddingGenerator = new EmbeddingGenerator(); var embeddingGenerationResult = await embeddingGenerator.GenerateEmbeddings(textChunks); Console.WriteLine(embeddingGenerationResult); } このコード:
PdfIngester
を使用して PDF をテキストセグメントに読み込み、チャンク化します。EmbeddingGenerator
を使用してPDFから各テキストチャンクの埋め込みを生成し、テキストチャンクと埋め込みをrag_db.test
コレクションに書き込みます。
<path-name>
プレースホルダーを、レポートをダウンロードしたいパスに置き換えてください。macOS システムでは、パスは/Users/<username>/MyCompany.RAG/
のように表示されます。パスは後続のスラッシュで終わる必要があります。プロジェクトをコンパイルして実行し、埋め込みを生成してください。
dotnet run MyCompany.RAG.csproj PDF downloaded and saved to <PATH> Successfully chunked the PDF text into 73 chunks. Generating embeddings. Successfully inserted 73 documents into Atlas.
Atlas Vector Search を使用してドキュメントを検索します。
このセクションでは、Atlas Vector Search を設定してドキュメントを検索するためにベクトルデータベースを使用します。MongoDB C# ドライバー v3.1.0 以降を使用してコレクションの Atlas Vector Search インデックスを作成するには、次の手順を実行します。
Atlas Vector Search インデックスを定義します。
MongoDBDataService.cs
という名前のファイルに新しいCreateVectorIndex()
メソッドを追加して、検索インデックスを定義してください。このコードは、Atlas クラスターに接続し、rag_db.test
コレクションにvectorSearchタイプのインデックスを作成します。MongoDBDataService.csnamespace MyCompany.RAG; using MongoDB.Driver; using MongoDB.Bson; public class DataService { private static readonly string? ConnectionString = Environment.GetEnvironmentVariable("ATLAS_CONNECTION_STRING"); private static readonly MongoClient Client = new MongoClient(ConnectionString); private static readonly IMongoDatabase Database = Client.GetDatabase("rag_db"); private static readonly IMongoCollection<BsonDocument> Collection = Database.GetCollection<BsonDocument>("test"); public async Task<string> AddDocumentsAsync(Dictionary<string, float[]> embeddings) { // Method details... } public string CreateVectorIndex() { var searchIndexView = Collection.SearchIndexes; var name = "vector_index"; var type = SearchIndexType.VectorSearch; var definition = new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "embedding" }, { "numDimensions", 1536 }, { "similarity", "cosine" } } } } }; var model = new CreateSearchIndexModel(name, type, definition); try { searchIndexView.CreateOne(model); Console.WriteLine($"New search index named {name} is building."); // Polling for index status Console.WriteLine("Polling to check if the index is ready. This may take up to a minute."); bool queryable = false; while (!queryable) { var indexes = searchIndexView.List(); foreach (var index in indexes.ToEnumerable()) { if (index["name"] == name) { queryable = index["queryable"].AsBoolean; } } if (!queryable) { Thread.Sleep(5000); } } } catch (Exception e) { throw new ApplicationException("Error creating the vector index: " + e.Message); } return $"{name} is ready for querying."; } } Program.cs
ファイルを更新してください。Program.cs
のコードを以下のコードに置き換えてインデックスを作成してください:Program.csusing MyCompany.RAG; var dataService = new MongoDBDataService(); var result = dataService.CreateVectorIndex(); Console.WriteLine(result); プロジェクトをコンパイルして実行し、インデックスを作成します。
dotnet run MyCompany.RAG.csproj 関連データを取得するための関数を定義します。
MongoDBDataService.cs
という名前のファイルに新しいPerformVectorQuery
メソッドを追加して、関連するドキュメントを検索します。詳細については、「 ベクター検索クエリの実行 」を参照してください。MongoDBDataService.csnamespace MyCompany.RAG; using MongoDB.Driver; using MongoDB.Bson; public class MongoDBDataService { private static readonly string? ConnectionString = Environment.GetEnvironmentVariable("ATLAS_CONNECTION_STRING"); private static readonly MongoClient Client = new MongoClient(ConnectionString); private static readonly IMongoDatabase Database = Client.GetDatabase("rag_db"); private static readonly IMongoCollection<BsonDocument> Collection = Database.GetCollection<BsonDocument>("test"); public async Task<string> AddDocumentsAsync(Dictionary<string, float[]> embeddings) { // Method details... } public string CreateVectorIndex() { // Method details... } public List<BsonDocument>? PerformVectorQuery(float[] vector) { var vectorSearchStage = new BsonDocument { { "$vectorSearch", new BsonDocument { { "index", "vector_index" }, { "path", "embedding" }, { "queryVector", new BsonArray(vector) }, { "exact", true }, { "limit", 5 } } } }; var projectStage = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "text", 1 }, { "score", new BsonDocument { { "$meta", "vectorSearchScore"} } } } } }; var pipeline = new[] { vectorSearchStage, projectStage }; return Collection.Aggregate<BsonDocument>(pipeline).ToList(); } } データの取得をテストします。
次のコードを貼り付けて、同じ名前のファイルに
PerformTestQuery
という名前の新しいクラスを作成してください。このコードは、テキスト入力文字列をベクトル埋め込みに変換し、一致する結果をデータベースにクエリします。GetEmbeddingsAsync
関数を使用して、検索クエリから埋め込みを作成します。次に、クエリを実行して、セマンティックに類似したドキュメントを返します。PerformTestQuery.csnamespace MyCompany.RAG; public class PerformTestQuery { private readonly MongoDBDataService _dataService = new(); private readonly OpenAIService _openAiService = new(); public async Task<string> GetQueryResults(string question) { // Get the vector embedding for the query var query = question; var queryEmbeddings = await _openAiService.GetEmbeddingsAsync([query]); // Query the vector database for applicable query results var matchingDocuments = _dataService.PerformVectorQuery(queryEmbeddings[query]); // Construct a string from the query results for performing QA with the LLM var sb = new System.Text.StringBuilder(); if (matchingDocuments != null) { foreach (var doc in matchingDocuments) { sb.AppendLine($"Text: {doc.GetValue("text").ToString()}"); sb.AppendLine($"Score: {doc.GetValue("score").ToString()}"); } } else { return "No matching documents found."; } return sb.ToString(); } } Program.cs
ファイルを更新してください。Program.cs
のコードを次のコードに置き換えて、テストクエリを実行してください。Program.csusing MyCompany.RAG; var query = "AI Technology"; var queryCoordinator = new PerformTestQuery(); var result = await queryCoordinator.GetQueryResults(query); Console.WriteLine(result); プロジェクトをコンパイルして実行し、クエリ結果を確認してください。
dotnet run MyCompany.RAG.csproj Text: time series queries—and the general availability of Atlas Stream Processing to build sophisticated,event-driven applications with real-time data.MongoDB continues to expand its AI ecosystem with the announcement of the MongoDB AI Applications Program (MAAP), which provides customers with reference architectures, pre-built partner integrations, and professional services to helpthem quickly build AI Score: 0.72528624534606934 Text: hem quickly build AI-powered applications. Accenture will establish a center of excellence focused on MongoDB projects,and is the first global systems integrator to join MAAP.Bendigo and Adelaide Bank partnered with MongoDB to modernize their core banking technology. With the help ofMongoDB Relational Migrator and generative AI-powered modernization tools, Bendigo and Adelaide Bank decomposed anou Score: 0.71915638446807861 Text: and regulatory issues relating to the use of new and evolving technologies, such asartificial intelligence, in our offerings or partnerships; the growth and expansion of the market for database products and our ability to penetrate thatmarket; our ability to integrate acquired businesses and technologies successfully or achieve the expected benefits of such acquisitions; our ability tomaintain the Score: 0.70376789569854736 Text: architecture is particularly well-suited for the variety and scale of data required by AI-powered applications. We are confident MongoDB will be a substantial beneficiary of this next wave of application development."First Quarter Fiscal 2025 Financial HighlightsRevenue: Total revenue was $450.6 million for the first quarter of fiscal 2025, an increase of 22% year-over-year.Subscription revenue wa Score: 0.67905724048614502 Text: tures, services orenhancements; our ability to effectively expand our sales and marketing organization; our ability to continue to build and maintain credibility with thedeveloper community; our ability to add new customers or increase sales to our existing customers; our ability to maintain, protect, enforce andenhance our intellectual property; the effects of social, ethical and regulatory issue Score: 0.64435118436813354
LLM を使用して応答を生成します。
このセクションでは、検索されたドキュメントをコンテキストとして使用するよう LLM に指示して応答を 生成 します。この例では、先ほど定義した関数を使用して、一致するドキュメントを データベースから検索し、さらに次のことも行います。
OpenAI の gpt-4o-mini モデルにアクセスします。
プロンプトにユーザーの質問と検索されたドキュメントを含めるようにLLMに指示します。
LLMMongoDBの最新のAI に関する発表について を要求します。
OpenAIService.cs
という名前のファイルに、インポート、新しいChatClient
情報、およびGenerateAnswer
という新しいメソッドを追加してください。OpenAIService.csnamespace MyCompany.RAG; using OpenAI.Embeddings; using OpenAI.Chat; using System; using System.Text; using System.Threading.Tasks; public class OpenAIService { private static readonly string? OpenAIApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY"); private static readonly string EmbeddingModelName = "text-embedding-3-small"; private static readonly EmbeddingClient EmbeddingClient = new(model: EmbeddingModelName, apiKey: OpenAIApiKey); private static readonly string ChatModelName = "gpt-4o-mini"; private static readonly ChatClient ChatClient = new(model: ChatModelName, apiKey: OpenAIApiKey); public async Task<Dictionary<string, float[]>> GetEmbeddingsAsync(string[] texts) { // Method details... } public async Task<string> GenerateAnswer(string question, string context) { string prompt = $""" Answer the following question based on the given context. Context: {context} Question: {question} """; byte[] binaryContent = Encoding.UTF8.GetBytes(prompt); IEnumerable<ChatMessage> messages = new List<ChatMessage>([prompt]); ChatCompletion responses = await ChatClient.CompleteChatAsync(messages, new ChatCompletionOptions { MaxOutputTokenCount = 400 }); var summaryResponse = responses.Content[0].Text; if (summaryResponse is null) { throw new ApplicationException("No response from the chat client."); } return summaryResponse; } } RAGPipeline
クラスを作成してください。次のコードを貼り付けて、同名のファイルに
RAGPipeline
という名前の新しいクラスを作成してください。このコードは、次のコンポーネントを調整します。GetEmbeddingsAsync
関数: 文字列クエリをベクトル埋め込みに変換する。PerformVectorQuery
関数: データベースから意味的に類似した結果を検索します。GenerateAnswer
機能: データベースから検索したドキュメントをLLMに渡して応答を生成します。
RAGPipeline.csnamespace MyCompany.RAG; public class RAGPipeline { private readonly MongoDBDataService _dataService = new(); private readonly OpenAIService _openAiService = new(); public async Task<string> GenerateResults(string question) { // Get the vector embedding for the query var query = question; var queryEmbedding = await _openAiService.GetEmbeddingsAsync([query]); // Query the vector database for applicable query results var matchingDocuments = _dataService.PerformVectorQuery(queryEmbedding[query]); // Construct a string from the query results for performing QA with the LLM var sb = new System.Text.StringBuilder(); if (matchingDocuments != null) { foreach (var doc in matchingDocuments) { sb.AppendLine($"Text: {doc.GetValue("text").ToString()}"); } } else { return "No matching documents found."; } return await _openAiService.GenerateAnswer(question, sb.ToString()); } } Program.cs
ファイルを更新してください。Program.cs
のコードを次のコードで置き換えて、RAG パイプラインを呼び出します。Program.csusing MyCompany.RAG; var question = "In a few sentences, what are MongoDB's latest AI announcements?"; var ragPipeline = new RAGPipeline(); var result = await ragPipeline.GenerateResults(question); Console.WriteLine(result); プロジェクトをコンパイルして実行し、RAG を実行します。生成される応答は異なる場合があります。
dotnet run MyCompany.RAG.csproj MongoDB has recently announced the MongoDB AI Applications Program (MAAP), which aims to support customers in building AI-powered applications through reference architectures, pre-built partner integrations, and professional services. Additionally, the program includes a partnership with Accenture, which will establish a center of excellence focused on MongoDB projects. These initiatives demonstrate MongoDB's commitment to expanding its AI ecosystem and its strategy to adapt its document-based architecture for the demands of AI-driven application development.
環境を設定します。
Go プロジェクトを初期化します。
ターミナルで次のコマンドを実行して、
rag-mongodb
という名前の新しいディレクトリを作成し、プロジェクトを初期化します。mkdir rag-mongodb cd rag-mongodb go mod init rag-mongodb 依存関係をインストールしてインポートします。
次のコマンドを実行します。
go get github.com/joho/godotenv go get go.mongodb.org/mongo-driver/mongo go get github.com/tmc/langchaingo/llms go get github.com/tmc/langchaingo/documentloaders go get github.com/tmc/langchaingo/embeddings/huggingface go get github.com/tmc/langchaingo/llms/huggingface go get github.com/tmc/langchaingo/prompts .env
ファイルを作成します。プロジェクトで、 Atlas 接続文字列と Hugeface アクセス トークンを保存するための
.env
ファイルを作成します。.envHUGGINGFACEHUB_API_TOKEN = "<access-token>" ATLAS_CONNECTION_STRING = "<connection-string>" <access-token>
プレースホルダー値を 1 つのドキュメント アクセス トークンに置き換えます。<connection-string>
プレースホルダー値を、Atlas クラスター SRV 接続文字列に置き換えます。接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
ベクトル埋め込みを生成する関数を作成します。
このセクションでは、次の関数を作成します。
Hugging Face のモデル ハブから、mxbai-embed-large-v1 埋め込みモデルを読み込みます。
入力データからベクトル埋め込みを作成します。
次のコマンドを実行して、埋め込みの作成に再利用できるものを含め、共通の機能を格納するディレクトリを作成します。
mkdir common && cd common common
ディレクトリにget-embeddings.go
というファイルを作成し、次のコードをそのファイルに貼り付けます。get-embeddings.gopackage common import ( "context" "log" "github.com/tmc/langchaingo/embeddings/huggingface" ) func GetEmbeddings(documents []string) [][]float32 { hf, err := huggingface.NewHuggingface( huggingface.WithModel("mixedbread-ai/mxbai-embed-large-v1"), huggingface.WithTask("feature-extraction")) if err != nil { log.Fatalf("failed to connect to Hugging Face: %v", err) } embs, err := hf.EmbedDocuments(context.Background(), documents) if err != nil { log.Fatalf("failed to generate embeddings: %v", err) } return embs }
Atlas にデータを取り込みます。
このセクションでは、LLM がアクセスできないサンプルデータを Atlasに取り込みます。以下のコードでは、LangChain 用の Go ライブラリと Go ドライバを使って、次の処理を実行します。
MongoDB 収益レポートを含む HTML ファイルを作成します。
データをチャンクに分割し、チャンク サイズ(文字数)とチャンクの重複(連続するチャンク間で重複する文字数)を指定します。
定義した
GetEmbeddings
関数を使用して、チャンク データからベクトル埋め込みを作成します。これらの埋め込みを、Atlas クラスターの
rag_db.test
コレクション内のチャンク データと一緒に保存します。
rag-mongodb
プロジェクト ディレクトリのルートに移動します。プロジェクトに
ingest-data.go
というファイルを作成し、次のコードをそのファイルに貼り付けます。ingest-data.gopackage main import ( "context" "fmt" "io" "log" "net/http" "os" "rag-mongodb/common" // Module that contains the embedding function "github.com/joho/godotenv" "github.com/tmc/langchaingo/documentloaders" "github.com/tmc/langchaingo/textsplitter" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type DocumentToInsert struct { PageContent string `bson:"pageContent"` Embedding []float32 `bson:"embedding"` } func downloadReport(filename string) { _, err := os.Stat(filename) if err == nil { return } url := "https://investors.mongodb.com/node/12236" fmt.Println("Downloading ", url, " to ", filename) resp, err := http.Get(url) if err != nil { log.Fatalf("failed to connect to download the report: %v", err) } defer func() { _ = resp.Body.Close() }() f, err := os.Create(filename) if err != nil { return } defer func() { _ = f.Close() }() _, err = io.Copy(f, resp.Body) if err != nil { log.Fatalf("failed to copy the report: %v", err) } } func main() { ctx := context.Background() filename := "investor-report.html" downloadReport(filename) f, err := os.Open(filename) if err != nil { defer func() { _ = f.Close() }() log.Fatalf("failed to open the report: %v", err) } defer func() { _ = f.Close() }() html := documentloaders.NewHTML(f) split := textsplitter.NewRecursiveCharacter() split.ChunkSize = 400 split.ChunkOverlap = 20 docs, err := html.LoadAndSplit(context.Background(), split) if err != nil { log.Fatalf("failed to chunk the HTML into documents: %v", err) } fmt.Printf("Successfully chunked the HTML into %v documents.\n", len(docs)) if err := godotenv.Load(); err != nil { log.Fatal("no .env file found") } // Connect to your Atlas cluster uri := os.Getenv("ATLAS_CONNECTION_STRING") if uri == "" { log.Fatal("set your 'ATLAS_CONNECTION_STRING' environment variable.") } clientOptions := options.Client().ApplyURI(uri) client, err := mongo.Connect(ctx, clientOptions) if err != nil { log.Fatalf("failed to connect to the server: %v", err) } defer func() { _ = client.Disconnect(ctx) }() // Set the namespace coll := client.Database("rag_db").Collection("test") fmt.Println("Generating embeddings.") var pageContents []string for i := range docs { pageContents = append(pageContents, docs[i].PageContent) } embeddings := common.GetEmbeddings(pageContents) docsToInsert := make([]interface{}, len(embeddings)) for i := range embeddings { docsToInsert[i] = DocumentToInsert{ PageContent: pageContents[i], Embedding: embeddings[i], } } result, err := coll.InsertMany(ctx, docsToInsert) if err != nil { log.Fatalf("failed to insert documents: %v", err) } fmt.Printf("Successfully inserted %v documents into Atlas\n", len(result.InsertedIDs)) } 次のコマンドを実行して、コードを実行します。
go run ingest-data.go Successfully chunked the HTML into 163 documents. Generating embeddings. Successfully inserted document with id: &{ObjectID("66faffcd60da3f6d4f990fa4")} Successfully inserted document with id: &{ObjectID("66faffce60da3f6d4f990fa5")} ...
Atlas Vector Search を使用してドキュメントを検索します。
このセクションでは、Atlas Vector Search を設定してベクトルデータベースからドキュメントを検索します。 次の手順を実行します。
ベクトル埋め込みに Atlas Vector Search インデックスを作成します。
rag-vector-index.go
という名前の新しいファイルを作成し、次のコードを貼り付けます。 このコードは Atlas クラスターに接続し、rag_db.test
コレクションにvectorSearchタイプのインデックスを作成します。rag-vector-index.gopackage main import ( "context" "log" "os" "time" "go.mongodb.org/mongo-driver/bson" "github.com/joho/godotenv" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { ctx := context.Background() if err := godotenv.Load(); err != nil { log.Fatal("no .env file found") } // Connect to your Atlas cluster uri := os.Getenv("ATLAS_CONNECTION_STRING") if uri == "" { log.Fatal("set your 'ATLAS_CONNECTION_STRING' environment variable.") } clientOptions := options.Client().ApplyURI(uri) client, err := mongo.Connect(ctx, clientOptions) if err != nil { log.Fatalf("failed to connect to the server: %v", err) } defer func() { _ = client.Disconnect(ctx) }() // Specify the database and collection coll := client.Database("rag_db").Collection("test") indexName := "vector_index" opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") type vectorDefinitionField struct { Type string `bson:"type"` Path string `bson:"path"` NumDimensions int `bson:"numDimensions"` Similarity string `bson:"similarity"` } type filterField struct { Type string `bson:"type"` Path string `bson:"path"` } type vectorDefinition struct { Fields []vectorDefinitionField `bson:"fields"` } indexModel := mongo.SearchIndexModel{ Definition: vectorDefinition{ Fields: []vectorDefinitionField{{ Type: "vector", Path: "embedding", NumDimensions: 1024, Similarity: "cosine"}}, }, Options: opts, } log.Println("Creating the index.") searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, indexModel) if err != nil { log.Fatalf("failed to create the search index: %v", err) } // Await the creation of the index. log.Println("Polling to confirm successful index creation.") log.Println("NOTE: This may take up to a minute.") searchIndexes := coll.SearchIndexes() var doc bson.Raw for doc == nil { cursor, err := searchIndexes.List(ctx, options.SearchIndexes().SetName(searchIndexName)) if err != nil { log.Printf("failed to list search indexes: %w", err) } if !cursor.Next(ctx) { break } name := cursor.Current.Lookup("name").StringValue() queryable := cursor.Current.Lookup("queryable").Boolean() if name == searchIndexName && queryable { doc = cursor.Current } else { time.Sleep(5 * time.Second) } } log.Println("Name of Index Created: " + searchIndexName) } 次のコマンドを実行して、インデックスを作成します。
go run rag-vector-index.go 関連データを取得するための関数を定義します。
この手順では、関連するドキュメントを取得するためのクエリを実行する
GetQueryResults
という取得関数を作成します。GetEmbeddings
を使用して、検索クエリから埋め込みを作成します。 次に、クエリを実行してセマンティックで同様のドキュメントを返します。詳細については、「ベクトル検索クエリの実行 」を参照してください。
common
ディレクトリにget-query-results.go
という新しいファイルを作成し、そのファイルに次のコードを貼り付けます。get-クエリ-results.gopackage common import ( "context" "log" "os" "github.com/joho/godotenv" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type TextWithScore struct { PageContent string `bson:"pageContent"` Score float64 `bson:"score"` } func GetQueryResults(query string) []TextWithScore { ctx := context.Background() if err := godotenv.Load(); err != nil { log.Fatal("no .env file found") } // Connect to your Atlas cluster uri := os.Getenv("ATLAS_CONNECTION_STRING") if uri == "" { log.Fatal("set your 'ATLAS_CONNECTION_STRING' environment variable.") } clientOptions := options.Client().ApplyURI(uri) client, err := mongo.Connect(ctx, clientOptions) if err != nil { log.Fatalf("failed to connect to the server: %v", err) } defer func() { _ = client.Disconnect(ctx) }() // Specify the database and collection coll := client.Database("rag_db").Collection("test") queryEmbedding := GetEmbeddings([]string{query}) vectorSearchStage := bson.D{ {"$vectorSearch", bson.D{ {"index", "vector_index"}, {"path", "embedding"}, {"queryVector", queryEmbedding[0]}, {"exact", true}, {"limit", 5}, }}} projectStage := bson.D{ {"$project", bson.D{ {"_id", 0}, {"pageContent", 1}, {"score", bson.D{{"$meta", "vectorSearchScore"}}}, }}} cursor, err := coll.Aggregate(ctx, mongo.Pipeline{vectorSearchStage, projectStage}) if err != nil { log.Fatalf("failed to execute the aggregation pipeline: %v", err) } var results []TextWithScore if err = cursor.All(context.TODO(), &results); err != nil { log.Fatalf("failed to connect unmarshal retrieved documents: %v", err) } return results } データの取得をテストします。
rag-mongodb
プロジェクト ディレクトリに、retrieve-documents-test.go
という新しいファイルを作成します。この手順では、定義した関数が適切な結果を返すことを確認します。このコードを ファイルに貼り付けます。
retrieve-documents-test.gopackage main import ( "fmt" "rag-mongodb/common" // Module that contains the GetQueryResults function ) func main() { query := "AI Technology" documents := common.GetQueryResults(query) for _, doc := range documents { fmt.Printf("Text: %s \nScore: %v \n\n", doc.PageContent, doc.Score) } } 次のコマンドを実行して、コードを実行します。
go run retrieve-documents-test.go Text: for the variety and scale of data required by AI-powered applications. We are confident MongoDB will be a substantial beneficiary of this next wave of application development." Score: 0.835033655166626 Text: "As we look ahead, we continue to be incredibly excited by our large market opportunity, the potential to increase share, and become a standard within more of our customers. We also see a tremendous opportunity to win more legacy workloads, as AI has now become a catalyst to modernize these applications. MongoDB's document-based architecture is particularly well-suited for the variety and Score: 0.8280757665634155 Text: to the use of new and evolving technologies, such as artificial intelligence, in our offerings or partnerships; the growth and expansion of the market for database products and our ability to penetrate that market; our ability to integrate acquired businesses and technologies successfully or achieve the expected benefits of such acquisitions; our ability to maintain the security of our software Score: 0.8165900111198425 Text: MongoDB continues to expand its AI ecosystem with the announcement of the MongoDB AI Applications Program (MAAP), which provides customers with reference architectures, pre-built partner integrations, and professional services to help them quickly build AI-powered applications. Accenture will establish a center of excellence focused on MongoDB projects, and is the first global systems Score: 0.8023912906646729 Text: Bendigo and Adelaide Bank partnered with MongoDB to modernize their core banking technology. With the help of MongoDB Relational Migrator and generative AI-powered modernization tools, Bendigo and Adelaide Bank decomposed an outdated consumer-servicing application into microservices and migrated off its underlying legacy relational database technology significantly faster and more easily than Score: 0.7959681749343872
LLM を使用して応答を生成します。
このセクションでは、検索されたドキュメントをコンテキストとして使用するよう LLM に指示して応答を 生成 します。この例では、先ほど定義した関数を使用して、一致するドキュメントを データベースから検索し、さらに次のことも行います。
Misttal7 B 指示 にアクセスする からのモデル化
プロンプトにユーザーの質問と検索されたドキュメントを含めるようにLLMに指示します。
LLMMongoDBの最新のAI に関する発表について を要求します。
generate-responses.go
という新しいファイルを作成し、次のコードをそのファイルに貼り付けます。generate-responses.gopackage main import ( "context" "fmt" "log" "rag-mongodb/common" // Module that contains the GetQueryResults function "strings" "github.com/tmc/langchaingo/llms" "github.com/tmc/langchaingo/llms/huggingface" "github.com/tmc/langchaingo/prompts" ) func main() { ctx := context.Background() query := "AI Technology" documents := common.GetQueryResults(query) var textDocuments strings.Builder for _, doc := range documents { textDocuments.WriteString(doc.PageContent) } question := "In a few sentences, what are MongoDB's latest AI announcements?" template := prompts.NewPromptTemplate( `Answer the following question based on the given context. Question: {{.question}} Context: {{.context}}`, []string{"question", "context"}, ) prompt, err := template.Format(map[string]any{ "question": question, "context": textDocuments.String(), }) opts := llms.CallOptions{ Model: "mistralai/Mistral-7B-Instruct-v0.3", MaxTokens: 150, Temperature: 0.1, } llm, err := huggingface.New(huggingface.WithModel("mistralai/Mistral-7B-Instruct-v0.3")) if err != nil { log.Fatalf("failed to initialize a Hugging Face LLM: %v", err) } completion, err := llms.GenerateFromSinglePrompt(ctx, llm, prompt, llms.WithOptions(opts)) if err != nil { log.Fatalf("failed to generate a response from the prompt: %v", err) } response := strings.Split(completion, "\n\n") if len(response) == 2 { fmt.Printf("Prompt: %v\n\n", response[0]) fmt.Printf("Response: %v\n", response[1]) } } このコマンドを実行してコードを実行します。生成される応答は異なる場合があります。
go run generate-responses.go Prompt: Answer the following question based on the given context. Question: In a few sentences, what are MongoDB's latest AI announcements? Context: for the variety and scale of data required by AI-powered applications. We are confident MongoDB will be a substantial beneficiary of this next wave of application development.""As we look ahead, we continue to be incredibly excited by our large market opportunity, the potential to increase share, and become a standard within more of our customers. We also see a tremendous opportunity to win more legacy workloads, as AI has now become a catalyst to modernize these applications. MongoDB's document-based architecture is particularly well-suited for the variety andto the use of new and evolving technologies, such as artificial intelligence, in our offerings or partnerships; the growth and expansion of the market for database products and our ability to penetrate that market; our ability to integrate acquired businesses and technologies successfully or achieve the expected benefits of such acquisitions; our ability to maintain the security of our softwareMongoDB continues to expand its AI ecosystem with the announcement of the MongoDB AI Applications Program (MAAP), which provides customers with reference architectures, pre-built partner integrations, and professional services to help them quickly build AI-powered applications. Accenture will establish a center of excellence focused on MongoDB projects, and is the first global systemsBendigo and Adelaide Bank partnered with MongoDB to modernize their core banking technology. With the help of MongoDB Relational Migrator and generative AI-powered modernization tools, Bendigo and Adelaide Bank decomposed an outdated consumer-servicing application into microservices and migrated off its underlying legacy relational database technology significantly faster and more easily than expected. Response: MongoDB's latest AI announcements include the launch of the MongoDB AI Applications Program (MAAP) and a partnership with Accenture to establish a center of excellence focused on MongoDB projects. Additionally, Bendigo and Adelaide Bank have partnered with MongoDB to modernize their core banking technology using MongoDB's AI-powered modernization tools.
Javaプロジェクトを作成し、依存関係をインストールします。
IDE から、Maven または Gradle を使用してJavaプロジェクトを作成します。
パッケージマネージャーに応じて、次の依存関係を追加してください。
Mavenを使用している場合は、プロジェクトの
pom.xml
ファイルのdependencies
配列に次の依存関係を追加し、dependencyManagement
配列にBill of Materials(BOM)を追加してください。pom.xml<dependencies> <!-- MongoDB Java Sync Driver v5.2.0 or later --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> <!-- Java library for Hugging Face models --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-hugging-face</artifactId> </dependency> <!-- Java library for URL Document Loader --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> </dependency> <!-- Java library for ApachePDFBox Document Parser --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-document-parser-apache-pdfbox</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- Bill of Materials (BOM) to manage Java library versions --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-bom</artifactId> <version>0.36.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> Gradle を使用している場合は、プロジェクトの
build.gradle
ファイルのdependencies
配列に次の Bill of Materials(BOM)と依存関係を追加してください。build.groupledependencies { // Bill of Materials (BOM) to manage Java library versions implementation platform('dev.langchain4j:langchain4j-bom:0.36.2') // MongoDB Java Sync Driver v5.2.0 or later implementation 'org.mongodb:mongodb-driver-sync:5.2.0' // Java library for Hugging Face models implementation 'dev.langchain4j:langchain4j-hugging-face' // Java library for URL Document Loader implementation 'dev.langchain4j:langchain4j' // Java library for Apache PDFBox Document Parser implementation 'dev.langchain4j:langchain4j-document-parser-apache-pdfbox' } パッケージマネージャーを実行して、プロジェクトに依存関係をインストールします。
環境変数を設定します。
注意
この例では、 IDE でプロジェクトの変数を設定します。 実稼働アプリケーションでは、配置構成、 CI/CDパイプライン、または シークレット マネージャー を使用して環境変数を管理する場合がありますが、提供されたコードをユースケースに合わせて調整できます。
IDE で新しい構成テンプレートを作成し、次の変数をプロジェクトに追加します 。
IntelliJ IDEA を使用している場合は、新しい Application 実行構成テンプレートを作成し、Environment variables フィールドに変数をセミコロン区切りの値として追加します(例:
FOO=123;BAR=456
)。OKをクリックして、変更を適用します。詳細については、IntelliJ IDEA ドキュメントの「 テンプレートから実行/デバッグ構成を作成する 」セクションを参照してください。
Eclipse を使用している場合は、新しい Java Application 起動構成を作成し、各変数を新しいキーと値のペアとして Environmentタブに追加します。 変更を適用し、OK をクリックします。
詳細については、Eclipse IDE ドキュメントの「 Javaアプリケーション起動構成の作成 」セクションを参照してください。
HUGGING_FACE_ACCESS_TOKEN=<access-token> ATLAS_CONNECTION_STRING=<connection-string>
プレースホルダーを次の値で更新してください。
<access-token>
プレースホルダー値を 1 つのドキュメント アクセス トークンに置き換えます。<connection-string>
プレースホルダー値を、Atlas クラスター SRV 接続文字列に置き換えます。接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
データを解析して分裂するメソッドを定義します。
PDFProcessor.java
という名前のファイルを作成し、次のコードを貼り付けます。
このコードは、次のメソッドを定義します。
parsePDFDocument
メソッドはApache PDFBoxライブラリとLangChain4j URLドキュメントローダーを使用して、指定されたURLのPDFファイルを読み込み、解析します。このメソッドは解析されたPDFをlangchain4jドキュメントとして返します。splitDocument
メソッドは、指定されたlangchain4jドキュメントを、指定されたチャンクサイズ(文字数)とチャンクオーバーラップ(連続するチャンク間で重複する文字数)に従ってチャンクに分割します。このメソッドはテキストセグメントのリストを返します。
import dev.langchain4j.data.document.Document; import dev.langchain4j.data.document.DocumentParser; import dev.langchain4j.data.document.DocumentSplitter; import dev.langchain4j.data.document.loader.UrlDocumentLoader; import dev.langchain4j.data.document.parser.apache.pdfbox.ApachePdfBoxDocumentParser; import dev.langchain4j.data.document.splitter.DocumentByCharacterSplitter; import dev.langchain4j.data.segment.TextSegment; import java.util.List; public class PDFProcessor { /** Parses a PDF document from the specified URL, and returns a * langchain4j Document object. * */ public static Document parsePDFDocument(String url) { DocumentParser parser = new ApachePdfBoxDocumentParser(); return UrlDocumentLoader.load(url, parser); } /** Splits a parsed langchain4j Document based on the specified chunking * parameters, and returns an array of text segments. */ public static List<TextSegment> splitDocument(Document document) { int maxChunkSize = 400; // number of characters int maxChunkOverlap = 20; // number of overlapping characters between consecutive chunks DocumentSplitter splitter = new DocumentByCharacterSplitter(maxChunkSize, maxChunkOverlap); return splitter.split(document); } }
ベクトル埋め込みを生成する方法を定義します。
EmbeddingProvider.java
という名前のファイルを作成し、次のコードを貼り付けます。
このコードでは、mxます。1
複数の入力:
getEmbeddings
メソッドはテキストセグメントの配列 (List<TextSegment>
) を受け入れ、1回のAPI呼び出しで複数の埋め込みを作成することができます。このメソッドは、APIが提供する浮動小数点数の配列をBSONの倍精度浮動小数点数の配列に変換し、Atlasクラスターに保存します。単一入力:
getEmbedding
メソッドは単一のString
を受け入れます。これはベクトルデータに対して実行するクエリを表します。メソッドは、 APIが提供する浮動小数点数の配列を、コレクションをクエリするときに使用する double のBSON配列に変換します 。
import dev.langchain4j.data.embedding.Embedding; import dev.langchain4j.data.segment.TextSegment; import dev.langchain4j.model.huggingface.HuggingFaceChatModel; import dev.langchain4j.model.huggingface.HuggingFaceEmbeddingModel; import dev.langchain4j.model.output.Response; import org.bson.BsonArray; import org.bson.BsonDouble; import java.util.List; import static java.time.Duration.ofSeconds; public class EmbeddingProvider { private static HuggingFaceEmbeddingModel embeddingModel; private static HuggingFaceEmbeddingModel getEmbeddingModel() { if (embeddingModel == null) { String accessToken = System.getenv("HUGGING_FACE_ACCESS_TOKEN"); if (accessToken == null || accessToken.isEmpty()) { throw new RuntimeException("HUGGING_FACE_ACCESS_TOKEN env variable is not set or is empty."); } embeddingModel = HuggingFaceEmbeddingModel.builder() .accessToken(accessToken) .modelId("mixedbread-ai/mxbai-embed-large-v1") .waitForModel(true) .timeout(ofSeconds(60)) .build(); } return embeddingModel; } /** * Returns the Hugging Face chat model interface used by the createPrompt() method * to process queries and generate responses. */ private static HuggingFaceChatModel chatModel; public static HuggingFaceChatModel getChatModel() { String accessToken = System.getenv("HUGGING_FACE_ACCESS_TOKEN"); if (accessToken == null || accessToken.isEmpty()) { throw new IllegalStateException("HUGGING_FACE_ACCESS_TOKEN env variable is not set or is empty."); } if (chatModel == null) { chatModel = HuggingFaceChatModel.builder() .timeout(ofSeconds(25)) .modelId("mistralai/Mistral-7B-Instruct-v0.3") .temperature(0.1) .maxNewTokens(150) .accessToken(accessToken) .waitForModel(true) .build(); } return chatModel; } /** * Takes an array of text segments and returns a BSON array of embeddings to * store in the database. */ public List<BsonArray> getEmbeddings(List<TextSegment> texts) { List<TextSegment> textSegments = texts.stream() .toList(); Response<List<Embedding>> response = getEmbeddingModel().embedAll(textSegments); return response.content().stream() .map(e -> new BsonArray( e.vectorAsList().stream() .map(BsonDouble::new) .toList())) .toList(); } /** * Takes a single string and returns a BSON array embedding to * use in a vector query. */ public static BsonArray getEmbedding(String text) { Response<Embedding> response = getEmbeddingModel().embed(text); return new BsonArray( response.content().vectorAsList().stream() .map(BsonDouble::new) .toList()); } }
Atlas にデータを取り込む方法を定義してください。
DataIngest.java
という名前のファイルを作成し、次のコードを貼り付けます。
このコードはLangChain4jライブラリとMongoDBJava Sync Driverを使用して、ingestサンプルデータをAtlasにLLMがアクセスできないようにします。
具体的には、このコードでは次の処理が行われます。
Atlas クラスターへの接続
以前に定義した
parsePDFDocument
メソッドを使用して、URLからMongoDBの収益報告書PDFファイルを読み込み、解析します。以前に定義された
splitDocument
メソッドを使用して、データをチャンクに分割します。以前に定義した
GetEmbeddings
メソッドを使用して、チャンクされたデータからベクトル埋め込みを作成します。Atlas クラスターの
rag_db.test
コレクションに、チャンク化されたデータと一緒に埋め込みを保存します。DataIngest.javaimport com.mongodb.MongoException; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.result.InsertManyResult; import dev.langchain4j.data.segment.TextSegment; import org.bson.BsonArray; import org.bson.Document; import java.util.ArrayList; import java.util.List; public class DataIngest { public static void main(String[] args) { String uri = System.getenv("ATLAS_CONNECTION_STRING"); if (uri == null || uri.isEmpty()) { throw new RuntimeException("ATLAS_CONNECTION_STRING env variable is not set or is empty."); } // establish connection and set namespace try (MongoClient mongoClient = MongoClients.create(uri)) { MongoDatabase database = mongoClient.getDatabase("rag_db"); MongoCollection<Document> collection = database.getCollection("test"); // parse the PDF file at the specified URL String url = "https://investors.mongodb.com/node/12236/pdf"; String fileName = "mongodb_annual_report.pdf"; System.out.println("Parsing the [" + fileName + "] file from url: " + url); dev.langchain4j.data.document.Document parsedDoc = PDFProcessor.parsePDFDocument(url); // split (or "chunk") the parsed document into text segments List<TextSegment> segments = PDFProcessor.splitDocument(parsedDoc); System.out.println(segments.size() + " text segments created successfully."); // create vector embeddings from the chunked data (i.e. text segments) System.out.println("Creating vector embeddings from the parsed data segments. This may take a few moments."); List<Document> documents = embedText(segments); // insert the embeddings into the Atlas collection try { System.out.println("Ingesting data into the " + collection.getNamespace() + " collection."); insertDocuments(documents, collection); } catch (MongoException me) { throw new RuntimeException("Failed to insert documents", me); } } catch (MongoException me) { throw new RuntimeException("Failed to connect to MongoDB", me); } catch (Exception e) { throw new RuntimeException("Operation failed: ", e); } } /** * Embeds text segments into vector embeddings using the EmbeddingProvider * class and returns a list of BSON documents containing the text and * generated embeddings. */ private static List<Document> embedText(List<TextSegment> segments) { EmbeddingProvider embeddingProvider = new EmbeddingProvider(); List<BsonArray> embeddings = embeddingProvider.getEmbeddings(segments); List<Document> documents = new ArrayList<>(); int i = 0; for (TextSegment segment : segments) { Document doc = new Document("text", segment.text()).append("embedding", embeddings.get(i)); documents.add(doc); i++; } return documents; } /** * Inserts a list of BSON documents into the specified MongoDB collection. */ private static void insertDocuments(List<Document> documents, MongoCollection<Document> collection) { List<String> insertedIds = new ArrayList<>(); InsertManyResult result = collection.insertMany(documents); result.getInsertedIds().values() .forEach(doc -> insertedIds.add(doc.toString())); System.out.println(insertedIds.size() + " documents inserted into the " + collection.getNamespace() + " collection successfully."); } }
埋め込みを生成します。
注意
は額文字モデルを呼び出す場合の 503
Hugging Face モデル ハブ モデルを呼び出すときに、503 エラーが発生する場合があります。この問題を解決するには、少し待ってから再試行します。
DataIngest.java
ファイルを保存して実行します。出力は次のようになります。
Parsing the [mongodb_annual_report.pdf] file from url: https://investors.mongodb.com/node/12236/pdf 72 text segments created successfully. Creating vector embeddings from the parsed data segments. This may take a few moments... Ingesting data into the rag_db.test collection. 72 documents inserted into the rag_db.test collection successfully.
Atlas Vector Search を使用してドキュメントを検索します。
このセクションでは、Atlas Vector Search を設定して検索するためにベクトルデータベースからドキュメントを取得します。
VectorIndex.java
という名前のファイルを作成し、次のコードを貼り付けます。このコードは、以下のインデックス定義を使用してコレクションに Atlas Vector Search インデックスを作成します。
rag_db.test
コレクションのベクトルインデックスタイプにembedding
フィールドをインデックスします。このフィールドには、埋め込みモデルを使用して作成された埋め込みが含まれます。1024
ベクトル次元を強制し、cosine
を使用してベクトル間の類似性を測定します。
VectorIndex.javaimport com.mongodb.MongoException; import com.mongodb.client.ListSearchIndexesIterable; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.SearchIndexModel; import com.mongodb.client.model.SearchIndexType; import org.bson.Document; import org.bson.conversions.Bson; import java.util.Collections; import java.util.List; public class VectorIndex { public static void main(String[] args) { String uri = System.getenv("ATLAS_CONNECTION_STRING"); if (uri == null || uri.isEmpty()) { throw new IllegalStateException("ATLAS_CONNECTION_STRING env variable is not set or is empty."); } // establish connection and set namespace try (MongoClient mongoClient = MongoClients.create(uri)) { MongoDatabase database = mongoClient.getDatabase("rag_db"); MongoCollection<Document> collection = database.getCollection("test"); // define the index details for the index model String indexName = "vector_index"; Bson definition = new Document( "fields", Collections.singletonList( new Document("type", "vector") .append("path", "embedding") .append("numDimensions", 1024) .append("similarity", "cosine"))); SearchIndexModel indexModel = new SearchIndexModel( indexName, definition, SearchIndexType.vectorSearch()); // create the index using the defined model try { List<String> result = collection.createSearchIndexes(Collections.singletonList(indexModel)); System.out.println("Successfully created vector index named: " + result); System.out.println("It may take up to a minute for the index to build before you can query using it."); } catch (Exception e) { throw new RuntimeException(e); } // wait for Atlas to build the index and make it queryable System.out.println("Polling to confirm the index has completed building."); waitForIndexReady(collection, indexName); } catch (MongoException me) { throw new RuntimeException("Failed to connect to MongoDB", me); } catch (Exception e) { throw new RuntimeException("Operation failed: ", e); } } /** * Polls the collection to check whether the specified index is ready to query. */ public static void waitForIndexReady(MongoCollection<Document> collection, String indexName) throws InterruptedException { ListSearchIndexesIterable<Document> searchIndexes = collection.listSearchIndexes(); while (true) { try (MongoCursor<Document> cursor = searchIndexes.iterator()) { if (!cursor.hasNext()) { break; } Document current = cursor.next(); String name = current.getString("name"); boolean queryable = current.getBoolean("queryable"); if (name.equals(indexName) && queryable) { System.out.println(indexName + " index is ready to query"); return; } else { Thread.sleep(500); } } } } } Atlas Vector Search インデックスを作成します。
ファイルを保存して実行します。 出力は次のようになります。
Successfully created a vector index named: [vector_index] Polling to confirm the index has completed building. It may take up to a minute for the index to build before you can query using it. vector_index index is ready to query
LLM を使用して応答を生成するコードを作成してください。
このセクションでは、検索されたドキュメントをコンテキストとして使用するよう LLM に指示して応答を生成します。
LLMPrompt.java
という新しいファイルを作成し、次のコードを貼り付けてください。
このコードでは、次の処理が行われます。
retrieveDocuments
メソッドを使用して、rag_db.test
コレクション内の一致するドキュメントを照会します。このメソッドは、以前に作成した
getEmbedding
メソッドを使用して検索クエリから埋め込みを生成し、そのクエリを実行してセマンティックに類似したドキュメントを返します。詳細については、「ベクトル検索クエリの実行 」を参照してください。
Hugging Face のモデル ハブからミストラル 7B インストラクトモデルにアクセスし、
createPrompt
メソッドを使用してテンプレート化されたプロンプトを作成します。このメソッドは、ユーザーの質問と検索されたドキュメントを定義されたプロンプトに含めるようにLLMに指示します。
MongoDB の最新の AI 発表について LLM にプロンプトを出し、生成された応答を返します。
LLMPrompt.javaimport com.mongodb.MongoException; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.search.FieldSearchPath; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.model.huggingface.HuggingFaceChatModel; import dev.langchain4j.model.input.Prompt; import dev.langchain4j.model.input.PromptTemplate; import org.bson.BsonArray; import org.bson.BsonValue; import org.bson.Document; import org.bson.conversions.Bson; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static com.mongodb.client.model.Aggregates.project; import static com.mongodb.client.model.Aggregates.vectorSearch; import static com.mongodb.client.model.Projections.exclude; import static com.mongodb.client.model.Projections.fields; import static com.mongodb.client.model.Projections.include; import static com.mongodb.client.model.Projections.metaVectorSearchScore; import static com.mongodb.client.model.search.SearchPath.fieldPath; import static com.mongodb.client.model.search.VectorSearchOptions.exactVectorSearchOptions; import static java.util.Arrays.asList; public class LLMPrompt { // User input: the question to answer static String question = "In a few sentences, what are MongoDB's latest AI announcements?"; public static void main(String[] args) { String uri = System.getenv("ATLAS_CONNECTION_STRING"); if (uri == null || uri.isEmpty()) { throw new IllegalStateException("ATLAS_CONNECTION_STRING env variable is not set or is empty."); } // establish connection and set namespace try (MongoClient mongoClient = MongoClients.create(uri)) { MongoDatabase database = mongoClient.getDatabase("rag_db"); MongoCollection<Document> collection = database.getCollection("test"); // generate a response to the user question try { createPrompt(question, collection); } catch (Exception e) { throw new RuntimeException("An error occurred while generating the response: ", e); } } catch (MongoException me) { throw new RuntimeException("Failed to connect to MongoDB ", me); } catch (Exception e) { throw new RuntimeException("Operation failed: ", e); } } /** * Returns a list of documents from the specified MongoDB collection that * match the user's question. * NOTE: Update or omit the projection stage to change the desired fields in the response */ public static List<Document> retrieveDocuments(String question, MongoCollection<Document> collection) { try { // generate the query embedding to use in the vector search BsonArray queryEmbeddingBsonArray = EmbeddingProvider.getEmbedding(question); List<Double> queryEmbedding = new ArrayList<>(); for (BsonValue value : queryEmbeddingBsonArray.stream().toList()) { queryEmbedding.add(value.asDouble().getValue()); } // define the pipeline stages for the vector search index String indexName = "vector_index"; FieldSearchPath fieldSearchPath = fieldPath("embedding"); int limit = 5; List<Bson> pipeline = asList( vectorSearch( fieldSearchPath, queryEmbedding, indexName, limit, exactVectorSearchOptions()), project( fields( exclude("_id"), include("text"), metaVectorSearchScore("score")))); // run the query and return the matching documents List<Document> matchingDocuments = new ArrayList<>(); collection.aggregate(pipeline).forEach(matchingDocuments::add); return matchingDocuments; } catch (Exception e) { System.err.println("Error occurred while retrieving documents: " + e.getMessage()); return new ArrayList<>(); } } /** * Creates a templated prompt from a submitted question string and any retrieved documents, * then generates a response using the Hugging Face chat model. */ public static void createPrompt(String question, MongoCollection<Document> collection) { // retrieve documents matching the user's question List<Document> retrievedDocuments = retrieveDocuments(question, collection); if (retrievedDocuments.isEmpty()) { System.out.println("No relevant documents found. Unable to generate a response."); return; } else System.out.println("Generating a response from the retrieved documents. This may take a few moments."); // define a prompt template HuggingFaceChatModel huggingFaceChatModel = EmbeddingProvider.getChatModel(); PromptTemplate promptBuilder = PromptTemplate.from(""" Answer the following question based on the given context: Question: {{question}} Context: {{information}} ------- """); // build the information string from the retrieved documents StringBuilder informationBuilder = new StringBuilder(); for (Document doc : retrievedDocuments) { String text = doc.getString("text"); informationBuilder.append(text).append("\n"); } Map<String, Object> variables = new HashMap<>(); variables.put("question", question); variables.put("information", informationBuilder); // generate and output the response from the chat model Prompt prompt = promptBuilder.apply(variables); AiMessage response = huggingFaceChatModel.generate(prompt.toUserMessage()).content(); // extract the generated text to output a formatted response String responseText = response.text(); String marker = "-------"; int markerIndex = responseText.indexOf(marker); String generatedResponse; if (markerIndex != -1) { generatedResponse = responseText.substring(markerIndex + marker.length()).trim(); } else { generatedResponse = responseText; // else fallback to the full response } // output the question and formatted response System.out.println("Question:\n " + question); System.out.println("Response:\n " + generatedResponse); // output the filled-in prompt and context information for demonstration purposes System.out.println("\n" + "---- Prompt Sent to LLM ----"); System.out.println(prompt.text() + "\n"); } }
LLM を使用して応答を生成します。
ファイルを保存して実行します。 出力は次のようになりますが、生成される応答は異なる場合があることにご注意ください。
Generating a response from the retrieved documents. This may take a few moments. Question: In a few sentences, what are MongoDB's latest AI announcements? Response: MongoDB's latest AI announcements include the MongoDB AI Applications Program (MAAP), which provides customers with reference architectures, pre-built partner integrations, and professional services to help them quickly build AI-powered applications. Accenture will establish a center of excellence focused on MongoDB projects. These announcements highlight MongoDB's growing focus on AI application development and its potential to modernize legacy workloads. ---- Prompt Sent to LLM ---- Answer the following question based on the given context: Question: In a few sentences, what are MongoDB's latest AI announcements? Context: time data. MongoDB continues to expand its AI ecosystem with the announcement of the MongoDB AI Applications Program (MAAP), which provides customers with reference architectures, pre-built partner integrations, and professional services to help them quickly build AI-powered applications. Accenture will establish a center of excellence focused on MongoDB projects, and is the first global systems i ighlights MongoDB announced a number of new products and capabilities at MongoDB.local NYC. Highlights included the preview of MongoDB 8.0—with significant performance improvements such as faster reads and updates, along with significantly faster bulk inserts and time series queries—and the general availability of Atlas Stream Processing to build sophisticated, event-driven applications with real- ble future as well as the criticality of MongoDB to artificial intelligence application development. These forward-looking statements include, but are not limited to, plans, objectives, expectations and intentions and other statements contained in this press release that are not historical facts and statements identified by words such as "anticipate," "believe," "continue," "could," "estimate," "e ve Officer of MongoDB. "As we look ahead, we continue to be incredibly excited by our large market opportunity, the potential to increase share, and become a standard within more of our customers. We also see a tremendous opportunity to win more legacy workloads, as AI has now become a catalyst to modernize these applications. MongoDB's document-based architecture is particularly well-suited for t ictable, impact on its future GAAP financial results. Conference Call Information MongoDB will host a conference call today, May 30, 2024, at 5:00 p.m. (Eastern Time) to discuss its financial results and business outlook. A live webcast of the call will be available on the "Investor Relations" page of MongoDB's website at https://investors.mongodb.com. To access the call by phone, please go to thi
環境を設定します。
Node.js プロジェクトを初期化します。
ターミナルで次のコマンドを実行して、
rag-mongodb
という名前の新しいディレクトリを作成し、プロジェクトを初期化します。mkdir rag-mongodb cd rag-mongodb npm init -y 依存関係をインストールしてインポートします。
次のコマンドを実行します:
npm install mongodb langchain @langchain/community @xenova/transformers @huggingface/inference pdf-parse package.json
ファイルを更新します。プロジェクトの
package.json
ファイルで、次の例に示すようにtype
フィールドを指定し、ファイルを保存します。{ "name": "rag-mongodb", "type": "module", ... .env
ファイルを作成します。プロジェクトで、 Atlas 接続文字列と Hugeface アクセス トークンを保存するための
.env
ファイルを作成します。HUGGING_FACE_ACCESS_TOKEN = "<access-token>" ATLAS_CONNECTION_STRING = "<connection-string>" <access-token>
プレースホルダー値を 1 つのドキュメント アクセス トークンに置き換えます。<connection-string>
プレースホルダー値を、Atlas クラスター SRV 接続文字列に置き換えます。接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net 注意
Node.js の最低バージョン要件
Node.js v20.x は、
--env-file
オプションを導入しました。古いバージョンの Node.js を使用している場合は、dotenv
パッケージをプロジェクトに追加するか、別の方法を使用して環境変数を管理します。
ベクトル埋め込みを生成する関数を作成します。
このセクションでは、次の関数を作成します。
noomic- embedded-text-v1 を ロードする 埋め込みモデルを使用する。
入力データからベクトル埋め込みを作成します。
プロジェクトにget-embeddings.js
というファイルを作成し、次のコードを貼り付けます。
import { pipeline } from '@xenova/transformers'; // Function to generate embeddings for a given data source export async function getEmbedding(data) { const embedder = await pipeline( 'feature-extraction', 'Xenova/nomic-embed-text-v1'); const results = await embedder(data, { pooling: 'mean', normalize: true }); return Array.from(results.data); }
Atlas にデータを取り込みます。
このセクションでは、 がアクセスできないサンプル データを に 取り込みAtlas LLMます。次のコードでは、 Lgachein 統合とNode.js ドライバーを使用して次の処理を実行します。
データをチャンクに分割し、チャンク サイズ(文字数)とチャンクの重複(連続するチャンク間で重複する文字数)を指定します。
定義した
getEmbedding
関数を使用して、チャンク データからベクトル埋め込みを作成します。これらの埋め込みを、Atlas クラスターの
rag_db.test
コレクション内のチャンク データと一緒に保存します。
プロジェクトにingest-data.js
というファイルを作成し、次のコードを貼り付けます。
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf"; import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; import { MongoClient } from 'mongodb'; import { getEmbedding } from './get-embeddings.js'; import * as fs from 'fs'; async function run() { const client = new MongoClient(process.env.ATLAS_CONNECTION_STRING); try { // Save online PDF as a file const rawData = await fetch("https://investors.mongodb.com/node/12236/pdf"); const pdfBuffer = await rawData.arrayBuffer(); const pdfData = Buffer.from(pdfBuffer); fs.writeFileSync("investor-report.pdf", pdfData); const loader = new PDFLoader(`investor-report.pdf`); const data = await loader.load(); // Chunk the text from the PDF const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 400, chunkOverlap: 20, }); const docs = await textSplitter.splitDocuments(data); console.log(`Successfully chunked the PDF into ${docs.length} documents.`); // Connect to your Atlas cluster await client.connect(); const db = client.db("rag_db"); const collection = db.collection("test"); console.log("Generating embeddings and inserting documents..."); const insertDocuments = []; await Promise.all(docs.map(async doc => { // Generate embeddings using the function that you defined const embedding = await getEmbedding(doc.pageContent); // Add the document with the embedding to array of documents for bulk insert insertDocuments.push({ document: doc, embedding: embedding }); })) // Continue processing documents if an error occurs during an operation const options = { ordered: false }; // Insert documents with embeddings into Atlas const result = await collection.insertMany(insertDocuments, options); console.log("Count of documents inserted: " + result.insertedCount); } catch (err) { console.log(err.stack); } finally { await client.close(); } } run().catch(console.dir);
次に、次のコマンドを実行してコードを実行します。
node --env-file=.env ingest-data.js
Generating embeddings and inserting documents... Count of documents inserted: 86
Tip
このコードの実行には時間がかかります。 Atlas UI でrag_db.test
コレクションに移動すると、挿入されたベクトル埋め込みを表示できます。
Atlas Vector Search を使用してドキュメントを検索します。
このセクションでは、Atlas Vector Search を設定してベクトルデータベースからドキュメントを検索します。 次の手順を実行します。
ベクトル埋め込みに Atlas Vector Search インデックスを作成します。
rag-vector-index.js
という名前の新しいファイルを作成し、次のコードを貼り付けます。 このコードは Atlas クラスターに接続し、rag_db.test
コレクションにvectorSearchタイプのインデックスを作成します。import { MongoClient } from 'mongodb'; // Connect to your Atlas cluster const client = new MongoClient(process.env.ATLAS_CONNECTION_STRING); async function run() { try { const database = client.db("rag_db"); const collection = database.collection("test"); // Define your Atlas Vector Search index const index = { name: "vector_index", type: "vectorSearch", definition: { "fields": [ { "type": "vector", "numDimensions": 768, "path": "embedding", "similarity": "cosine" } ] } } // Call the method to create the index const result = await collection.createSearchIndex(index); console.log(result); } finally { await client.close(); } } run().catch(console.dir); 次に、次のコマンドを実行してコードを実行します。
node --env-file=.env rag-vector-index.js 関連データを取得するための関数を定義します。
retrieve-documents.js
という新しいファイルを作成します。この手順では、クエリを実行して関連するドキュメントを取得する
getQueryResults
getEmbedding
という取得関数を作成します。関数を使用して、検索クエリーから埋め込みを作成します。次に、クエリを実行してセマンティックで同様のドキュメントを返します。詳細については、「ベクトル検索クエリの実行 」を参照してください。
このコードを ファイルに貼り付けます。
import { MongoClient } from 'mongodb'; import { getEmbedding } from './get-embeddings.js'; // Function to get the results of a vector query export async function getQueryResults(query) { // Connect to your Atlas cluster const client = new MongoClient(process.env.ATLAS_CONNECTION_STRING); try { // Get embedding for a query const queryEmbedding = await getEmbedding(query); await client.connect(); const db = client.db("rag_db"); const collection = db.collection("test"); const pipeline = [ { $vectorSearch: { index: "vector_index", queryVector: queryEmbedding, path: "embedding", exact: true, limit: 5 } }, { $project: { _id: 0, document: 1, } } ]; // Retrieve documents from Atlas using this Vector Search query const result = collection.aggregate(pipeline); const arrayOfQueryDocs = []; for await (const doc of result) { arrayOfQueryDocs.push(doc); } return arrayOfQueryDocs; } catch (err) { console.log(err.stack); } finally { await client.close(); } } データの取得をテストします。
retrieve-documents-test.js
という新しいファイルを作成します。 この手順では、定義した関数が関連する結果を返すことを確認します。このコードを ファイルに貼り付けます。
import { getQueryResults } from './retrieve-documents.js'; async function run() { try { const query = "AI Technology"; const documents = await getQueryResults(query); documents.forEach( doc => { console.log(doc); }); } catch (err) { console.log(err.stack); } } run().catch(console.dir); 次に、次のコマンドを実行してコードを実行します。
node --env-file=.env retrieve-documents-test.js { document: { pageContent: 'MongoDB continues to expand its AI ecosystem with the announcement of the MongoDB AI Applications Program (MAAP),', metadata: { source: 'investor-report.pdf', pdf: [Object], loc: [Object] }, id: null } } { document: { pageContent: 'artificial intelligence, in our offerings or partnerships; the growth and expansion of the market for database products and our ability to penetrate that\n' + 'market; our ability to integrate acquired businesses and technologies successfully or achieve the expected benefits of such acquisitions; our ability to', metadata: { source: 'investor-report.pdf', pdf: [Object], loc: [Object] }, id: null } } { document: { pageContent: 'more of our customers. We also see a tremendous opportunity to win more legacy workloads, as AI has now become a catalyst to modernize these\n' + "applications. MongoDB's document-based architecture is particularly well-suited for the variety and scale of data required by AI-powered applications. \n" + 'We are confident MongoDB will be a substantial beneficiary of this next wave of application development."', metadata: { source: 'investor-report.pdf', pdf: [Object], loc: [Object] }, id: null } } { document: { pageContent: 'which provides customers with reference architectures, pre-built partner integrations, and professional services to help\n' + 'them quickly build AI-powered applications. Accenture will establish a center of excellence focused on MongoDB projects,\n' + 'and is the first global systems integrator to join MAAP.', metadata: { source: 'investor-report.pdf', pdf: [Object], loc: [Object] }, id: null } } { document: { pageContent: 'Bendigo and Adelaide Bank partnered with MongoDB to modernize their core banking technology. With the help of\n' + 'MongoDB Relational Migrator and generative AI-powered modernization tools, Bendigo and Adelaide Bank decomposed an\n' + 'outdated consumer-servicing application into microservices and migrated off its underlying legacy relational database', metadata: { source: 'investor-report.pdf', pdf: [Object], loc: [Object] }, id: null } }
LLM を使用して応答を生成します。
このセクションでは、検索されたドキュメントをコンテキストとして使用するよう LLM に指示して応答を 生成 します。この例では、先ほど定義した関数を使用して、一致するドキュメントを データベースから検索し、さらに次のことも行います。
Misttal7 B 指示 にアクセスする からのモデル化
プロンプトにユーザーの質問と検索されたドキュメントを含めるようにLLMに指示します。
LLMMongoDBの最新のAI に関する発表について を要求します。
generate-responses.js
という新しいファイルを作成し、次のコードをそのファイルに貼り付けます。
import { getQueryResults } from './retrieve-documents.js'; import { HfInference } from '@huggingface/inference' async function run() { try { // Specify search query and retrieve relevant documents const query = "AI Technology"; const documents = await getQueryResults(query); // Build a string representation of the retrieved documents to use in the prompt let textDocuments = ""; documents.forEach(doc => { textDocuments += doc.document.pageContent; }); const question = "In a few sentences, what are MongoDB's latest AI announcements?"; // Create a prompt consisting of the question and context to pass to the LLM const prompt = `Answer the following question based on the given context. Question: {${question}} Context: {${textDocuments}} `; // Connect to Hugging Face, using the access token from the environment file const hf = new HfInference(process.env.HUGGING_FACE_ACCESS_TOKEN); const llm = hf.endpoint( "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" ); // Prompt the LLM to answer the question using the // retrieved documents as the context const output = await llm.chatCompletion({ model: "mistralai/Mistral-7B-Instruct-v0.2", messages: [{ role: "user", content: prompt }], max_tokens: 150, }); // Output the LLM's response as text. console.log(output.choices[0].message.content); } catch (err) { console.log(err.stack); } } run().catch(console.dir);
次に、このコマンドを実行してコードを実行します。 生成される応答は異なる場合があります。
node --env-file=.env generate-responses.js
MongoDB's latest AI announcements include the launch of the MongoDB AI Applications Program (MAAP), which provides customers with reference architectures, pre-built partner integrations, and professional services to help them build AI-powered applications quickly. Accenture has joined MAAP as the first global systems integrator, establishing a center of excellence focused on MongoDB projects. Additionally, Bendigo and Adelaide Bank have partnered with MongoDB to modernize their core banking technology using MongoDB's Relational Migrator and generative AI-powered modernization tools.
Atlas にデータを取り込みます。
このセクションでは、LLM がアクセスできないサンプルデータを Atlas に取り込みます。ノートブックに次の各コード スニペットを貼り付けて実行します。
ベクトル埋め込みを生成する関数を定義する。
このコードを実行して、オープンソースの埋め込みモデルを使用してベクトル埋め込みを生成する関数を作成します。具体的には、このコードでは次の処理が行われます。
Sentence Transformers から nomic-embed-text-v1 埋め込みモデルをロードします。
モデルを使用して特定のテキスト入力の埋め込みを生成する、
get_embedding
という名前の関数を作成します。
from sentence_transformers import SentenceTransformer # Load the embedding model (https://huggingface.co/nomic-ai/nomic-embed-text-v1") model = SentenceTransformer("nomic-ai/nomic-embed-text-v1", trust_remote_code=True) # Define a function to generate embeddings def get_embedding(data): """Generates vector embeddings for the given data.""" embedding = model.encode(data) return embedding.tolist() データをロードして分割します。
このコードを実行すると、LangChain 統合を使用してサンプル データをロードし、分割できます。具体的には、このコードでは次の処理が行われます。
MongoDB の収益レポートを含む PDF をロードします。
チャンクサイズ(文字数)とチャンクのオーバーラップ(連続するチャンク間で重なり合う文字数)を指定して、データをチャンクに分割します。
from langchain_community.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter # Load the PDF loader = PyPDFLoader("https://investors.mongodb.com/node/12236/pdf") data = loader.load() # Split the data into chunks text_splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=20) documents = text_splitter.split_documents(data) データをベクトル埋め込みに変換します。
このコードを実行して、対応するベクトルが埋め込まれたドキュメントのリストを作成し、チャンク化されたドキュメントを取り込む準備をします。これらの埋め込みは、先ほど定義した
get_embedding
関数を使用して生成します。# Prepare documents for insertion docs_to_insert = [{ "text": doc.page_content, "embedding": get_embedding(doc.page_content) } for doc in documents] データと埋め込みを Atlas に保存する
次のコードを実行して、埋め込みを含むドキュメントを Atlas クラスターの
rag_db.test
コレクションに挿入します。コードを実行する前に、<connection-string>
を Atlas 接続文字列に置き換えます。from pymongo import MongoClient # Connect to your Atlas cluster client = MongoClient("<connection-string>") collection = client["rag_db"]["test"] # Insert documents into the collection result = collection.insert_many(docs_to_insert) Tip
コードの実行後、クラスター内の
rag_db.test
コレクションに移動すると、Atlas UI でベクトル埋め込みを表示できます。
Atlas Vector Search を使用してドキュメントを検索します。
このセクションでは、Atlas Vector Search を使用して検索システムを作成し、ベクトルデータベースから関連するドキュメントを取得します。ノートブックに次の各コード スニペットを貼り付けて実行します。
ベクトル埋め込みに Atlas Vector Search インデックスを作成します。
PyMongo ドライバーを使用してアプリケーションから直接インデックスを作成するには、次のコードを実行します。このコードには、インデックスが使用可能かどうかを確認するポーリング メカニズムも含まれています。
詳細については、「ベクトル検索のフィールドにインデックスを作成する方法 」を参照してください。
from pymongo.operations import SearchIndexModel import time # Create your index model, then create the search index index_name="vector_index" search_index_model = SearchIndexModel( definition = { "fields": [ { "type": "vector", "numDimensions": 768, "path": "embedding", "similarity": "cosine" } ] }, name = index_name, type = "vectorSearch" ) collection.create_search_index(model=search_index_model) # Wait for initial sync to complete print("Polling to check if the index is ready. This may take up to a minute.") predicate=None if predicate is None: predicate = lambda index: index.get("queryable") is True while True: indices = list(collection.list_search_indexes(index_name)) if len(indices) and predicate(indices[0]): break time.sleep(5) print(index_name + " is ready for querying.") ベクトル検索クエリを実行するための関数を定義します。
このコードを実行して、基本的なベクトル検索クエリを実行する
get_query_results
という検索関数を作成します。get_embedding
関数を使用して、検索クエリから埋め込みを作成します。次に、クエリを実行して、セマンティックに類似したドキュメントを返します。詳細については、「ベクター検索クエリの実行」を参照してください。
# Define a function to run vector search queries def get_query_results(query): """Gets results from a vector search query.""" query_embedding = get_embedding(query) pipeline = [ { "$vectorSearch": { "index": "vector_index", "queryVector": query_embedding, "path": "embedding", "exact": True, "limit": 5 } }, { "$project": { "_id": 0, "text": 1 } } ] results = collection.aggregate(pipeline) array_of_results = [] for doc in results: array_of_results.append(doc) return array_of_results # Test the function with a sample query import pprint pprint.pprint(get_query_results("AI technology")) [{'text': 'more of our customers. We also see a tremendous opportunity to win ' 'more legacy workloads, as AI has now become a catalyst to modernize ' 'these\n' "applications. MongoDB's document-based architecture is " 'particularly well-suited for the variety and scale of data required ' 'by AI-powered applications.'}, {'text': 'artificial intelligence, in our offerings or partnerships; the ' 'growth and expansion of the market for database products and our ' 'ability to penetrate that\n' 'market; our ability to integrate acquired businesses and ' 'technologies successfully or achieve the expected benefits of such ' 'acquisitions; our ability to'}, {'text': 'MongoDB continues to expand its AI ecosystem with the announcement ' 'of the MongoDB AI Applications Program (MAAP),'}, {'text': 'which provides customers with reference architectures, pre-built ' 'partner integrations, and professional services to help\n' 'them quickly build AI-powered applications. Accenture will ' 'establish a center of excellence focused on MongoDB projects,\n' 'and is the first global systems integrator to join MAAP.'}, {'text': 'Bendigo and Adelaide Bank partnered with MongoDB to modernize ' 'their core banking technology. With the help of\n' 'MongoDB Relational Migrator and generative AI-powered modernization ' 'tools, Bendigo and Adelaide Bank decomposed an\n' 'outdated consumer-servicing application into microservices and ' 'migrated off its underlying legacy relational database'}]
LLM を使用して応答を生成します。
このセクションでは、検索されたドキュメントをコンテキストとして使用するよう LLM に指示して応答を生成します。
次のコードの <token>
を Hugging Face アクセス トークンに置き換え、ノートブックでコードを実行します。このコードは、次の処理を行います。
定義した
get_query_results
関数を使用して、Atlas から関連するドキュメントを検索します。ユーザーの質問と検索されたドキュメントをコンテキストとして、プロンプトを作成します。
Misttal7 B 指示 にアクセスする からのモデル化
LLM に MongoDB の最新の AI に関する発表を支持します。生成される応答は異なる場合があります。
import os from huggingface_hub import InferenceClient # Specify search query, retrieve relevant documents, and convert to string query = "What are MongoDB's latest AI announcements?" context_docs = get_query_results(query) context_string = " ".join([doc["text"] for doc in context_docs]) # Construct prompt for the LLM using the retrieved documents as the context prompt = f"""Use the following pieces of context to answer the question at the end. {context_string} Question: {query} """ # Authenticate to Hugging Face and access the model os.environ["HF_TOKEN"] = "<token>" llm = InferenceClient( "mistralai/Mistral-7B-Instruct-v0.3", token = os.getenv("HF_TOKEN")) # Prompt the LLM (this code varies depending on the model you use) output = llm.chat_completion( messages=[{"role": "user", "content": prompt}], max_tokens=150 ) print(output.choices[0].message.content)
MongoDB's latest AI announcements include the MongoDB AI Applications Program (MAAP), a program designed to help customers build AI-powered applications more efficiently. Additionally, they have announced significant performance improvements in MongoDB 8.0, featuring faster reads, updates, bulk inserts, and time series queries. Another announcement is the general availability of Atlas Stream Processing to build sophisticated, event-driven applications with real-time data.
次のステップ
より詳細なRAGチュートリアルについては、以下のリソースをご覧ください。
RAG一般的なLLM フレームワークとAI サービスを使用して を実装する方法については、「 ベクトル検索と テクノロジーの統合AI 」を参照してください。
ローカル Atlas 配置とローカルモデルを使用して RAG を実装する方法については、Atlas ベクトル検索を使用したローカル RAG 実装のビルドを参照してください。
ユースケースに基づくチュートリアルとインタラクティブ Python ノートについては、「 生成系 AI ユースケースのリポジトリ 」を参照してください。
Atlas Vector Search で本番環境に対応できるチャットボットの構築を開始するには、 MongoDB チャットボット フレームワーク を使用できます 。このフレームワークは、AI チャットボット アプリケーションを迅速に構築できるライブラリのセットを提供します。
微調整
RG アプリケーションを最適化および微調整するには、「 クエリ結果の精度を測定し、ベクトル検索のパフォーマンスを向上させる方法 」を参照してください。
また、さまざまな埋め込みモデル、チャンク戦略、 LM を試すこともできます。詳しくは、次のリソースを参照してください。
さらに、Atlas Vector Search は高度な検索システムをサポートします。 Atlas ではベクトル データを他のデータとともにシームレスにインデックス化できるため、コレクション内の他のフィールドで事前にフィルタリングするか、セマンティック検索と全文検索結果を組み合わせたハイブリッド検索を実行することで、検索結果を微調整できます。