Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

DashVector

DashVector is a fully-managed vectorDB service that supports high-dimension dense and sparse vectors, real-time insertion and filtered search. It is built to scale automatically and can adapt to different application requirements.

This notebook shows how to use functionality related to theDashVector vector database.

To use DashVector, you must have an API key.Here are theinstallation instructions.

Install

%pip install--upgrade--quiet  langchain-community dashvector dashscope

We want to useDashScopeEmbeddings so we also have to get the Dashscope API Key.

import getpass
import os

if"DASHVECTOR_API_KEY"notin os.environ:
os.environ["DASHVECTOR_API_KEY"]= getpass.getpass("DashVector API Key:")
if"DASHSCOPE_API_KEY"notin os.environ:
os.environ["DASHSCOPE_API_KEY"]= getpass.getpass("DashScope API Key:")

Example

from langchain_community.embeddings.dashscopeimport DashScopeEmbeddings
from langchain_community.vectorstoresimport DashVector
from langchain_text_splittersimport CharacterTextSplitter
from langchain_community.document_loadersimport TextLoader

loader= TextLoader("../../how_to/state_of_the_union.txt")
documents= loader.load()
text_splitter= CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs= text_splitter.split_documents(documents)

embeddings= DashScopeEmbeddings()
API Reference:TextLoader

We can create DashVector from documents.

dashvector= DashVector.from_documents(docs, embeddings)

query="What did the president say about Ketanji Brown Jackson"
docs= dashvector.similarity_search(query)
print(docs)

We can add texts with meta datas and ids, and search with meta filter.

texts=["foo","bar","baz"]
metadatas=[{"key": i}for iinrange(len(texts))]
ids=["0","1","2"]

dashvector.add_texts(texts, metadatas=metadatas, ids=ids)

docs= dashvector.similarity_search("foo",filter="key = 2")
print(docs)
[Document(page_content='baz', metadata={'key': 2})]

Operating bandpartition parameters

Thepartition parameter defaults to default, and if a non-existentpartition parameter is passed in, thepartition will be created automatically.

texts=["foo","bar","baz"]
metadatas=[{"key": i}for iinrange(len(texts))]
ids=["0","1","2"]
partition="langchain"

# add texts
dashvector.add_texts(texts, metadatas=metadatas, ids=ids, partition=partition)

# similarity search
query="What did the president say about Ketanji Brown Jackson"
docs= dashvector.similarity_search(query, partition=partition)

# delete
dashvector.delete(ids=ids, partition=partition)

Related


[8]ページ先頭

©2009-2025 Movatter.jp