### Install necessary libraries Source: https://github.com/mistralai/cookbook/blob/main/mistral/function_calling/text_to_SQL.ipynb Install the Mistral AI client, Langchain, and DeepEval for building and evaluating the Text-to-SQL system. This is a prerequisite for running the examples. ```python !pip install mistralai langchain deepeval ``` -------------------------------- ### Install Dependencies Source: https://github.com/mistralai/cookbook/blob/main/third_party/E2B_Code_Interpreting/codestral-code-interpreter-js/README.md Run this command to install all necessary project dependencies. ```bash npm install ``` -------------------------------- ### Install Panel and MistralAI Source: https://github.com/mistralai/cookbook/blob/main/third_party/panel/README.md Install the necessary libraries for building the chatbot. ```shell pip install panel mistralai ``` -------------------------------- ### Install Mistral AI and Datasets Source: https://github.com/mistralai/cookbook/blob/main/mistral/ocr/batch_ocr.ipynb Installs the necessary Python libraries for using the Mistral AI client and the datasets library. This is a prerequisite for running the examples. ```python !pip install mistralai datasets ``` -------------------------------- ### Install Dependencies Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs necessary libraries for the fine-tuning process. Ensure you have a compatible environment. ```bash pip install -q transformers datasets accelerate bitsandbytes peft trl scipy Pillow ``` -------------------------------- ### Install Dependencies Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs necessary libraries for the fine-tuning process. Ensure you have a compatible environment. ```bash pip install -q transformers datasets accelerate bitsandbytes peft trl pip install -q torch torchvision torchaudio pip install -q einops pip install -q matplotlib pip install -q scikit-learn pip install -q Pillow pip install -q opencv-python pip install -q segmentation-models-pytorch pip install -q timm ``` -------------------------------- ### Install and Install Pre-commit Hooks Source: https://github.com/mistralai/cookbook/blob/main/CONTRIBUTING_GUIDE.md Install the pre-commit package and then install the repository's pre-commit hooks. This is a mandatory step before making any commits to ensure security checks are active. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### Install ChromaDB and MistralAI Source: https://github.com/mistralai/cookbook/blob/main/third_party/ChromaDB/chroma_mistral_embed_fn.ipynb Installs the necessary libraries for ChromaDB and MistralAI. Use the `-Uq` flags for an upgrade and quiet installation. ```python %pip install chromadb mistralai -Uq ``` -------------------------------- ### Install necessary libraries Source: https://github.com/mistralai/cookbook/blob/main/third_party/gradio/README.md Install the required libraries for PDF processing, vector search, and Gradio. ```shell pip install numpy PyPDF2 faiss ``` -------------------------------- ### Install Libraries Source: https://github.com/mistralai/cookbook/blob/main/third_party/wandb/01_prepare_data.ipynb Installs necessary libraries like scikit-learn and datasets. Run this in a notebook environment. ```python # !pip install scikit-learn datasets ``` -------------------------------- ### Install Libraries Source: https://github.com/mistralai/cookbook/blob/main/mistral/function_calling/function_calling.ipynb Installs the necessary pandas and mistralai libraries. Run this command before proceeding. ```bash !pip install pandas mistralai ``` -------------------------------- ### Install Mesop and MistralAI Source: https://github.com/mistralai/cookbook/blob/main/third_party/mesop/README.md Install the necessary libraries for Mesop and MistralAI. This demo uses specific versions. ```shell pip install mesop mistralai ``` -------------------------------- ### Install Streamlit and Mistral AI Source: https://github.com/mistralai/cookbook/blob/main/third_party/streamlit/README.md Install the necessary libraries for building the chatbot. ```shell pip install streamlit mistralai ``` -------------------------------- ### Install Necessary Libraries Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs the required libraries for fine-tuning, including transformers, datasets, accelerate, and bitsandbytes. ```python import os import sys # Install necessary libraries os.system('pip install -q transformers datasets accelerate bitsandbytes') sys.path.append('../..') ``` -------------------------------- ### Install Dependencies Source: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/quantization/methods/bnb.ipynb Install the necessary libraries for quantization, including bitsandbytes, transformers, peft, and accelerate. ```python !pip install -q -U bitsandbytes !pip install -q -U git+https://github.com/huggingface/transformers.git !pip install -q -U git+https://github.com/huggingface/peft.git !pip install -q -U git+https://github.com/huggingface/accelerate.git ``` -------------------------------- ### Install Solara and Mistral AI Source: https://github.com/mistralai/cookbook/blob/main/third_party/solara/README.md Install the necessary libraries for building the chat interface. ```shell pip install solara mistralai ``` -------------------------------- ### Start Conversation with OAuth-Authenticated Connector (Gmail) Source: https://github.com/mistralai/cookbook/blob/main/mistral/connectors/02-connectors-in-conversations-and-agents.md This example demonstrates how to start a conversation using a connector that requires OAuth2 authentication, such as Gmail. ```APIDOC ## POST /v1/conversations ### Description Starts a new conversation, optionally with tools that require OAuth2 authentication. ### Method POST ### Endpoint /v1/conversations ### Parameters #### Request Body - **model** (string) - Required - The model to use for the conversation. - **inputs** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the message sender ('user' or 'assistant'). - **content** (string) - Required - The content of the message. - **tools** (array) - Optional - An array of tool definitions to be used in the conversation. - **type** (string) - Required - The type of the tool, must be 'connector'. - **connector_id** (string) - Required - The ID of the connector to use. - **authorization** (object) - Required - The authorization details for the connector. - **type** (string) - Required - The type of authorization, must be 'oauth2-token'. - **value** (string) - Required - The OAuth2 access token. ### Request Example ```bash curl -X POST "https://api.mistral.ai/v1/conversations" \ -H "Authorization: Bearer ${MISTRAL_API_KEY}" \ -H "Content-Type: application/json" \ -d '{ "model": "mistral-small-latest", "inputs": [{"role": "user", "content": "What is the latest email I received?"}], "tools": [{ "type": "connector", "connector_id": "gmail", "authorization": { "type": "oauth2-token", "value": "" } }] }' ``` ### Response #### Success Response (200) - **response** (object) - The response from the conversation. #### Response Example ```json { "response": "Your latest email is from John Doe with the subject \"Q1 Report Review\" received at 2:30 PM today..." } ``` ``` -------------------------------- ### Setting up the Environment Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs necessary libraries for fine-tuning, including transformers, datasets, accelerate, and bitsandbytes for efficient model loading. ```bash pip install transformers datasets accelerate bitsandbytes ``` -------------------------------- ### Install LlamaIndex Finetuning Packages Source: https://github.com/mistralai/cookbook/blob/main/third_party/LlamaIndex/llamaindex_mistralai_finetuning.ipynb Installs necessary LlamaIndex packages for finetuning, callbacks, and MistralAI integration. ```python %pip install llama-index-finetuning %pip install llama-index-finetuning-callbacks %pip install llama-index-llms-mistralai %pip install llama-index-embeddings-mistralai ``` -------------------------------- ### Install Mistral AI and FAISS Source: https://github.com/mistralai/cookbook/blob/main/mistral/rag/basic_RAG.ipynb Installs the necessary Python packages for Mistral AI and FAISS CPU. This is a prerequisite for running the RAG examples. ```python ! pip install faiss-cpu==1.7.4 mistralai ``` -------------------------------- ### Setting up the Environment Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs necessary libraries for fine-tuning Pixtral. Ensure you have a compatible Python environment. ```bash pip install -e .[train] pip install flash-attn --no-build-isolation ``` -------------------------------- ### Download External Guides Source: https://github.com/mistralai/cookbook/blob/main/mistral/data_generation/data_generation_refining_news.ipynb Use wget to download necessary guide files from GitHub. These files serve as references for the model. ```python !wget https://github.com/mistralai/cookbook/blob/main/mistral/data_generation/external_files/guide_1.txt !wget https://github.com/mistralai/cookbook/blob/main/mistral/data_generation/external_files/guide_2.txt !wget https://github.com/mistralai/cookbook/blob/main/mistral/data_generation/external_files/guide_3.txt !wget https://github.com/mistralai/cookbook/blob/main/mistral/data_generation/external_files/guide_4.txt ``` -------------------------------- ### Classify a Single Example Source: https://github.com/mistralai/cookbook/blob/main/mistral/embeddings/embeddings.ipynb Classifies a single text example using the trained Logistic Regression model and its corresponding embedding. This demonstrates how to get a prediction for new, unseen text. ```python # Classify a single example text = "I've been experiencing frequent headaches and vision problems." clf.predict([get_text_embedding([text])]) ``` -------------------------------- ### Basic QueryEngine Setup Source: https://github.com/mistralai/cookbook/blob/main/mistral/rag/00-Introduction/concepts.ipynb Demonstrates the simplest QueryEngine setup with a single VectorRetriever. This configuration is suitable for basic search operations. ```python query_engine = QueryEngine( retriever=[VectorRetriever(client=vector_store, embedder=embedder)], ) result = await query_engine.search(query="What is the main topic?", top_k=5) ``` -------------------------------- ### Example cURL Query for Pet Information Source: https://github.com/mistralai/cookbook/blob/main/third_party/Ollama/function_calling_local.ipynb Demonstrates how to retrieve information about a specific pet using its ID via a GET request to the Pet Store API. This is an example of an external API call. ```bash curl -X 'GET' \ 'https://petstore3.swagger.io/api/v3/pet/1' \ -H 'accept: application/json' ``` -------------------------------- ### Set Up Environment Variables Source: https://github.com/mistralai/cookbook/blob/main/mistral/connectors/03-connectors-tool-calling.md Configure your Mistral AI API key by creating a .env file. ```bash MISTRAL_API_KEY=your-mistral-api-key ``` -------------------------------- ### Run Chainlit Application Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/agents_api/multi_agents_data_analysis/README.md Starts the Chainlit application using UV. This command should be run after installing dependencies and setting up the environment. ```bash uv run -- chainlit run app.py ``` -------------------------------- ### Starting the Training Process Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Executes the fine-tuning process using the configured Trainer. ```python trainer.train() ``` -------------------------------- ### Progress Bar Output Example Source: https://github.com/mistralai/cookbook/blob/main/third_party/LlamaIndex/llamaindex_mistralai_finetuning.ipynb This output shows the progress bar from `tqdm` indicating that the processing of questions has started and is at 2% completion. ```text Processing questions: 2%|▎ | 1/40 [00:02<01:43, 2.66s/it] ``` -------------------------------- ### Run Mistral Invoice OCR App Locally Source: https://github.com/mistralai/cookbook/blob/main/third_party/microsoft/README.md Install Python requirements and start the Gradio web UI for the end-to-end invoice OCR pipeline. ```bash python app.py ``` -------------------------------- ### Initialize Langtrace, Mistral, and ChromaDB Clients Source: https://github.com/mistralai/cookbook/blob/main/third_party/Langtrace/langtrace_mistral.ipynb Initializes the necessary SDKs and clients for the application. Ensure you have your API keys set up. ```python import chromadb from mistralai import Mistral from langchain_community.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from chromadb.utils import embedding_functions from langtrace_python_sdk import langtrace, with_langtrace_root_span langtrace.init(api_key='') mistral = Mistral(api_key='') client = chromadb.Client() ``` -------------------------------- ### Start a Basic Conversation (TypeScript) Source: https://github.com/mistralai/cookbook/blob/main/mistral/connectors/02-connectors-in-conversations-and-agents.md Initiate a conversation with the Mistral API using TypeScript. This example demonstrates sending a user query and processing the model's reply. ```typescript import Mistral from "@mistralai/mistralai"; const client = new Mistral({ apiKey: "your-api-key" }); async function main(): Promise { const response = await client.beta.conversations.start({ model: "mistral-small-latest", inputs: [ { role: "user", content: "What is the capital of France?" }, ], }); displayResponse(response); } main(); ``` -------------------------------- ### Production Patterns Setup - Imports and Utilities Source: https://github.com/mistralai/cookbook/blob/main/mistral/ocr/hcls/ocr_hcls.ipynb Sets up the necessary imports and utility functions for production patterns in document intelligence. Includes API key retrieval, Mistral client initialization, and PDF encoding. ```python import base64 import json import time import uuid import os from datetime import datetime from enum import Enum from typing import List, Dict, Any, Optional from dataclasses import dataclass from collections import Counter from pydantic import BaseModel, Field, JsonValue from mistralai import Mistral from mistralai.extra import response_format_from_pydantic_model from google.colab import userdata api_key = userdata.get('MISTRAL_API_KEY') # Replace with your way to retrieve API key if not api_key: print("⚠️ WARNING: No API key found!") print(" Set MISTRAL_API_KEY environment variable, or") print(" Uncomment and set api_key directly above") else: client = Mistral(api_key=api_key) print("✅ Mistral client initialized") def encode_pdf(pdf_path: str) -> Optional[str]: """Encode a PDF file to base64 string.""" try: with open(pdf_path, "rb") as pdf_file: return base64.b64encode(pdf_file.read()).decode('utf-8') except FileNotFoundError: print(f"Error: The file {pdf_path} was not found.") return None except Exception as e: print(f"Error: {e}") return None print("✅ Production patterns setup complete") ``` -------------------------------- ### Retrieve Similar Queries for Few-Shot Prompting Source: https://github.com/mistralai/cookbook/blob/main/third_party/Neon/neon_text_to_sql.ipynb Fetches similar example queries from the vector store to be used in few-shot prompting. This guides the LLM in generating accurate SQL queries. ```python similar_queries = vector_store.similarity_search( query=question, k=3, filter={"topic": {"$eq": "query"}} ) # similar_queries ``` -------------------------------- ### Constructing the Prompt for Text-to-SQL Source: https://github.com/mistralai/cookbook/blob/main/third_party/Neon/neon_text_to_sql.ipynb This code constructs the prompt sent to the Mistral AI model. It includes the user's question, database schema, and example query pairs to guide the LLM. ```python import json prompt = """ You are an AI assistant that converts natural language questions into SQL queries. To do this, you will be provided with three key pieces of information: 1. Some DDL statements describing tables, columns and indexes in the database: {SCHEMA} 2. Some example pairs demonstrating how to convert natural language text into a corresponding SQL query for this schema: {EXAMPLES} 3. The actual natural language question to convert into an SQL query: {QUESTION} Follow the instructions below: 1. Your task is to generate an SQL query that will retrieve the data needed to answer the question, based on the database schema. 2. First, carefully study the provided schema and examples to understand the structure of the database and how the examples map natural language to SQL for this schema. 3. Your answer should have two parts: - Inside XML tag, write out step-by-step reasoning to explain how you are generating the query based on the schema, example, and question. - Then, inside XML tag, output your generated SQL. """ schema = "" for stmt in relevant_ddl_stmts: schema += stmt.page_content + "\n\n" examples = "" for stmt in similar_queries: text_sql_pair = json.loads(stmt.page_content) examples += "Question: " + text_sql_pair["question"] + "\n" examples += "SQL: " + text_sql_pair["query"] + "\n\n" ``` -------------------------------- ### System Prompt Handling Example Source: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md Demonstrates the default method for prepending a system prompt to the first user message, followed by two new lines. ```text [INST] system prompt user message [/INST] assistant message [INST] new user message [/INST] ``` -------------------------------- ### Start a Basic Conversation (Python) Source: https://github.com/mistralai/cookbook/blob/main/mistral/connectors/02-connectors-in-conversations-and-agents.md Send a simple user message to the Mistral API and display the model's response. This is useful for verifying your setup and understanding the basic response structure. ```python import asyncio from mistralai import Mistral client = Mistral(api_key="your-api-key") async def main() -> None: response = await client.beta.conversations.start_async( model="mistral-small-latest", inputs=[ {"role": "user", "content": "What is the capital of France?"} ], ) display_response(response) asyncio.run(main()) ``` -------------------------------- ### Download SWE-bench Repository Structure Source: https://github.com/mistralai/cookbook/blob/main/mistral/embeddings/code_embedding.ipynb Demonstrates the download process for the SWE-bench repository structure from a Google Drive URL. Shows progress and destination. ```shell Downloading... From (original): https://drive.google.com/uc?id=1wG1CcfVHi-70FoAd5wPI59WdI4g1LkpS From (redirected): https://drive.google.com/uc?id=1wG1CcfVHi-70FoAd5wPI59WdI4g1LkpS&confirm=t&uuid=ff0d2b10-c817-4e53-abd5-44d7755dd926 To: /content/min_swebench_repo_structure.zip 100%|██████████| 200M/200M [00:01<00:00, 139MB/s] ``` -------------------------------- ### Chat Completion with Mistral AI Source: https://github.com/mistralai/cookbook/blob/main/quickstart.ipynb Use the Mistral AI client to get chat completions. Ensure you have activated payments and have a valid API key. The `mistral-large-latest` model is used for this example. ```python from mistralai import Mistral api_key = "TYPE YOUR API KEY" model = "mistral-large-latest" client = Mistral(api_key=api_key) chat_response = client.chat.complete( model=model, messages=[{"role":"user", "content":"What is the best French cheese?"}] ) print(chat_response.choices[0].message.content) ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/agents_api/food_diet_companion/README.MD Clone the Mistral AI cookbook repository and navigate to the food diet companion directory. ```bash git clone https://github.com/mistralai/cookbook.git cd mistral/agents/agents_api/food_diet_companion ``` -------------------------------- ### French Pirate Assistant with Prefix Source: https://github.com/mistralai/cookbook/blob/main/mistral/prompting/prefix_use_cases.ipynb Use a prefix to guide the model into a specific persona, like a French pirate. This example shows how a simple prefix can influence the assistant's tone and language. ```python resp = cli.chat.complete(model = "open-mixtral-8x7b", messages = [{"role":"user", "content":question}, {"role":"assistant", "content":prefix, "prefix":True}], max_tokens = 128) print(resp.choices[0].message.content[len(prefix):]) ``` -------------------------------- ### Chainlit Starter Questions Configuration Source: https://github.com/mistralai/cookbook/blob/main/third_party/Chainlit/Chainlit_Mistral_reasoning.ipynb Sets up predefined starter questions for users to interact with the application. These questions are displayed to guide users and showcase the application's capabilities. ```python async def set_starters(): return [ cl.Starter( label="What's the weather in Napoleon's hometown", message="What's the weather in Napoleon's hometown?", icon="/images/idea.svg", ), cl.Starter( label="What's the weather in Paris, TX?", message="What's the weather in Paris, TX?", icon="/images/learn.svg", ), cl.Starter( label="What's the weather in Michel-Angelo's hometown?", message="What's the weather in Michel-Angelo's hometown?", icon="/images/write.svg", ), ] ``` -------------------------------- ### Example cURL Query for User Information Source: https://github.com/mistralai/cookbook/blob/main/third_party/Ollama/function_calling_local.ipynb Shows how to fetch user details by username using a GET request to the Pet Store API. This illustrates another type of external data retrieval. ```bash curl -X 'GET' \ 'https://petstore3.swagger.io/api/v3/user/user1' \ -H 'accept: application/json' ``` -------------------------------- ### Install Necessary Libraries Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs the required libraries for the fine-tuning process, including `transformers`, `datasets`, `accelerate`, and `peft`. ```bash pip install transformers datasets accelerate peft bitsandbytes ``` -------------------------------- ### French Pirate Assistant with Prefix Source: https://github.com/mistralai/cookbook/blob/main/mistral/prompting/prefix_use_cases.ipynb Illustrates using a prefix to guide the model's response in French, ensuring it starts with a specific phrase. This method can be more effective for language adherence than system prompts alone. ```python system = """ Tu es un Assistant qui répond aux questions de l'utilisateur. Tu es un Assistant pirate, tu dois toujours répondre tel un pirate. Réponds toujours en français, et seulement en français. Ne réponds pas en anglais. """ ## You are an Assistant who answers user's questions. You are a Pirate Assistant, you must always answer like a pirate. Always respond in French, and only in French. Do not respond in English. question = """ Hi there! """ prefix = """ Voici votre réponse en français : """ ``` -------------------------------- ### Install Dependencies and Run Web Application Source: https://github.com/mistralai/cookbook/blob/main/third_party/microsoft/mistral-azure-ocr-invoice/README.md Installs project dependencies using pip and then runs the Gradio web application. Open http://localhost:7860 in your browser after running. ```bash pip install -r requirements.txt python app.py ``` -------------------------------- ### Setup Mistral Client Source: https://github.com/mistralai/cookbook/blob/main/mistral/moderation/moderation-explored.ipynb Initialize the Mistral client with your API key. Ensure you have obtained an API key from the Mistral console. ```python from mistralai import Mistral api_key = "API_KEY" client = Mistral(api_key=api_key) ``` -------------------------------- ### Initiate Conversation with a Custom Connector (TypeScript) Source: https://github.com/mistralai/cookbook/blob/main/mistral/connectors/02-connectors-in-conversations-and-agents.md This TypeScript example demonstrates how to start a conversation that utilizes a custom connector. The model can then invoke the tools provided by the connector. Replace 'my_deepwiki' with your connector's identifier. ```typescript import Mistral from "@mistralai/mistralai"; const client = new Mistral({ apiKey: "your-api-key" }); async function main(): Promise { const response = await client.beta.conversations.start({ model: "mistral-small-latest", inputs: [ { role: "user", content: "Using deepwiki, tell me about the structure of the sqlite/sqlite repository.", }, ], tools: [ { type: "connector", connectorId: "my_deepwiki", // name or UUID }, ], }); displayResponse(response); } main(); ``` -------------------------------- ### Install Indexify Source: https://github.com/mistralai/cookbook/blob/main/third_party/Indexify/pdf-entity-extraction/README.md Installs Indexify using the official installation script. ```shell curl https://getindexify.ai | sh ``` -------------------------------- ### Install uv Package Manager Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/non_framework/hubspot_dynamic_multi_agent/chainlit_app/README.md Installs the uv package manager, a fast Python package installer and resolver. Use this if you don't have uv installed. ```bash pip install uv ``` -------------------------------- ### Get Response with Function Calling Source: https://github.com/mistralai/cookbook/blob/main/mistral/rag/RAG_via_function_calling.ipynb This Python function uses Mistral's function calling to determine the appropriate database ('HR', 'Product', 'Finance', or 'Other') to search for an answer to a given question. It defines a 'search_in_database' tool and a system prompt to guide the AI. The function prints the user's question and the suggested database for searching. ```python def get_response(question): """ Generate questions about HR, Prodcut, Finance or anything else. Returns: List[str]: Llist of generated questions. """ tools = [ { "type": "function", "function": { "name": "search_in_database", "description": "search_answer_in", "parameters": { "type": "object", "properties": { "question": { "type": "string", "description": "The question asked by used", }, "source" : { "type": "string", "description": "Source to use to answer the question" } }, "required": ["source", "question"], }, }, } ] system_prompt = """ Your are an AI assistant for a Lemonade company. Your job is to help employee retrieve relevant information. Specifically, you generate calls to a tool function which will then be able to perform efficient retrieval of relevant information. You dispose of a single tool function called "search_answer_in" which has two parameters "sources" and "question". "question" is a copy paste of the user question. "source" is a string whose values can exclusively be "HR", "Product", "Finance" or "Other" The tool call will provide you some content to answer the user question. Based on the the content from the tool call, answer the user's query. You should be as helpful as possible while remaining factual, objective and keeping a professional tone. You must answer in the same language as the user (or the one they ask you to). """ chat_history = [ { "role": "system", "content": system_prompt }, { "role": "user", "content": question } ] chat_response = client.chat.complete( model="mistral-large-latest", temperature=0.3, messages=chat_history, tools=tools, tool_choice='any' ) print(question) chat_history.append(chat_response.choices[0].message) db_to_search_in = json.loads(chat_response.choices[0].message.tool_calls[0].function.arguments)['source'] print(f"Suggest search in {db_to_search_in} DB") return db_to_search_in get_response('What are the different tastes of limonade ?') ``` -------------------------------- ### Install Dependencies Source: https://github.com/mistralai/cookbook/blob/main/third_party/LlamaIndex/llamaindex_agentic_rag.ipynb Installs necessary LlamaIndex, Mistral AI, and PDF parsing libraries. Run this before setting up the connection. ```python !pip install llama-index-core !pip install llama-index-embeddings-mistralai !pip install llama-index-llms-mistralai !pip install llama-index-readers-file !pip install mistralai pypdf ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/agents_api/prd_linear_ticket/README.md Installs necessary Python packages for the application. Ensure you have Python and pip installed. ```bash pip install chainlit mcp loguru pydantic gql ``` -------------------------------- ### Install Mistral AI Library Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/non_framework/recruitment_agent/Multi_Agent_Workflow_For_Recruitment.ipynb Installs the necessary Mistral AI Python library. Ensure you have pip installed. ```python !pip install mistralai ``` -------------------------------- ### Install Necessary Libraries Source: https://github.com/mistralai/cookbook/blob/main/mistral/fine_tune/pixtral_finetune_on_satellite_data.ipynb Installs the required libraries for the fine-tuning process, including transformers, datasets, accelerate, bitsandbytes, and peft. ```bash pip install transformers datasets accelerate bitsandbytes peft ``` -------------------------------- ### Install Mistral AI Package Source: https://github.com/mistralai/cookbook/blob/main/mistral/ocr/product_datasheet_analysis/product_datasheet_analysis.ipynb Installs the necessary Mistral AI Python package. Uncomment the line if you need to install it. ```python # Install required packages (uncomment if needed) # !pip install mistralai ``` -------------------------------- ### Initialize ReActAgent with Tools Source: https://github.com/mistralai/cookbook/blob/main/third_party/LlamaIndex/Agents_Tools.ipynb Set up a ReActAgent by providing a list of tools, an LLM, and enabling verbose output for debugging. This agent can then be used to process user queries. ```python from llama_index.core.agent import ReActAgent agent = ReActAgent.from_tools([query_engine_tool], llm=llm, verbose=True) ``` -------------------------------- ### Set up Vector Store Index and Query Engine Source: https://github.com/mistralai/cookbook/blob/main/third_party/LlamaIndex/llamaindex_mistralai_finetuning.ipynb Configures the embedding model and LLM, then builds a VectorStoreIndex from documents. A query engine is created for similarity searches. ```python from llama_index.core import VectorStoreIndex Settings.embed_model = MistralAIEmbedding() Settings.llm = llm index = VectorStoreIndex.from_documents( documents, ) query_engine = index.as_query_engine(similarity_top_k=2) ``` -------------------------------- ### Quantize Model with Examples Source: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/quantization/methods/gptq.ipynb Quantizes a model using a list of examples. The examples should be a list of dictionaries, each containing 'input_ids' and 'attention_mask'. ```python model.quantize(examples) ``` -------------------------------- ### Install Dependencies for PDF Chat Source: https://github.com/mistralai/cookbook/blob/main/third_party/solara/README.md Installs the necessary libraries for PDF processing and vector storage. Use 'faiss-cpu' for CPU-only installations. ```bash pip install PyPDF2 faiss ``` -------------------------------- ### Configure Environment Variables for Travel Assistant Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/agents_api/travel_assistant/README.MD Sets up environment variables by copying an example file and adding API keys for Mistral AI and SerpAPI. ```bash # Create a .env file cp .env.example .env # Add your API keys to .env export MISTRAL_API_KEY= export SERP_API_KEY= ``` -------------------------------- ### Install Mistral AI Library Source: https://github.com/mistralai/cookbook/blob/main/mistral/classifier_factory/intent_classification.ipynb Installs the Mistral AI Python client library. Use `%%capture` to suppress output during installation. ```python %%capture !pip install mistralai ``` -------------------------------- ### Loading Guides for Data Generation Source: https://github.com/mistralai/cookbook/blob/main/mistral/data_generation/data_generation_refining_news.ipynb Reads guide content from multiple files into a list. These guides are used in the data generation process. ```python guides = [] for pick in range(1, 5): jsonl_file_path = f"./guide_{pick}.txt" with open(jsonl_file_path, "r") as f: guide = f.read() guides.append(guide) ``` -------------------------------- ### Run the Application Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/agents_api/food_diet_companion/README.MD Run the food diet companion agent application using Chainlit. ```bash chainlit run agent.py ``` -------------------------------- ### Install MetaGPT Dependencies Source: https://github.com/mistralai/cookbook/blob/main/third_party/metagpt/prompt_optimization.ipynb Installs the necessary dependencies for MetaGPT by referencing the requirements file. Use '-qUr' for quiet installation and recursive requirements. ```bash # install dependancies !pip install -qUr MetaGPT/requirements.txt ``` -------------------------------- ### Initialize DatabaseQueryAgent Source: https://github.com/mistralai/cookbook/blob/main/mistral/agents/non_framework/industrial_knowledge_agent/IndustrialKnowledgeAgent.ipynb Initializes the agent with the database path, tools, and Mistral client. Sets up a mapping from tool names to their corresponding functions. ```python class DatabaseQueryAgent: """ Agent responsible for interacting with the SQLite database. """ def __init__(self, db_path: str, mistral_client: Mistral): self.db_path = db_path self.tools = tools self.names_to_functions = { 'query_compliance': functools.partial(self.query_compliance), 'query_maintenance': functools.partial(self.query_maintenance), 'query_technical_specs': functools.partial(self.query_technical_specs), 'query_parts_inventory_compatibility': functools.partial(self.query_parts_inventory_compatibility) } self.mistral_client = mistral_client ``` -------------------------------- ### Install Dependencies for Ollama and Mistral Source: https://github.com/mistralai/cookbook/blob/main/third_party/Ollama/function_calling_local.ipynb Installs the necessary Python packages for Ollama, Mistral-common, pandas, prance, and openapi-spec-validator. Ensure these are installed before proceeding. ```python !pip install --upgrade ollama mistral-common pandas !pip install --upgrade prance openapi-spec-validator ``` -------------------------------- ### Install Dependencies Source: https://github.com/mistralai/cookbook/blob/main/third_party/Phoenix/arize_phoenix_tracing.ipynb Installs necessary libraries for Mistral AI, OpenInference, and Arize Phoenix. Use this command to set up your environment. ```python !pip install -q arize-phoenix jsonschema openinference-instrumentation-mistralai !pip install -qU mistralai ```