An integration package connecting AI21 and LangChain
Project description
langchain-ai21
This package contains the LangChain integrations for AI21 through their AI21 SDK.
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.
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="j2-ultra")
messages = [HumanMessage(content="Hello from AI21")]
chat.invoke(messages)
LLMs
You can use AI21's generative AI models as Langchain LLMs:
from langchain.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 models as:
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!"])
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
langchain_ai21-0.0.1.tar.gz
(5.8 kB
view details)
Built Distribution
File details
Details for the file langchain_ai21-0.0.1.tar.gz
.
File metadata
- Download URL: langchain_ai21-0.0.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2092734293f97f9295b4d50bf38660027430a4046fe4f1637d2e5b5f49436999 |
|
MD5 | 1ae652346addef9642f09e00f9552367 |
|
BLAKE2b-256 | c4c94f7d0612a14a4faf532bf98b22bf907e0cd07fd5709c203cda0a64a2c472 |
File details
Details for the file langchain_ai21-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: langchain_ai21-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4e9900ac05efad6d5fe784562aca01a5ea9c5487e16bb236374cec9dd6300d4 |
|
MD5 | 56af996e35e33fc2ed7098f0392e6ef4 |
|
BLAKE2b-256 | ae393742dc39f6dac973e415f6cba0ad078aabfc207bda5fd49fe8a9080586b3 |