Notebooks
M
Mistral AI
Azure Ai Search Rag

Azure Ai Search Rag

Azure_AI_Searchmistral-cookbookthird_party

RAG with Mistral AI, Azure AI Search and Azure AI Studio

Overview

This notebook demonstrates how to integrate Mistral Embeddings with Azure AI Search as a vector store, and use the results to ground responses in the Mistral Chat Completion Model.

Prerequisites

  • Mistral AI API Key OR Azure AI Studio Deployed Mistral Chat Completion Model and Azure AI Studio API Key
  • Azure AI Search service
  • Python 3.x environment with necessary libraries installed

Steps

  1. Install required packages
  2. Load data and generate Mistral embeddings
  3. Index embeddings in Azure AI Search
  4. Perform search using Azure AI Search
  5. Ground search results in Mistral Chat Completion Model

Install Required Packages

[ ]

Load Data and Generate Mistral Embeddings

[ ]

We have 10K chunks, where each chunk is roughly the length of 1-2 paragraphs in length. Here is an example of a single record:

[3]
{'id': '2401.04088#0',
, 'title': 'Mixtral of Experts',
, 'content': '4 2 0 2 n a J 8 ] G L . s c [ 1 v 8 8 0 4 0 . 1 0 4 2 : v i X r a # Mixtral of Experts Albert Q. Jiang, Alexandre Sablayrolles, Antoine Roux, Arthur Mensch, Blanche Savary, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Emma Bou Hanna, Florian Bressand, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Lélio Renard Lavaud, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Sandeep Subramanian, Sophia Yang, Szymon Antoniak, Teven Le Scao, Théophile Gervet, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed Abstract We introduce Mixtral 8x7B, a Sparse Mixture of Experts (SMoE) language model. Mixtral has the same architecture as Mistral 7B, with the difference that each layer is composed of 8 feedforward blocks (i.e. experts). For every token, at each layer, a router network selects two experts to process the current state and combine their outputs. Even though each token only sees two experts, the selected experts can be different at each timestep. As a result, each token has access to 47B parameters, but only uses 13B active parameters during inference. Mixtral was trained with a context size of 32k tokens and it outperforms or matches Llama 2 70B and GPT-3.5 across all evaluated benchmarks. In particular, Mixtral vastly outperforms Llama 2 70B on mathematics, code generation, and multilingual benchmarks. We also provide a model fine- tuned to follow instructions, Mixtral 8x7B â Instruct, that surpasses GPT-3.5 Turbo, Claude-2.1, Gemini Pro, and Llama 2 70B â chat model on human bench- marks. Both the base and instruct models are released under the Apache 2.0 license.',
, 'prechunk_id': '',
, 'postchunk_id': '2401.04088#1',
, 'arxiv_id': '2401.04088',
, 'references': ['1905.07830']}

Format the data into the format we need, this will contain id, title, content (which we will embed), and arxiv_id.

[4]
Dataset({
,    features: ['id', 'title', 'content', 'arxiv_id'],
,    num_rows: 10000
,})

We need to define an embedding model to create our embedding vectors for retrieval, for that we will be using Mistral AI's mistral-embed. There is some cost associated with this model, so be aware of that (costs for running this notebook are <$1).

[12]
[13]

We can view the dimensionality of our returned embeddings, which we'll need soon when initializing our vector index:

[14]
1024

Index Embeddings into Azure AI Search

Now we create our vector DB to store our vectors. For this, we need to set up an Azure AI Search service.

There are two ways to authenticate to Azure AI Search:

  1. Service Key: The service key can be found in the "Settings -> Keys" section in the left navbar of the Azure portal dashboard. Make sure to select the ADMIN key.
  2. Managed Identity: Using Microsoft Entra ID (f.k.a. Azure Active Directory) is a more secure and recommended way to authenticate. You can follow the instructions in the official Microsoft documentation to set up Managed Identity.

For more detailed instructions on creating an Azure AI Search service, please refer to the official Microsoft documentation.

Authenticate into Azure AI Search

[10]
Using AAD for authentication.

Create a vector index

[11]
ai-arxiv2-semantic-chunks created

Estimate Cost for Embedding Generation

As per the information from Lunary.ai's Mistral Tokenizer, one token is approximately equivalent to five characters of text.

According to Mistral's Pricing, the cost for using mistral-embed is $0.1 per 1M tokens for both inputs and outputs.

In the following code block, we will calculate the estimated cost for generating embeddings based on the size of our dataset and these pricing details.

[15]
Estimated cost for generating embeddings: $0.19047898000000002

Transform Dataset for Azure AI Search Upload

[17]

Generate Embeddings

[19]

Azure AI Search doesn't allow certain unsafe keys so we'll base64 encode id here

[20]

Upload Documents

[44]
Uploaded 10000 documents in total

Perform a Vector Search

[21]
ID: MjMxMC4wNjgyNSMw
Arxiv ID: 2310.06825
Title: Mistral 7B
Score: 0.79391503
Content: 3 2 0 2 t c O 0 1 ] L C . s c [ 1 v 5 2 8 6 0 . 0 1 3 2 : v i X r a # Mistral 7B Albert Q. Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lucile Saulnier, Lélio Renard Lavaud, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed Abstract We introduce Mistral 7B, a 7â billion-parameter language model engineered for superior performance and efficiency. Mistral 7B outperforms the best open 13B model (Llama 2) across all evaluated benchmarks, and the best released 34B model (Llama 1) in reasoning, mathematics, and code generation. Our model leverages grouped-query attention (GQA) for faster inference, coupled with sliding window attention (SWA) to effectively handle sequences of arbitrary length with a reduced inference cost. We also provide a model fine-tuned to follow instructions, Mistral 7B â Instruct, that surpasses Llama 2 13B â chat model both on human and automated benchmarks. Our models are released under the Apache 2.0 license. Code: https://github.com/mistralai/mistral-src Webpage: https://mistral.ai/news/announcing-mistral-7b/ # Introduction In the rapidly evolving domain of Natural Language Processing (NLP), the race towards higher model performance often necessitates an escalation in model size. However, this scaling tends to increase computational costs and inference latency, thereby raising barriers to deployment in practical, real-world scenarios. In this context, the search for balanced models delivering both high-level performance and efficiency becomes critically essential. Our model, Mistral 7B, demonstrates that a carefully designed language model can deliver high performance while maintaining an efficient inference.
--------------------------------------------------
ID: MjMxMC4wNjgyNSMx
Arxiv ID: 2310.06825
Title: Mistral 7B
Score: 0.7921863
Content: Mistral 7B outperforms the previous best 13B model (Llama 2, [26]) across all tested benchmarks, and surpasses the best 34B model (LLaMa 34B, [25]) in mathematics and code generation. Furthermore, Mistral 7B approaches the coding performance of Code-Llama 7B [20], without sacrificing performance on non-code related benchmarks. Mistral 7B leverages grouped-query attention (GQA) [1], and sliding window attention (SWA) [6, 3]. GQA significantly accelerates the inference speed, and also reduces the memory requirement during decoding, allowing for higher batch sizes hence higher throughput, a crucial factor for real-time applications. In addition, SWA is designed to handle longer sequences more effectively at a reduced computational cost, thereby alleviating a common limitation in LLMs. These attention mechanisms collectively contribute to the enhanced performance and efficiency of Mistral 7B. Mistral 7B is released under the Apache 2.0 license. This release is accompanied by a reference implementation1 facilitating easy deployment either locally or on cloud platforms such as AWS, GCP, or Azure using the vLLM [17] inference server and SkyPilot 2. Integration with Hugging Face 3 is also streamlined for easier integration. Moreover, Mistral 7B is crafted for ease of fine-tuning across a myriad of tasks. As a demonstration of its adaptability and superior performance, we present a chat model fine-tuned from Mistral 7B that significantly outperforms the Llama 2 13B â Chat model. Mistral 7B takes a significant step in balancing the goals of getting high performance while keeping large language models efficient. Through our work, our aim is to help the community create more affordable, efficient, and high-performing language models that can be used in a wide range of real-world applications.
--------------------------------------------------
ID: MjQwMS4wNDA4OCMx
Arxiv ID: 2401.04088
Title: Mixtral of Experts
Score: 0.7851202
Content: Code: https://github.com/mistralai/mistral-src Webpage: https://mistral.ai/news/mixtral-of-experts/ # Introduction In this paper, we present Mixtral 8x7B, a sparse mixture of experts model (SMoE) with open weights, licensed under Apache 2.0. Mixtral outperforms Llama 2 70B and GPT-3.5 on most benchmarks. As it only uses a subset of its parameters for every token, Mixtral allows faster inference speed at low batch-sizes, and higher throughput at large batch-sizes. Mixtral is a sparse mixture-of-experts network. It is a decoder-only model where the feedforward block picks from a set of 8 distinct groups of parameters. At every layer, for every token, a router network chooses two of these groups (the â
--------------------------------------------------

Ground retrieved results from Azure AI Search to Mistral-Large LLM

[22]
Mistral AI is headquartered in Paris, France.

Ground Results to Mistral-Large hosted in Azure AI Studio

[25]
Mistral AI is headquartered in Paris, France.