6大核心模块(Modules)
示例(Examples)
Annoy

LangChain

Annoy #

“烦恼(Approximate Nearest Neighbors Oh Yeah)是一个C++库,带有Python绑定,用于搜索与给定查询点接近的空间点。它还创建了大型的只读基于文件的数据结构,这些数据结构被映射到内存中,以便许多进程可以共享相同的数据。”

本教程展示了如何使用与Annoy向量数据库相关的功能。

通过Annoy (opens in a new tab)

注意:Annoy是只读的 - 一旦索引建立,您就不能再添加任何嵌入! 如果您想逐步向VectorStore添加新条目,则最好选择其他选项!

从文本创建VectorStore#

from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Annoy
 
embeddings_func = HuggingFaceEmbeddings()
 
texts = ["pizza is great", "I love salad", "my car", "a dog"]
 
# default metric is angular
vector_store = Annoy.from_texts(texts, embeddings_func)
 
# allows for custom annoy parameters, defaults are n_trees=100, n_jobs=-1, metric="angular"
vector_store_v2 = Annoy.from_texts(
    texts, embeddings_func, metric="dot", n_trees=100, n_jobs=1
)
 
vector_store.similarity_search("food", k=3)
 
[Document(page_content='pizza is great', metadata={}),
 Document(page_content='I love salad', metadata={}),
 Document(page_content='my car', metadata={})]
 
# the score is a distance metric, so lower is better
vector_store.similarity_search_with_score("food", k=3)
 
[(Document(page_content='pizza is great', metadata={}), 1.0944390296936035),
 (Document(page_content='I love salad', metadata={}), 1.1273186206817627),
 (Document(page_content='my car', metadata={}), 1.1580758094787598)]
 

从文档创建VectorStore#

from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
 
loader = TextLoader("../../../state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
 
docs[:5]
 
[Document(page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.    Last year COVID-19 kept us apart. This year we are finally together again.   Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans.   With a duty to one another to the American people to the Constitution.   And with an unwavering resolve that freedom will always triumph over tyranny.   Six days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated.   He thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined.   He met the Ukrainian people.   From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.', metadata={'source': '../../../state_of_the_union.txt'}),
 Document(page_content='Groups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.   In this struggle as President Zelenskyy said in his speech to the European Parliament “Light will win over darkness.” The Ukrainian Ambassador to the United States is here tonight.   Let each of us here tonight in this Chamber send an unmistakable signal to Ukraine and to the world.   Please rise if you are able and show that, Yes, we the United States of America stand with the Ukrainian people.   Throughout our history we’ve learned this lesson when dictators do not pay a price for their aggression they cause more chaos.     They keep moving.     And the costs and the threats to America and the world keep rising.     That’s why the NATO Alliance was created to secure peace and stability in Europe after World War 2.   The United States is a member along with 29 other nations.   It matters. American diplomacy matters. American resolve matters.', metadata={'source': '../../../state_of_the_union.txt'}),
 Document(page_content='Putin’s latest attack on Ukraine was premeditated and unprovoked.   He rejected repeated efforts at diplomacy.   He thought the West and NATO wouldn’t respond. And he thought he could divide us at home. Putin was wrong. We were ready.  Here is what we did.     We prepared extensively and carefully.   We spent months building a coalition of other freedom-loving nations from Europe and the Americas to Asia and Africa to confront Putin.   I spent countless hours unifying our European allies. We shared with the world in advance what we knew Putin was planning and precisely how he would try to falsely justify his aggression.    We countered Russia’s lies with truth.     And now that he has acted the free world is holding him accountable.   Along with twenty-seven members of the European Union including France, Germany, Italy, as well as countries like the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.', metadata={'source': '../../../state_of_the_union.txt'}),
 Document(page_content='We are inflicting pain on Russia and supporting the people of Ukraine. Putin is now isolated from the world more than ever.   Together with our allies –we are right now enforcing powerful economic sanctions.   We are cutting off Russia’s largest banks from the international financial system.    Preventing Russia’s central bank from defending the Russian Ruble making Putin’s $630 Billion “war fund” worthless.     We are choking off Russia’s access to technology that will sap its economic strength and weaken its military for years to come.    Tonight I say to the Russian oligarchs and corrupt leaders who have bilked billions of dollars off this violent regime no more.   The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs.    We are joining with our European allies to find and seize your yachts your luxury apartments your private jets. We are coming for your ill-begotten gains.', metadata={'source': '../../../state_of_the_union.txt'}),
 Document(page_content='And tonight I am announcing that we will join our allies in closing off American air space to all Russian flights – further isolating Russia – and adding an additional squeeze –on their economy. The Ruble has lost 30% of its value.   The Russian stock market has lost 40% of its value and trading remains suspended. Russia’s economy is reeling and Putin alone is to blame.   Together with our allies we are providing support to the Ukrainians in their fight for freedom. Military assistance. Economic assistance. Humanitarian assistance.   We are giving more than $1 Billion in direct assistance to Ukraine.   And we will continue to aid the Ukrainian people as they defend their country and to help ease their suffering.    Let me be clear, our forces are not engaged and will not engage in conflict with Russian forces in Ukraine.    Our forces are not going to Europe to fight in Ukraine, but to defend our NATO Allies – in the event that Putin decides to keep moving west.', metadata={'source': '../../../state_of_the_union.txt'})]
 
vector_store_from_docs = Annoy.from_documents(docs, embeddings_func)
 
query = "What did the president say about Ketanji Brown Jackson"
docs = vector_store_from_docs.similarity_search(query)
 
print(docs[0].page_content[:100])
 
Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Ac
 

通过现有嵌入创建VectorStore#

embs = embeddings_func.embed_documents(texts)
 
data = list(zip(texts, embs))
 
vector_store_from_embeddings = Annoy.from_embeddings(data, embeddings_func)
 
vector_store_from_embeddings.similarity_search_with_score("food", k=3)
 
[(Document(page_content='pizza is great', metadata={}), 1.0944390296936035),
 (Document(page_content='I love salad', metadata={}), 1.1273186206817627),
 (Document(page_content='my car', metadata={}), 1.1580758094787598)]
 

通过嵌入搜索#

motorbike_emb = embeddings_func.embed_query("motorbike")
 
vector_store.similarity_search_by_vector(motorbike_emb, k=3)
 
[Document(page_content='my car', metadata={}),
 Document(page_content='a dog', metadata={}),
 Document(page_content='pizza is great', metadata={})]
 
vector_store.similarity_search_with_score_by_vector(motorbike_emb, k=3)
 
[(Document(page_content='my car', metadata={}), 1.0870471000671387),
 (Document(page_content='a dog', metadata={}), 1.2095637321472168),
 (Document(page_content='pizza is great', metadata={}), 1.3254905939102173)]
 

通过文档存储ID搜索#

vector_store.index_to_docstore_id
 
{0: '2d1498a8-a37c-4798-acb9-0016504ed798',
 1: '2d30aecc-88e0-4469-9d51-0ef7e9858e6d',
 2: '927f1120-985b-4691-b577-ad5cb42e011c',
 3: '3056ddcf-a62f-48c8-bd98-b9e57a3dfcae'}
 
some_docstore_id = 0  # texts[0]
 
vector_store.docstore._dict[vector_store.index_to_docstore_id[some_docstore_id]]
 
Document(page_content='pizza is great', metadata={})
 
# same document has distance 0
vector_store.similarity_search_with_score_by_index(some_docstore_id, k=3)
 
[(Document(page_content='pizza is great', metadata={}), 0.0),
 (Document(page_content='I love salad', metadata={}), 1.0734446048736572),
 (Document(page_content='my car', metadata={}), 1.2895267009735107)]
 

保存和加载#

vector_store.save_local("my_annoy_index_and_docstore")
 
saving config
 
loaded_vector_store = Annoy.load_local(
    "my_annoy_index_and_docstore", embeddings=embeddings_func
)
 
# same document has distance 0
loaded_vector_store.similarity_search_with_score_by_index(some_docstore_id, k=3)
 
[(Document(page_content='pizza is great', metadata={}), 0.0),
 (Document(page_content='I love salad', metadata={}), 1.0734446048736572),
 (Document(page_content='my car', metadata={}), 1.2895267009735107)]
 

从头开始构建#

import uuid
from annoy import AnnoyIndex
from langchain.docstore.document import Document
from langchain.docstore.in_memory import InMemoryDocstore
 
metadatas = [{"x": "food"}, {"x": "food"}, {"x": "stuff"}, {"x": "animal"}]
 
# embeddings
embeddings = embeddings_func.embed_documents(texts)
 
# embedding dim
f = len(embeddings[0])
 
# index
metric = "angular"
index = AnnoyIndex(f, metric=metric)
for i, emb in enumerate(embeddings):
    index.add_item(i, emb)
index.build(10)
 
# docstore
documents = []
for i, text in enumerate(texts):
    metadata = metadatas[i] if metadatas else {}
    documents.append(Document(page_content=text, metadata=metadata))
index_to_docstore_id = {i: str(uuid.uuid4()) for i in range(len(documents))}
docstore = InMemoryDocstore(
    {index_to_docstore_id[i]: doc for i, doc in enumerate(documents)}
)
 
db_manually = Annoy(
    embeddings_func.embed_query, index, metric, docstore, index_to_docstore_id
)
 
db_manually.similarity_search_with_score("eating!", k=3)
 
[(Document(page_content='pizza is great', metadata={'x': 'food'}),
  1.1314140558242798),
 (Document(page_content='I love salad', metadata={'x': 'food'}),
  1.1668788194656372),
 (Document(page_content='my car', metadata={'x': 'stuff'}), 1.226445198059082)]