docs/examples/cookbooks/mistralai.ipynb
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/mistralai.ipynb" target="_parent"></a>
MistralAI released mixtral-8x22b.
It is a sparse Mixture-of-Experts (SMoE) model that uses only 39B active parameters out of 141B, offering unparalleled cost efficiency for its size with 64K tokens context window, multilingual, strong maths coding, coding and Function calling capabilities.
This is a cook-book in showcasing the usage of mixtral-8x22b model with llama-index.
import nest_asyncio
nest_asyncio.apply()
import os
os.environ["MISTRAL_API_KEY"] = "<YOUR MISTRAL API KEY>"
from llama_index.llms.mistralai import MistralAI
from llama_index.embeddings.mistralai import MistralAIEmbedding
from llama_index.core import Settings
llm = MistralAI(model="open-mixtral-8x22b", temperature=0.1)
embed_model = MistralAIEmbedding(model_name="mistral-embed")
Settings.llm = llm
Settings.embed_model = embed_model
We will use Uber-2021 and Lyft-2021 10K SEC filings.
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/uber_2021.pdf' -O './uber_2021.pdf'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/lyft_2021.pdf' -O './lyft_2021.pdf'
from llama_index.core import SimpleDirectoryReader
uber_docs = SimpleDirectoryReader(input_files=["./uber_2021.pdf"]).load_data()
lyft_docs = SimpleDirectoryReader(input_files=["./lyft_2021.pdf"]).load_data()
from llama_index.core import VectorStoreIndex
uber_index = VectorStoreIndex.from_documents(uber_docs)
uber_query_engine = uber_index.as_query_engine(similarity_top_k=5)
lyft_index = VectorStoreIndex.from_documents(lyft_docs)
lyft_query_engine = lyft_index.as_query_engine(similarity_top_k=5)
response = uber_query_engine.query("What is the revenue of uber in 2021?")
print(response)
response = lyft_query_engine.query("What are lyft investments in 2021?")
print(response)
FunctionAgent with RAG QueryEngineTools.Here we use Fuction Calling capabilities of the model.
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.agent.workflow import FunctionAgent
query_engine_tools = [
QueryEngineTool(
query_engine=lyft_query_engine,
metadata=ToolMetadata(
name="lyft_10k",
description="Provides information about Lyft financials for year 2021",
),
),
QueryEngineTool(
query_engine=uber_query_engine,
metadata=ToolMetadata(
name="uber_10k",
description="Provides information about Uber financials for year 2021",
),
),
]
agent = FunctionAgent(
tools=query_engine_tools,
llm=llm,
)
response = await agent.run("What is the revenue of uber in 2021.")
print(str(response))
response = await agent.run("What are lyft investments in 2021?")
print(str(response))
from llama_index.core.tools import FunctionTool
from llama_index.core.agent.workflow import (
FunctionAgent,
ReActAgent,
)
def multiply(a: int, b: int) -> int:
"""Multiply two integers and returns the result integer"""
return a * b
def add(a: int, b: int) -> int:
"""Add two integers and returns the result integer"""
return a + b
def subtract(a: int, b: int) -> int:
"""Subtract two integers and returns the result integer"""
return a - b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
add_tool = FunctionTool.from_defaults(fn=add)
subtract_tool = FunctionTool.from_defaults(fn=subtract)
agent = FunctionAgent(
tools=[multiply_tool, add_tool, subtract_tool],
llm=llm,
)
response = await agent.run("What is (26 * 2) + 2024?")
print(str(response))
agent = ReActAgent(tools=[multiply_tool, add_tool, subtract_tool], llm=llm)
response = await agent.run("What is (26 * 2) + 2024?")
print(str(response))