add fake embeddings class (#1503)

This commit is contained in:
Harrison Chase 2023-03-07 15:23:46 -08:00 committed by GitHub
parent 27104d4921
commit 3610ef2830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 1 deletions

View File

@ -463,6 +463,64 @@
"source": [ "source": [
"query_result = embeddings.embed_query(text)" "query_result = embeddings.embed_query(text)"
] ]
},
{
"cell_type": "markdown",
"id": "f9c02c78",
"metadata": {},
"source": [
"## Fake Embeddings\n",
"\n",
"LangChain also provides a fake embedding class. You can use this to test your pipelines."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2ffc2e4b",
"metadata": {},
"outputs": [],
"source": [
"from langchain.embeddings import FakeEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "80777571",
"metadata": {},
"outputs": [],
"source": [
"embeddings = FakeEmbeddings(size=1352)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3ec9d8f0",
"metadata": {},
"outputs": [],
"source": [
"query_result = embeddings.embed_query(\"foo\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3b9ae9e1",
"metadata": {},
"outputs": [],
"source": [
"doc_results = embeddings.embed_documents([\"foo\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88d366bd",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
@ -481,7 +539,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.9" "version": "3.9.1"
}, },
"vscode": { "vscode": {
"interpreter": { "interpreter": {

View File

@ -3,6 +3,7 @@ import logging
from typing import Any from typing import Any
from langchain.embeddings.cohere import CohereEmbeddings from langchain.embeddings.cohere import CohereEmbeddings
from langchain.embeddings.fake import FakeEmbeddings
from langchain.embeddings.huggingface import ( from langchain.embeddings.huggingface import (
HuggingFaceEmbeddings, HuggingFaceEmbeddings,
HuggingFaceInstructEmbeddings, HuggingFaceInstructEmbeddings,
@ -28,6 +29,7 @@ __all__ = [
"SelfHostedEmbeddings", "SelfHostedEmbeddings",
"SelfHostedHuggingFaceEmbeddings", "SelfHostedHuggingFaceEmbeddings",
"SelfHostedHuggingFaceInstructEmbeddings", "SelfHostedHuggingFaceInstructEmbeddings",
"FakeEmbeddings",
] ]

View File

@ -0,0 +1,19 @@
from typing import List
import numpy as np
from pydantic import BaseModel
from langchain.embeddings.base import Embeddings
class FakeEmbeddings(Embeddings, BaseModel):
size: int
def _get_embedding(self) -> List[float]:
return list(np.random.normal(size=self.size))
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return [self._get_embedding() for _ in texts]
def embed_query(self, text: str) -> List[float]:
return self._get_embedding()