Skip to main content

Quick Start: Python SDK (Langchain)

In this guide, we will show you how to quickly get started with the three key models of our platform:

  • ChatCompressa (LLM)
  • CompressaEmbeddings (for creating text vectors)
  • CompressaRerank (for improving search results)

By using these models together with Langchain, you can create custom RAG assistants.

Installation

First, install the langchain-compressa library:

pip install langchain-compressa

Importing Compressa Models

from langchain_compressa import CompressaEmbeddings
from langchain_compressa import ChatCompressa
from langchain_compressa.reranks import CompressaRerank

Obtaining an API Key

To use Compressa, you will need an API key. You can obtain it after registration.

Environment Setup

After receiving the key, set it as an environment variable:

import os
os.environ["COMPRESSA_API_KEY"] = "your_key_here"

Replace "your_key_here" with your actual API key.

CompressaEmbeddings

The CompressaEmbeddings model allows you to create vector representations (embeddings) for texts. This is useful for semantic search and other natural language processing tasks (e.g., classification).

from langchain_compressa.embeddings import CompressaEmbeddings

embeddings = CompressaEmbeddings()

# Create an embedding for a single query
query_embedding = embeddings.embed_query("How to cook borscht?")

# Create embeddings for multiple documents
docs_embeddings = embeddings.embed_documents([
"Borscht is a traditional Slavic soup",
"Beetroot is needed to make borscht",
"Borscht is usually served with sour cream",
"Meat is often added to borscht",
"Borscht has a characteristic red color"
])

# Asynchronous embedding creation

await embeddings.aembed_query("How to cook borscht?")

await = embeddings.aembed_documents([
"Borscht is a traditional Slavic soup",
"Beetroot is needed to make borscht",
"Borscht is usually served with sour cream",
"Meat is often added to borscht",
"Borscht has a characteristic red color"
])

If you want to dive deeper into Embeddings and understand how semantic search is technically structured, check out our practical guide.

ChatCompressa

ChatCompressa is a fast and cost-effective LLM with support for the Russian language.

from langchain_compressa import ChatCompressa

llm = ChatCompressa(temperature=0.5)

You can invoke the LLM in a simple way:

messages = [
("system", "You are a helpful assistant that translates from Russian to English. Translate the user's sentence."),
("human", "I love programming.")
]
ai_msg = llm.invoke(messages)
print(f"Model's response: {ai_msg.content}")

Or use the LLM in a chain with a prompt template:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates from {input_language} to {output_language}.",
),
("human", "{input}"),
]
)

chain = prompt | llm
result = chain.invoke(
{
"input_language": "Russian",
"output_language": "English",
"input": "I love programming.",
}
)

print(f"Chain result: {result.content}")

CompressaRerank

CompressaRerank allows you to re-rank documents based on their relevance to a given query.

from langchain_core.documents import Document
from langchain_compressa.reranks import CompressaRerank

# Create a list of documents in Langchain format, adding metadata

documents = [
Document(
page_content="""Carson City is the capital of the American state of Nevada.
According to the 2010 US Census, the population of Carson City was 55,274.""",
metadata={"source": "https://example.com/1"}
),
Document(
page_content="""The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean,
which is a political division controlled by the United States. The capital is Saipan.""",
metadata={"source": "https://example.com/2"}
),
Document(
page_content="""Charlotte Amalie is the capital and largest city of the US Virgin Islands.
It has a population of about 20,000. The city is located on the island of St. Thomas.""",
metadata={"source": "https://example.com/3"}
),
Document(
page_content="""Washington, D.C. (also known simply as Washington or D.C., and officially as the District of Columbia) is the capital of the United States.
It is a federal district. The residence of the US President and many major government institutions are located there,
making it the political center of the United States.""",
metadata={"source": "https://example.com/4"}
),
Document(
page_content="""Capital punishment existed in the United States even before the United States became a country.
As of 2017, the death penalty is allowed in 30 out of 50 states. The federal government (including the US armed forces) also
applies the death penalty.""",
metadata={"source": "https://example.com/5"}
)
]

query = "What is the capital of the United States?"

# Define our re-ranking model
reranker = CompressaRerank()

# Apply CompressaRerank to re-rank the documents for the user's query
compress_res = reranker.compress_documents(query=query, documents=documents)

The benefits of re-ranking are easier to understand when working with a large number of documents. We have prepared a separate guide, where we improve keyword search accuracy using CompressaRerank.