-
Notifications
You must be signed in to change notification settings - Fork 16.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langchain[minor]: Adding
infinity
embedding integration. (#13928)
This adds integation to https://github.com/michaelfeil/infinity. Users requested it in michaelfeil/infinity#36 @saatvikshah Follows my implementation of gradient.ai. Feedback 1: Well done - I love your CI / repo / poetry setup - I adapted a lot in https://github.com/michaelfeil/infinity. Feedback 2: Not so good: The openai integration contains to much reverse engineering - in general projects such as michaelfeil/infinity and huggingface/text-embeddings-inference are compatible to the `pip install openai` package. Reverse engineering like this one is really hindering the use for me: https://github.com/langchain-ai/langchain/blob/8e88ba16a88affc272da4bb99bd515b714781179/libs/langchain/langchain/embeddings/openai.py#L347 https://github.com/langchain-ai/langchain/blob/8e88ba16a88affc272da4bb99bd515b714781179/libs/langchain/langchain/embeddings/openai.py#L351 - it is about preventing 3rd party providers to use the same url + uses interfaces of openai, that are not publically documented.
- Loading branch information
1 parent
10a6e7c
commit 6861626
Showing
6 changed files
with
629 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Infinity | ||
|
||
>[Infinity](https://github.com/michaelfeil/infinity) allows the creation of text embeddings. | ||
## Text Embedding Model | ||
|
||
There exists an infinity Embedding model, which you can access with | ||
```python | ||
from langchain.embeddings import InfinityEmbeddings | ||
``` | ||
For a more detailed walkthrough of this, see [this notebook](/docs/integrations/text_embedding/infinity) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Infinity\n", | ||
"\n", | ||
"`Infinity` allows to create `Embeddings` using a MIT-licensed Embedding Server. \n", | ||
"\n", | ||
"This notebook goes over how to use Langchain with Embeddings with the [Infinity Github Project](https://github.com/michaelfeil/infinity).\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain.embeddings import InfinityEmbeddings" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Optional: Make sure to start the Infinity instance\n", | ||
"\n", | ||
"To install infinity use the following command. For further details check out the [Docs on Github](https://github.com/michaelfeil/infinity).\n", | ||
"```bash\n", | ||
"pip install infinity_emb[all]\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Requirement already satisfied: infinity_emb[cli] in /home/michi/langchain/.venv/lib/python3.10/site-packages (0.0.8)\n", | ||
"\u001b[33mWARNING: infinity-emb 0.0.8 does not provide the extra 'cli'\u001b[0m\u001b[33m\n", | ||
"\u001b[0mRequirement already satisfied: numpy>=1.20.0 in /home/michi/langchain/.venv/lib/python3.10/site-packages (from infinity_emb[cli]) (1.24.4)\n", | ||
"\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n", | ||
"\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Install the infinity package\n", | ||
"!pip install infinity_emb[cli,torch]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Start up the server - best to be done from a separate terminal, not inside Jupyter Notebook\n", | ||
"\n", | ||
"```bash\n", | ||
"model=sentence-transformers/all-MiniLM-L6-v2\n", | ||
"port=7797\n", | ||
"infinity_emb --port $port --model-name-or-path $model\n", | ||
"```\n", | ||
"\n", | ||
"or alternativley just use docker:\n", | ||
"```bash\n", | ||
"model=sentence-transformers/all-MiniLM-L6-v2\n", | ||
"port=7797\n", | ||
"docker run -it --gpus all -p $port:$port michaelf34/infinity:latest --model-name-or-path $model --port $port\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Embed your documents using your Infinity instance " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"documents = [\n", | ||
" \"Baguette is a dish.\",\n", | ||
" \"Paris is the capital of France.\",\n", | ||
" \"numpy is a lib for linear algebra\",\n", | ||
" \"You escaped what I've escaped - You'd be in Paris getting fucked up too\",\n", | ||
"]\n", | ||
"query = \"Where is Paris?\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"embeddings created successful\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#\n", | ||
"infinity_api_url = \"http://localhost:7797/v1\"\n", | ||
"# model is currently not validated.\n", | ||
"embeddings = InfinityEmbeddings(\n", | ||
" model=\"sentence-transformers/all-MiniLM-L6-v2\", infinity_api_url=infinity_api_url\n", | ||
")\n", | ||
"try:\n", | ||
" documents_embedded = embeddings.embed_documents(documents)\n", | ||
" query_result = embeddings.embed_query(query)\n", | ||
" print(\"embeddings created successful\")\n", | ||
"except Exception as ex:\n", | ||
" print(\n", | ||
" \"Make sure the infinity instance is running. Verify by clicking on \"\n", | ||
" f\"{infinity_api_url.replace('v1','docs')} Exception: {ex}. \"\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'Baguette is a dish.': 0.31344215908661155,\n", | ||
" 'Paris is the capital of France.': 0.8148670296896388,\n", | ||
" 'numpy is a lib for linear algebra': 0.004429399861302009,\n", | ||
" \"You escaped what I've escaped - You'd be in Paris getting fucked up too\": 0.5088476180154582}" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# (demo) compute similarity\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"scores = np.array(documents_embedded) @ np.array(query_result).T\n", | ||
"dict(zip(documents, scores))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
}, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.