Langchain with Blablador¶
It's very simple to connect Langchain to Blablador, to be able to talk to your own documents.
Langchain uses the technique known as RAG (Retrieval-Augmented Generation) to generate text based on your own documents.
Please check the guides on how to get a Blablador token in the Guide for Blablador's API Access. Please also check there about the aliases for the LLMs.
Once you get it, install uv with the command pip install uv and run the following commands:
uv venv langchain
source langchain/bin/activate
uv pip install langchain_openai langchain_community langchain chromadb
This is a demonstration code of RAG with the speech of the State of the Union. Please replace the OPENAI_API_KEY with your actual Blablador token.
import os
from langchain_openai import ChatOpenAI
from langchain_community.document_loaders import TextLoader
from langchain_openai import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
from urllib.request import urlretrieve
# You need to set them as environment variables, because the OpenAI API client uses them
# multiple times
os.environ["OPENAI_API_KEY"] = "ADD_YOUR_TOKEN_HERE"
os.environ["OPENAI_API_BASE"] = "https://api.helmholtz-blablador.fz-juelich.de/v1"
embedding = OpenAIEmbeddings(model="text-embedding-ada-002")
# Download the file to query from
urlretrieve(
url="https://raw.githubusercontent.com/hwchase17/langchain/v0.0.200/docs/modules/state_of_the_union.txt",
filename="./state_of_the_union.txt")
loader = TextLoader("state_of_the_union.txt")
index = VectorstoreIndexCreator(embedding=embedding).from_loaders([loader])
llm = ChatOpenAI(model="alias-fast")
questions = [
"Who is the speaker",
"What did the president say about Ketanji Brown Jackson",
"What are the threats to America",
"Who are mentioned in the speech",
"Who is the vice president",
"How many projects were announced",
]
for query in questions:
print("Query:", query)
print("Answer:", index.query(query, llm=llm))
The code should read the file state_of_the_union.txt and answer the questions. You can replace the file with your own documents, and the questions with your own questions, of course.