Skip to main content

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!"])

Task Specific Models

Contextual Answers

You can use AI21's contextual answers model to receives text or document, serving as a context, and a question and returns an answer based entirely on this context.

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": "Your context", "question": "Your question"})

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"},
)

Project details


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.1.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

langchain_ai21-0.1.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file langchain_ai21-0.1.2.tar.gz.

File metadata

  • Download URL: langchain_ai21-0.1.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for langchain_ai21-0.1.2.tar.gz
Algorithm Hash digest
SHA256 47c4b6e7007e04198e151052a9efca866e00abad389b28537e1c4a9a0081d745
MD5 6ffbd6566f17389fabca374edc65c0e8
BLAKE2b-256 0784b8d9acaf10c43ba7a5f9347c727c374f11c3e283e87ef2249d13760b2836

See more details on using hashes here.

File details

Details for the file langchain_ai21-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_ai21-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f93c8081c389d6fb59c9f8daabf05c1b541dc682ec49bad4f40ad4127b1f1c9
MD5 0e8ef9e7195138ddfeedd2d299998fe9
BLAKE2b-256 9ed051c82326ee5f0f6503f7e35aded20d2e9765db65194b371bab6e16c9d697

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page