An integration package connecting AI21 and LangChain
Project description
langchain-ai21
This package contains the LangChain integrations for AI21 models and tools.
Installation and Setup
- Install the AI21 partner package
pip install langchain-ai21
- Get an AI21 api key and set it as an environment variable (
AI21_API_KEY
)
Chat Models
This package contains the ChatAI21
class, which is the recommended way to interface with AI21 chat models, including Jamba-Instruct
and any Jurassic chat models.
To use, install the requirements and configure your environment.
export AI21_API_KEY=your-api-key
Then initialize
from langchain_core.messages import HumanMessage
from langchain_ai21.chat_models import ChatAI21
chat = ChatAI21(model="jamba-instruct")
messages = [HumanMessage(content="Hello from AI21")]
chat.invoke(messages)
For a list of the supported models, see this page
Streaming in Chat
Streaming is supported by the latest models. To use streaming, set the streaming
parameter to True
when initializing the model.
from langchain_core.messages import HumanMessage
from langchain_ai21.chat_models import ChatAI21
chat = ChatAI21(model="jamba-instruct", streaming=True)
messages = [HumanMessage(content="Hello from AI21")]
response = chat.invoke(messages)
or use the stream
method directly
from langchain_core.messages import HumanMessage
from langchain_ai21.chat_models import ChatAI21
chat = ChatAI21(model="jamba-instruct")
messages = [HumanMessage(content="Hello from AI21")]
for chunk in chat.stream(messages):
print(chunk)
LLMs
You can use AI21's Jurassic generative AI models as LangChain LLMs. To use the newer Jamba model, use the ChatAI21 chat model, which supports single-turn instruction/question answering capabilities.
from langchain_core.prompts import PromptTemplate
from langchain_ai21 import AI21LLM
llm = AI21LLM(model="j2-ultra")
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
chain = prompt | llm
question = "Which scientist discovered relativity?"
print(chain.invoke({"question": question}))
Embeddings
You can use AI21's embeddings model as shown here:
Query
from langchain_ai21 import AI21Embeddings
embeddings = AI21Embeddings()
embeddings.embed_query("Hello! This is some query")
Document
from langchain_ai21 import AI21Embeddings
embeddings = AI21Embeddings()
embeddings.embed_documents(["Hello! This is document 1", "And this is document 2!"])
Task-Specific Models
Contextual Answers
You can use AI21's contextual answers model to parse given text and answer a question based entirely on the provided information.
This means that if the answer to your question is not in the document, the model will indicate it (instead of providing a false answer)
from langchain_ai21 import AI21ContextualAnswers
tsm = AI21ContextualAnswers()
response = tsm.invoke(input={"context": "Lots of information here", "question": "Your question about the context"})
You can also use it with chains and output parsers and vector DBs:
from langchain_ai21 import AI21ContextualAnswers
from langchain_core.output_parsers import StrOutputParser
tsm = AI21ContextualAnswers()
chain = tsm | StrOutputParser()
response = chain.invoke(
{"context": "Your context", "question": "Your question"},
)
Text Splitters
Semantic Text Splitter
You can use AI21's semantic text segmentation model to split a text into segments by topic. Text is split at each point where the topic changes.
For a list for examples, see this page.
from langchain_ai21 import AI21SemanticTextSplitter
splitter = AI21SemanticTextSplitter()
response = splitter.split_text("Your text")
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 langchain_ai21-0.1.7.tar.gz
.
File metadata
- Download URL: langchain_ai21-0.1.7.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f4863102e092eac4fab04ab1496701ea7c100a50ad8e467f0471cfaa2e1871f |
|
MD5 | 592d86b8feb852363c0dd4b941c01422 |
|
BLAKE2b-256 | 322db0b7267f3408f25b8813bfaac53602a8196606452c483338929c646495eb |
File details
Details for the file langchain_ai21-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: langchain_ai21-0.1.7-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6f2d353ba2f6b5fa06c02fd34eb328c5f337b48048768e6ebc8de94f38e91fc |
|
MD5 | f5415711e6efe3b6c1aa623365680932 |
|
BLAKE2b-256 | 0e26a5c46c69a3a387312b6219e44cc332e6b0e2e4e4933b34ed8dc8d6a963a7 |