Elasticsearch嵌入式向量¶
如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-vector-stores-elasticsearch
%pip install llama-index-embeddings-elasticsearch
%pip install llama-index-vector-stores-elasticsearch
%pip install llama-index-embeddings-elasticsearch
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
In [ ]:
Copied!
# 导入
from llama_index.embeddings.elasticsearch import ElasticsearchEmbedding
from llama_index.vector_stores.elasticsearch import ElasticsearchStore
from llama_index.core import StorageContext,VectorStoreIndex
from llama_index.core import Settings
# 导入
from llama_index.embeddings.elasticsearch import ElasticsearchEmbedding
from llama_index.vector_stores.elasticsearch import ElasticsearchStore
from llama_index.core import StorageContext,VectorStoreIndex
from llama_index.core import Settings
In [ ]:
Copied!
# 获取凭证并创建嵌入
import os
# 从环境变量中获取主机地址,默认为localhost:9200
host = os.environ.get("ES_HOST", "localhost:9200")
# 从环境变量中获取用户名,默认为elastic
username = os.environ.get("ES_USERNAME", "elastic")
# 从环境变量中获取密码,默认为changeme
password = os.environ.get("ES_PASSWORD", "changeme")
# 从环境变量中获取索引名称,默认为your-index-name
index_name = os.environ.get("INDEX_NAME", "your-index-name")
# 从环境变量中获取模型ID,默认为your-model-id
model_id = os.environ.get("MODEL_ID", "your-model-id")
# 通过凭证创建嵌入
embeddings = ElasticsearchEmbedding.from_credentials(
model_id=model_id, es_url=host, es_username=username, es_password=password
)
# 获取凭证并创建嵌入
import os
# 从环境变量中获取主机地址,默认为localhost:9200
host = os.environ.get("ES_HOST", "localhost:9200")
# 从环境变量中获取用户名,默认为elastic
username = os.environ.get("ES_USERNAME", "elastic")
# 从环境变量中获取密码,默认为changeme
password = os.environ.get("ES_PASSWORD", "changeme")
# 从环境变量中获取索引名称,默认为your-index-name
index_name = os.environ.get("INDEX_NAME", "your-index-name")
# 从环境变量中获取模型ID,默认为your-model-id
model_id = os.environ.get("MODEL_ID", "your-model-id")
# 通过凭证创建嵌入
embeddings = ElasticsearchEmbedding.from_credentials(
model_id=model_id, es_url=host, es_username=username, es_password=password
)
In [ ]:
Copied!
# 设置全局设置
Settings.embed_model = embeddings
Settings.chunk_size = 512
# 设置全局设置
Settings.embed_model = embeddings
Settings.chunk_size = 512
In [ ]:
Copied!
# 用于与elasticsearch向量存储一起使用
vector_store = ElasticsearchStore(
index_name=index_name, es_url=host, es_user=username, es_password=password
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_vector_store(
vector_store=vector_store,
storage_context=storage_context,
)
query_engine = index.as_query_engine()
response = query_engine.query("hello world")
# 用于与elasticsearch向量存储一起使用
vector_store = ElasticsearchStore(
index_name=index_name, es_url=host, es_user=username, es_password=password
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_vector_store(
vector_store=vector_store,
storage_context=storage_context,
)
query_engine = index.as_query_engine()
response = query_engine.query("hello world")