참고소스 수정본
This commit is contained in:
38
참고/neo4j-graphrag-python-main/examples/customize/retrievers/external/weaviate/README.md
vendored
Normal file
38
참고/neo4j-graphrag-python-main/examples/customize/retrievers/external/weaviate/README.md
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
### Start services locally
|
||||
|
||||
This is a manual task you need to do in the terminal.
|
||||
|
||||
This spins up Neo4j and Weaviate containers and is configuring Weaviate to use embeddings from Hugging Face's Sentence Transformers using the "all-MiniLM-L6-v2" model, which has 384 dimensions.
|
||||
|
||||
```bash
|
||||
docker compose -f tests/e2e/docker-compose.yml up
|
||||
```
|
||||
|
||||
### Write data (once)
|
||||
|
||||
Run this from the project root to write data to both dbs.
|
||||
|
||||
```
|
||||
uv run python -m tests/e2e/weaviate_e2e/populate_dbs.py
|
||||
```
|
||||
|
||||
### Install Weaviate client
|
||||
|
||||
You need to install the `weaviate-client` package to use this retriever.
|
||||
|
||||
```bash
|
||||
pip install weaviate-client
|
||||
```
|
||||
|
||||
### Search
|
||||
|
||||
```
|
||||
# search by vector
|
||||
uv run python -m examples.customize.retrievers.external.weaviate.vector_search
|
||||
|
||||
# search by text, with embeddings generated locally (via embedder argument)
|
||||
uv run python -m examples.customize.retrievers.external.weaviate.text_search_local_embedder
|
||||
|
||||
# search by text, with embeddings generated on the Weaviate side, via configured vectorizer
|
||||
uv run python -m examples.customize.retrievers.external.weaviate.text_search_remote_embedder
|
||||
```
|
||||
@@ -0,0 +1,40 @@
|
||||
"""This example demonstrates how to use WeaviateNeo4jRetriever, ie vectors are
|
||||
stored in the Weaviate database.
|
||||
|
||||
See the [README](./README.md) for more
|
||||
information about how spin up a Weaviate and Neo4j databases if needed.
|
||||
|
||||
In this example, we are embeddings a text and provide a local embeder to the
|
||||
WeaviateNeo4jRetriever.
|
||||
"""
|
||||
|
||||
from neo4j import GraphDatabase
|
||||
from neo4j_graphrag.embeddings.sentence_transformers import (
|
||||
SentenceTransformerEmbeddings,
|
||||
)
|
||||
from neo4j_graphrag.retrievers import WeaviateNeo4jRetriever
|
||||
from weaviate.connect.helpers import connect_to_local
|
||||
|
||||
NEO4J_URL = "neo4j://localhost:7687"
|
||||
NEO4J_AUTH = ("neo4j", "password")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
with GraphDatabase.driver(NEO4J_URL, auth=NEO4J_AUTH) as neo4j_driver:
|
||||
with connect_to_local() as w_client:
|
||||
embedder = SentenceTransformerEmbeddings(model="all-MiniLM-L6-v2")
|
||||
retriever = WeaviateNeo4jRetriever(
|
||||
driver=neo4j_driver,
|
||||
client=w_client,
|
||||
collection="Jeopardy",
|
||||
id_property_external="neo4j_id",
|
||||
id_property_neo4j="id",
|
||||
embedder=embedder,
|
||||
)
|
||||
|
||||
res = retriever.search(query_text="biology", top_k=2)
|
||||
print(res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,37 @@
|
||||
"""This example demonstrates how to use WeaviateNeo4jRetriever, ie vectors are
|
||||
stored in the Weaviate database.
|
||||
|
||||
See the [README](./README.md) for more
|
||||
information about how spin up a Weaviate and Neo4j databases if needed.
|
||||
|
||||
In this example, we are embeddings a text and provide a remote embeder to the
|
||||
WeaviateNeo4jRetriever.
|
||||
"""
|
||||
|
||||
from neo4j import GraphDatabase
|
||||
from neo4j_graphrag.retrievers import WeaviateNeo4jRetriever
|
||||
from weaviate.connect.helpers import connect_to_local
|
||||
|
||||
NEO4J_URL = "neo4j://localhost:7687"
|
||||
NEO4J_AUTH = ("neo4j", "password")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
with GraphDatabase.driver(NEO4J_URL, auth=NEO4J_AUTH) as neo4j_driver:
|
||||
with connect_to_local() as w_client:
|
||||
retriever = WeaviateNeo4jRetriever(
|
||||
driver=neo4j_driver,
|
||||
client=w_client,
|
||||
collection="Jeopardy",
|
||||
id_property_external="neo4j_id",
|
||||
id_property_neo4j="id",
|
||||
)
|
||||
|
||||
# Weaviate is configured to embed the text on the server side
|
||||
# see populate_dbs.py for the configuration
|
||||
res = retriever.search(query_text="biology", top_k=2)
|
||||
print(res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,34 @@
|
||||
"""This example demonstrates how to use WeaviateNeo4jRetriever, ie vectors are
|
||||
stored in the Weaviate database.
|
||||
|
||||
See the [README](./README.md) for more
|
||||
information about how spin up a Weaviate and Neo4j databases if needed.
|
||||
|
||||
In this example, search is performed from an already existing vector.
|
||||
"""
|
||||
|
||||
from embedding_biology import EMBEDDING_BIOLOGY
|
||||
from neo4j import GraphDatabase
|
||||
from neo4j_graphrag.retrievers import WeaviateNeo4jRetriever
|
||||
from weaviate.connect.helpers import connect_to_local
|
||||
|
||||
NEO4J_URL = "neo4j://localhost:7687"
|
||||
NEO4J_AUTH = ("neo4j", "password")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
with GraphDatabase.driver(NEO4J_URL, auth=NEO4J_AUTH) as neo4j_driver:
|
||||
with connect_to_local() as w_client:
|
||||
retriever = WeaviateNeo4jRetriever(
|
||||
driver=neo4j_driver,
|
||||
client=w_client,
|
||||
collection="Jeopardy",
|
||||
id_property_external="neo4j_id",
|
||||
id_property_neo4j="id",
|
||||
)
|
||||
res = retriever.search(query_vector=EMBEDDING_BIOLOGY, top_k=2)
|
||||
print(res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user