langchain 基于ES的数据向量化存储和检索

中文向量化模型候选:

1、sentence-transformers/all-MiniLM-L6-v2 向量维度为384维,支持多种语言。

2、BAAI/bge-m3

3、多语言模型:BAAI/bge-m3 支持的输入长度<=8192

from langchain_community.embeddings import HuggingFaceBgeEmbeddings

model_name = "sentence-transformers/all-MiniLM-L6-v2"

model_kwargs = {"device": "cpu"}

encode_kwargs = {"normalize_embeddings": True}

embeddings = HuggingFaceBgeEmbeddings(

model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs

)

1·、存储源为elasticsearch

from typing import Any, Dict, Iterable

from elasticsearch import Elasticsearch

from elasticsearch.helpers import bulk

from langchain.embeddings import DeterministicFakeEmbedding

from langchain_core.documents import Document

from langchain_core.embeddings import Embeddings

from langchain_elasticsearch import ElasticsearchRetriever

es_url = "http://user:password@localhost:9200"

es_client = Elasticsearch(hosts=[es_url])

es_client.info()

index_name = "test-langchain-retriever"

text_field = "text"

dense_vector_field = "fake_embedding"

num_characters_field = "num_characters"

texts = [

"foo",

"bar",

"world",

"hello world",

"hello",

"foo bar",

"bla bla foo",

]

def create_index(

es_client: Elasticsearch,

index_name: str,

text_field: str,

dense_vector_field: str,

num_characters_field: str,

):

es_client.indices.create(

index=index_name,

mappings={

"properties": {

text_field: {"type": "text"},

dense_vector_field: {"type": "dense_vector"},

num_characters_field: {"type": "integer"},

}

},

)

def index_data(

es_client: Elasticsearch,

index_name: str,

text_field: str,

dense_vector_field: str,

embeddings: Embeddings,

texts: Iterable[str],

refresh: bool = True,

) -> None:

create_index(

es_client, index_name, text_field, dense_vector_field, num_characters_field

)

vectors = embeddings.embed_documents(list(texts))

requests = [

{

"_op_type": "index",

"_index": index_name,

"_id": i,

text_field: text,

dense_vector_field: vector,

num_characters_field: len(text),

}

for i, (text, vector) in enumerate(zip(texts, vectors))

]

bulk(es_client, requests)

if refresh:

es_client.indices.refresh(index=index_name)

index_data(es_client, index_name, text_field, dense_vector_field, embeddings, texts)

2、elasticsearch 向量检索:

es_url = "http://user:password@localhost:9200"

index_name = "test-langchain-retriever"

text_field = "text"

dense_vector_field = "fake_embedding"

num_characters_field = "num_characters"

def gen_dsl(search_query: str) -> Dict:

vector = embeddings.embed_query(search_query) # same embeddings as for indexing

return {

"knn": {

"field": dense_vector_field,

"query_vector": vector,

"k": 5,

"num_candidates": 10,

}

}

vector_retriever = ElasticsearchRetriever.from_es_params(

index_name=index_name,

body_func=vector_query,

content_field=text_field,

url=es_url,

)

vector_retriever.invoke("foo")

说明:简单的向量检索,耗时比较长。

原因:1、直接对全局使用了余弦相似度计算。(cos),未做任何优化

2、返回数据将向量内容全部返回

相关推荐
CM莫问2 天前
python实战(七)——基于LangChain的RAG实践
人工智能·python·深度学习·算法·自然语言处理·chatgpt·langchain
xnuscd3 天前
langgraph_plan_and_execute
langchain
花千树-0104 天前
LangChain教程 - 创建 ReAct 风格智能代理
人工智能·langchain·aigc·ai编程
发菜君5 天前
LangChain大模型应用开发指南:打造个性化LLM
人工智能·学习·langchain·大模型·大模型学习·大模型入门·大模型教程
waiting不是违停7 天前
LangChain Ollama实战文献检索助手(二)少样本提示FewShotPromptTemplate示例选择器
langchain·llm·ollama
Y24834908917 天前
05LangChain实战课 - 提示工程与FewShotPromptTemplate的应用
人工智能·langchain
科研小达人8 天前
Langchain调用模型使用FAISS
python·chatgpt·langchain·faiss
小陈phd10 天前
大语言模型及LangChain介绍
人工智能·语言模型·langchain
写程序的小火箭11 天前
如何评估一个RAG系统(RAGas评测框架)-下篇
人工智能·gpt·语言模型·chatgpt·langchain
Stitch .11 天前
小北的字节跳动青训营与 LangChain 实战课:探索 AI 技术的新边界(持续更新中~~~)
人工智能·python·gpt·ai·语言模型·chatgpt·langchain