LLMRails
LLMRails is a API platform for building GenAI applications. It provides an easy-to-use API for document indexing and querying that is managed by LLMRails and is optimized for performance and accuracy. See the LLMRails API documentation for more information on how to use the API.
You'll need to install langchain-community
with pip install -qU langchain-community
to use this integration
This notebook shows how to use functionality related to the LLMRails
's integration with langchain.
Note that unlike many other integrations in this category, LLMRails provides an end-to-end managed service for retrieval augmented generation, which includes:
- A way to extract text from document files and chunk them into sentences.
- Its own embeddings model and vector store - each text segment is encoded into a vector embedding and stored in the LLMRails internal vector store
- A query service that automatically encodes the query into embedding, and retrieves the most relevant text segments (including support for Hybrid Search)
All of these are supported in this LangChain integration.
Setup
You will need a LLMRails account to use LLMRails with LangChain. To get started, use the following steps:
- Sign up for a LLMRails account if you don't already have one.
- Next you'll need to create API keys to access the API. Click on the "API Keys" tab in the corpus view and then the "Create API Key" button. Give your key a name. Click "Create key" and you now have an active API key. Keep this key confidential.
To use LangChain with LLMRails, you'll need to have this value: api_key. You can provide those to LangChain in two ways:
- Include in your environment these two variables:
LLM_RAILS_API_KEY
,LLM_RAILS_DATASTORE_ID
.
For example, you can set these variables using os.environ and getpass as follows:
import os
import getpass
os.environ["LLM_RAILS_API_KEY"] = getpass.getpass("LLMRails API Key:")
os.environ["LLM_RAILS_DATASTORE_ID"] = getpass.getpass("LLMRails Datastore Id:")
- Provide them as arguments when creating the LLMRails vectorstore object:
vectorstore = LLMRails(
api_key=llm_rails_api_key,
datastore_id=datastore_id
)
Adding text
For adding text to your datastore first you have to go to Datastores page and create one. Click Create Datastore button and choose a name and embedding model for your datastore. Then get your datastore id from newly created datatore settings.
%pip install tika
Collecting tika
Downloading tika-2.6.0.tar.gz (27 kB)
Preparing metadata (setup.py) ... [?25ldone
[?25hRequirement already satisfied: setuptools in /Users/omaraly/anaconda3/lib/python3.11/site-packages (from tika) (68.2.2)
Requirement already satisfied: requests in /Users/omaraly/anaconda3/lib/python3.11/site-packages (from tika) (2.31.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/omaraly/anaconda3/lib/python3.11/site-packages (from requests->tika) (2.1.1)
Requirement already satisfied: idna<4,>=2.5 in /Users/omaraly/anaconda3/lib/python3.11/site-packages (from requests->tika) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/omaraly/anaconda3/lib/python3.11/site-packages (from requests->tika) (1.26.16)
Requirement already satisfied: certifi>=2017.4.17 in /Users/omaraly/anaconda3/lib/python3.11/site-packages (from requests->tika) (2022.12.7)
Building wheels for collected packages: tika
Building wheel for tika (setup.py) ... [?25ldone
[?25h Created wheel for tika: filename=tika-2.6.0-py3-none-any.whl size=32621 sha256=b3f03c9dbd7f347d712c49027704d48f1a368f31560be9b4ee131f79a52e176f
Stored in directory: /Users/omaraly/Library/Caches/pip/wheels/27/ba/2f/37420d1191bdae5e855d69b8e913673045bfd395cbd78ad697
Successfully built tika
Installing collected packages: tika
Successfully installed tika-2.6.0
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.3.1[0m[39;49m -> [0m[32;49m23.3.2[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
Note: you may need to restart the kernel to use updated packages.
import os
from langchain_community.vectorstores import LLMRails
os.environ["LLM_RAILS_DATASTORE_ID"] = "Your datastore id "
os.environ["LLM_RAILS_API_KEY"] = "Your API Key"
llm_rails = LLMRails.from_texts(["Your text here"])