Skip to main content

Infinity

Infinity allows to create Embeddings using a MIT-licensed Embedding Server.

This notebook goes over how to use Langchain with Embeddings with the Infinity Github Project.

Imports

from langchain.embeddings import InfinityEmbeddings

Optional: Make sure to start the Infinity instance

To install infinity use the following command. For further details check out the Docs on Github.

pip install infinity_emb[all]
# Install the infinity package
!pip install infinity_emb[cli,torch]
Requirement already satisfied: infinity_emb[cli] in /home/michi/langchain/.venv/lib/python3.10/site-packages (0.0.8)
WARNING: infinity-emb 0.0.8 does not provide the extra 'cli'
Requirement already satisfied: numpy>=1.20.0 in /home/michi/langchain/.venv/lib/python3.10/site-packages (from infinity_emb[cli]) (1.24.4)
WARNING: There was an error checking the latest version of pip.

Start up the server - best to be done from a separate terminal, not inside Jupyter Notebook

model=sentence-transformers/all-MiniLM-L6-v2
port=7797
infinity_emb --port $port --model-name-or-path $model

or alternativley just use docker:

model=sentence-transformers/all-MiniLM-L6-v2
port=7797
docker run -it --gpus all -p $port:$port michaelf34/infinity:latest --model-name-or-path $model --port $port

Embed your documents using your Infinity instance

documents = [
"Baguette is a dish.",
"Paris is the capital of France.",
"numpy is a lib for linear algebra",
"You escaped what I've escaped - You'd be in Paris getting fucked up too",
]
query = "Where is Paris?"
#
infinity_api_url = "http://localhost:7797/v1"
# model is currently not validated.
embeddings = InfinityEmbeddings(
model="sentence-transformers/all-MiniLM-L6-v2", infinity_api_url=infinity_api_url
)
try:
documents_embedded = embeddings.embed_documents(documents)
query_result = embeddings.embed_query(query)
print("embeddings created successful")
except Exception as ex:
print(
"Make sure the infinity instance is running. Verify by clicking on "
f"{infinity_api_url.replace('v1','docs')} Exception: {ex}. "
)
embeddings created successful
# (demo) compute similarity
import numpy as np

scores = np.array(documents_embedded) @ np.array(query_result).T
dict(zip(documents, scores))
{'Baguette is a dish.': 0.31344215908661155,
'Paris is the capital of France.': 0.8148670296896388,
'numpy is a lib for linear algebra': 0.004429399861302009,
"You escaped what I've escaped - You'd be in Paris getting fucked up too": 0.5088476180154582}