Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Fixes

Tomaz Bratanicff26c02e0516
diff --git a/docs/api/retrieval_model_clients/Neo4jRM.md b/docs/api/retrieval_model_clients/Neo4jRM.md
index dc4530f09..2bb2ddd2f 100644
--- a/docs/api/retrieval_model_clients/Neo4jRM.md
+++ b/docs/api/retrieval_model_clients/Neo4jRM.md
@@ -28,6 +28,8 @@ You need to define the credentials as environment variables:
- `NEO4J_DATABASE` (_str_, optional): Specifies the name of the database to connect to within the Neo4j instance. If not set, the system defaults to using `"neo4j"` as the database name. This allows for flexibility in connecting to different databases within a single Neo4j server.
+- `OPENAI_API_KEY` (_str_): Specifies the API key required for authenticiating with OpenAI's services.
+
**Parameters:**
- `index_name` (_str_): Specifies the name of the vector index to be used within Neo4j for organizing and querying data.
- `text_node_property` (_str_, _optional_): Defines the specific property of nodes that will be returned.
@@ -67,6 +69,7 @@ import os
os.environ["NEO4J_URI"] = 'bolt://localhost:7687'
os.environ["NEO4J_USERNAME"] = 'neo4j'
os.environ["NEO4J_PASSWORD"] = 'password'
+os.environ["OPENAI_API_KEY"] = 'sk-'
retriever_model = Neo4jRM(
index_name="vector",
diff --git a/dspy/retrieve/neo4j_rm.py b/dspy/retrieve/neo4j_rm.py
index db0c671ad..bb5ad2523 100644
--- a/dspy/retrieve/neo4j_rm.py
+++ b/dspy/retrieve/neo4j_rm.py
@@ -53,6 +53,52 @@ DEFAULT_INDEX_QUERY = "CALL db.index.vector.queryNodes($index, $k, $embedding) Y
class Neo4jRM(dspy.Retrieve):
+ """
+ Implements a retriever that utilizes Neo4j for retrieving passages.
+ This class manages a connection to a Neo4j database using official Neo4j Python drivers and requires
+ the database credentials (username, password, URI, and optionally the database name) to be set as environment variables.
+ Additionally, it utilizes an embedding provider (defaulting to OpenAI's services) to compute query embeddings,
+ which are then used to find the most relevant nodes in the Neo4j graph based on the specified node property or custom retrieval query.
+
+ Returns a list of passages in the form of `dspy.Prediction` objects.
+
+ Args:
+ index_name (str): The name of the vector index in the Neo4j database to query against.
+ text_node_property (Optional[str]): The property of the node containing the text. Required if `retrieval_query` is not set.
+ k (Optional[int]): The default number of top passages to retrieve. Defaults to 5.
+ retrieval_query (Optional[str]): Custom Cypher query for retrieving passages. Required if `text_node_property` is not set.
+ embedding_provider (str): The provider of the embedding service. Defaults to "openai".
+ embedding_model (str): The model identifier for generating embeddings. Defaults to "text-embedding-ada-002".
+
+ Examples:
+ Below is a code snippet showcasing how to initialize Neo4jRM with environment variables for the database connection and OpenAI as the embedding provider:
+
+ ```python
+ import os
+
+ import dspy
+ import openai
+
+ os.environ["NEO4J_URI"] = "bolt://localhost:7687"
+ os.environ["NEO4J_USERNAME"] = "neo4j"
+ os.environ["NEO4J_PASSWORD"] = "password"
+ os.environ["OPENAI_API_KEY"] = "sk-"
+
+ neo4j_retriever = Neo4jRM(
+ index_name="myIndex",
+ text_node_property="text",
+ k=10,
+ embedding_provider="openai",
+ embedding_model="text-embedding-ada-002",
+ )
+
+ dspy.settings.configure(rm=neo4j_retriever)
+ ```
+
+ In this example, `Neo4jRM` is configured to retrieve nodes based on the "text" property from an index named "myIndex",
+ using embeddings computed by OpenAI's "text-embedding-ada-002" model.
+ """
+
def __init__(
self,
index_name: str,
@@ -96,12 +142,17 @@ class Neo4jRM(dspy.Retrieve):
query_or_queries = [query_or_queries]
query_vectors = self.embedder(query_or_queries)
contents = []
- retrieval_query = self.retrieval_query or f"RETURN node.{self.text_node_property} AS text"
+ retrieval_query = self.retrieval_query or f"RETURN node.{self.text_node_property} AS text, score"
for vector in query_vectors:
records, _, _ = self.driver.execute_query(
DEFAULT_INDEX_QUERY + retrieval_query,
{"embedding": vector, "index": self.index_name, "k": k or self.k},
database_=self.database,
)
- contents.extend([dotdict({"long_text": r["text"]}) for r in records])
- return contents
+ contents.extend([{"passage": dotdict({"long_text": r["text"]}), "score": r["score"]} for r in records])
+ sorted_passages = sorted(
+ contents,
+ key=lambda x: x["score"],
+ reverse=True,
+ )[: k or self.k]
+ return [el["passage"] for el in sorted_passages]