llama-index llms mistral ai integration
Project description
LlamaIndex Llms Integration: Mistral
Installation
Install the required packages using the following commands:
%pip install llama-index-llms-mistralai
!pip install llama-index
Basic Usage
Initialize the MistralAI Model
To use the MistralAI model, create an instance and provide your API key:
from llama_index.llms.mistralai import MistralAI
llm = MistralAI(api_key="<replace-with-your-key>")
Generate Completions
To generate a text completion for a prompt, use the complete
method:
resp = llm.complete("Paul Graham is ")
print(resp)
Chat with the Model
You can also chat with the model using a list of messages. Here’s an example:
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(role="system", content="You are CEO of MistralAI."),
ChatMessage(role="user", content="Tell me the story about La plateforme"),
]
resp = MistralAI().chat(messages)
print(resp)
Using Random Seed
To set a random seed for reproducibility, initialize the model with the random_seed
parameter:
resp = MistralAI(random_seed=42).chat(messages)
print(resp)
Streaming Responses
Stream Completions
You can stream responses using the stream_complete
method:
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
Stream Chat Responses
To stream chat messages, use the following code:
messages = [
ChatMessage(role="system", content="You are CEO of MistralAI."),
ChatMessage(role="user", content="Tell me the story about La plateforme"),
]
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
Configure Model
To use a specific model configuration, initialize the model with the desired model name:
llm = MistralAI(model="mistral-medium")
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
Function Calling
You can call functions from the model by defining tools. Here’s an example:
from llama_index.llms.mistralai import MistralAI
from llama_index.core.tools import FunctionTool
def multiply(a: int, b: int) -> int:
"""Multiply two integers and return the result."""
return a * b
def mystery(a: int, b: int) -> int:
"""Mystery function on two integers."""
return a * b + a + b
mystery_tool = FunctionTool.from_defaults(fn=mystery)
multiply_tool = FunctionTool.from_defaults(fn=multiply)
llm = MistralAI(model="mistral-large-latest")
response = llm.predict_and_call(
[mystery_tool, multiply_tool],
user_msg="What happens if I run the mystery function on 5 and 7",
)
print(str(response))
LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/mistralai/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file llama_index_llms_mistralai-0.3.0.tar.gz
.
File metadata
- Download URL: llama_index_llms_mistralai-0.3.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7108d57046ea6455692ee486b52cf390784931275f7e8b04aacce4ea0cc3c0c |
|
MD5 | f6e10b3735f113484b6698a6bdd0f02f |
|
BLAKE2b-256 | ae769c7f31c2f70a8c03ff387595e534aafd91380597cf8ff567d67fff720f3b |
File details
Details for the file llama_index_llms_mistralai-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: llama_index_llms_mistralai-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1da2610a64b27c9a446f3e5d05a5fc0df6574e9a0887ae4789edf2743fb79121 |
|
MD5 | ad15da5b571c93d604cf30abf6d152bf |
|
BLAKE2b-256 | e67c81e920a05850e03337ab7f1f1351300ee510b87dc250bdb496a7f58764e5 |