DSPy: The framework for programming—not prompting—language models
Python
Add docs & some improvements
Tomaz Bratanic1754cbd37d84
diff --git a/docs/api/retrieval_model_clients/Neo4jRM.md b/docs/api/retrieval_model_clients/Neo4jRM.mdnew file mode 100644index 000000000..dc4530f09--- /dev/null+++ b/docs/api/retrieval_model_clients/Neo4jRM.md@@ -0,0 +1,80 @@++# retrieve.neo4j_rm++### Constructor++Initialize an instance of the `Neo4jRM` class.++```python+Neo4jRM(+ index_name: str,+ text_node_property: str,+ k: int = 5,+ retrieval_query: str = None,+ embedding_provider: str = "openai",+ embedding_model: str = "text-embedding-ada-002",+)+```++**Environment Variables:**++You need to define the credentials as environment variables:++- `NEO4J_USERNAME` (_str_): Specifies the username required for authenticating with the Neo4j database. This is a crucial security measure to ensure that only authorized users can access the database.++- `NEO4J_PASSWORD` (_str_): Defines the password associated with the `NEO4J_USERNAME` for authentication purposes. This password should be kept secure to prevent unauthorized access to the database.++- `NEO4J_URI` (_str_): Indicates the Uniform Resource Identifier (URI) used to connect to the Neo4j database. This URI typically includes the protocol, hostname, and port, providing the necessary information to establish a connection to the database.++- `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.++**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.+- `k` (_int_, _optional_): The number of top results to return from the retrieval operation. It defaults to 5 if not explicitly specified.+- `retrieval_query` (_str_, _optional_): A custom query string provided for retrieving data. If not provided, a default query tailored to the `text_node_property` will be used.+- `embedding_provider` (_str_, _optional_): The name of the service provider for generating embeddings. Defaults to "openai" if not specified.+- `embedding_model` (_str_, _optional_): The specific embedding model to use from the provider. By default, it uses the "text-embedding-ada-002" model from OpenAI.+++### Methods++#### `forward(self, query: [str], k: Optional[int] = None) -> dspy.Prediction`++Search the neo4j vector index for the top `k` passages matching the given query or queries, using embeddings generated via the specified `embedding_model`.++**Parameters:**+- `query` (str_): The query.+- `k` (_Optional[int]_, _optional_): The number of results to retrieve. If not specified, defaults to the value set during initialization.++**Returns:**+- `dspy.Prediction`: Contains the retrieved passages as a list of string with the prediction signature.++ex:+```python+Prediction(+ passages=['Passage 1 Lorem Ipsum awesome', 'Passage 2 Lorem Ipsum Youppidoo', 'Passage 3 Lorem Ipsum Yassssss']+)+```++### Quick Example how to use Neo4j in a local environment.+++```python+from dspy.retrieve.neo4j_rm import Neo4jRM+import os++os.environ["NEO4J_URI"] = 'bolt://localhost:7687'+os.environ["NEO4J_USERNAME"] = 'neo4j'+os.environ["NEO4J_PASSWORD"] = 'password'++retriever_model = Neo4jRM(+ index_name="vector",+ text_node_property="text"+)++results = retriever_model("Explore the significance of quantum computing", k=3)++for passage in results.passages:+ print("Document:", result, "\n")+```diff --git a/dspy/retrieve/neo4j_rm.py b/dspy/retrieve/neo4j_rm.pyindex 2c5998c49..db0c671ad 100644--- a/dspy/retrieve/neo4j_rm.py+++ b/dspy/retrieve/neo4j_rm.py@@ -56,7 +56,7 @@ class Neo4jRM(dspy.Retrieve):def __init__(self,index_name: str,- text_node_property: str,+ text_node_property: str = None,k: int = 5,retrieval_query: str = None,embedding_provider: str = "openai",@@ -67,7 +67,7 @@ class Neo4jRM(dspy.Retrieve):self.username = os.getenv("NEO4J_USERNAME")self.password = os.getenv("NEO4J_PASSWORD")self.uri = os.getenv("NEO4J_URI")- self.database = os.getenv("NEO4J_DATABASE")+ self.database = os.getenv("NEO4J_DATABASE", "neo4j")self.k = kself.retrieval_query = retrieval_queryself.text_node_property = text_node_property@@ -77,6 +77,8 @@ class Neo4jRM(dspy.Retrieve):raise ValueError("Environment variable NEO4J_PASSWORD must be set")if not self.uri:raise ValueError("Environment variable NEO4J_URI must be set")+ if not self.text_node_property and not self.retrieval_query:+ raise ValueError("Either `text_node_property` or `retrieval_query` parameters must be defined")try:self.driver = GraphDatabase.driver(self.uri, auth=(self.username, self.password))self.driver.verify_connectivity()@@ -99,6 +101,7 @@ class Neo4jRM(dspy.Retrieve):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