LlamaIndex LLMs Integration: AI21 Labs
Installation
First, you need to install the package. You can do this using pip:
pip install llama-index-llms-ai21
Usage
Here's a basic example of how to use the AI21 class to generate text completions and handle chat interactions.
Initializing the AI21 Client
You need to initialize the AI21 client with the appropriate model and API key.
from llama_index.llms.ai21 import AI21
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
Chat Completions
from llama_index.llms.ai21 import AI21
from llama_index.core.base.llms.types import ChatMessage
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
messages = [ChatMessage(role="user", content="What is the meaning of life?")]
response = llm.chat(messages)
print(response.message.content)
Chat Streaming
from llama_index.llms.ai21 import AI21
from llama_index.core.base.llms.types import ChatMessage
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
messages = [ChatMessage(role="user", content="What is the meaning of life?")]
for chunk in llm.stream_chat(messages):
print(chunk.message.content)
Text Completion
from llama_index.llms.ai21 import AI21
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
response = llm.complete(prompt="What is the meaning of life?")
print(response.text)
Stream Text Completion
from llama_index.llms.ai21 import AI21
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
response = llm.stream_complete(prompt="What is the meaning of life?")
for chunk in response:
print(response.text)
Other Models Support
You could also use more model types. For example the j2-ultra and j2-mid
These models support chat and complete methods only.
Chat
from llama_index.llms.ai21 import AI21
from llama_index.core.base.llms.types import ChatMessage
api_key = "your_api_key"
llm = AI21(model="j2-chat", api_key=api_key)
messages = [ChatMessage(role="user", content="What is the meaning of life?")]
response = llm.chat(messages)
print(response.message.content)
Complete
from llama_index.llms.ai21 import AI21
api_key = "your_api_key"
llm = AI21(model="j2-ultra", api_key=api_key)
response = llm.complete(prompt="What is the meaning of life?")
print(response.text)
Tokenizer
The type of the tokenizer is determined by the name of the model
from llama_index.llms.ai21 import AI21
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
tokenizer = llm.tokenizer
tokens = tokenizer.encode("What is the meaning of life?")
print(tokens)
text = tokenizer.decode(tokens)
print(text)
Async Support
You can also use the async functionalities
async chat
from llama_index.llms.ai21 import AI21
from llama_index.core.base.llms.types import ChatMessage
async def main():
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
messages = [
ChatMessage(role="user", content="What is the meaning of life?")
]
response = await llm.achat(messages)
print(response.message.content)
async stream_chat
from llama_index.llms.ai21 import AI21
from llama_index.core.base.llms.types import ChatMessage
async def main():
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
messages = [
ChatMessage(role="user", content="What is the meaning of life?")
]
response = await llm.astream_chat(messages)
async for chunk in response:
print(chunk.message.content)
Tool Calling
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.ai21 import AI21
from llama_index.core.tools import FunctionTool
def multiply(a: int, b: int) -> int:
"""Multiply 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
def divide(a: int, b: int) -> float:
"""Divide two integers and returns the result float"""
return a - b
def add(a: int, b: int) -> int:
"""Add 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)
divide_tool = FunctionTool.from_defaults(fn=divide)
api_key = "your_api_key"
llm = AI21(model="jamba-1.5-mini", api_key=api_key)
agent = FunctionAgent(
tools=[multiply_tool, add_tool, subtract_tool, divide_tool],
llm=llm,
)
response = await agent.run(
"My friend Moses had 10 apples. He ate 5 apples in the morning. Then he found a box with 25 apples."
"He divided all his apples between his 5 friends. How many apples did each friend get?"
)
Metadata
Release files for llama-index-llms-ai21 0.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llama_index_llms_ai21-0.8.0.tar.gz | 7.7 kB | Without provenance | Details for llama_index_llms_ai21-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llama_index_llms_ai21-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for llama_index_llms_ai21-0.8.0-py3-none-any.whl |
Total release size: 15.4 kB
Release files / llama_index_llms_ai21-0.8.0.tar.gz
| Download URL | llama_index_llms_ai21-0.8.0.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a81e273791e85cfde4d41fff192ca41bdc3fc58d5ca4d519e361385ba7c9504e
|
|
BLAKE2b-256 checksum How to use checksums |
a6fffedf5de4b5c5416defeda4543d36fe4725d93f7a70cb8e1596e544cf67d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llama_index_llms_ai21-0.8.0-py3-none-any.whl
| Download URL | llama_index_llms_ai21-0.8.0-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2107f9c370f8e49f48e962ee1650d230edb7be71d4c45f0fe133b7d7f94e5b3b
|
|
BLAKE2b-256 checksum How to use checksums |
b917660409b9e2bf1e8074909ae9ec6b2578e43cb379e04594030ff375cc0a8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for llama-index-llms-ai21 0.8.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.