forked from neo4j/neo4j-graphrag-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
225 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/neo4j_graphrag/experimental/components/rag/prompt_builder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/neo4j_graphrag/experimental/components/rag/retrievers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/neo4j_graphrag/experimental/pipeline/config/template_pipeline/rag_pipeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from typing import ClassVar, Literal, Union, Optional, Any, cast | ||
|
||
from pydantic import ConfigDict | ||
|
||
from neo4j_graphrag.experimental.components.rag.generate import Generate | ||
from neo4j_graphrag.experimental.components.rag.prompt_builder import PromptBuilder | ||
from neo4j_graphrag.experimental.components.rag.retrievers import RetrieverWrapper | ||
from neo4j_graphrag.experimental.pipeline.config.object_config import ComponentConfig, T | ||
from neo4j_graphrag.experimental.pipeline.config.template_pipeline.base import \ | ||
TemplatePipelineConfig | ||
from neo4j_graphrag.experimental.pipeline.config.types import PipelineType | ||
from neo4j_graphrag.experimental.pipeline.types import ConnectionDefinition | ||
from neo4j_graphrag.generation import RagTemplate | ||
from neo4j_graphrag.retrievers.base import Retriever | ||
|
||
|
||
class RetrieverConfig(ComponentConfig): | ||
INTERFACE = Retriever # the result of _get_class is a Retriever | ||
# it is translated into a RetrieverWrapper (which is a Component) | ||
# in the 'parse' method below | ||
|
||
def parse(self, resolved_data: dict[str, Any] | None = None) -> RetrieverWrapper: | ||
retriever = super().parse(resolved_data) | ||
return RetrieverWrapper(retriever) | ||
|
||
|
||
class SimpleRAGPipelineConfig(TemplatePipelineConfig): | ||
COMPONENTS: ClassVar[list[str]] = [ | ||
"retriever", | ||
"prompt_builder", | ||
"generator", | ||
] | ||
retriever: RetrieverConfig | ||
prompt_template: Union[RagTemplate, str] = RagTemplate() | ||
template_: Literal[PipelineType.SIMPLE_RAG_PIPELINE] = ( | ||
PipelineType.SIMPLE_RAG_PIPELINE | ||
) | ||
|
||
model_config = ConfigDict( | ||
arbitrary_types_allowed=True | ||
) | ||
|
||
def _get_retriever(self) -> RetrieverWrapper: | ||
retriever = self.retriever.parse(self._global_data) | ||
return retriever | ||
|
||
def _get_prompt_builder(self) -> PromptBuilder: | ||
return PromptBuilder(self.prompt_template) | ||
|
||
def _get_generator(self) -> Generate: | ||
llm = self.get_default_llm() | ||
return Generate(llm) | ||
|
||
def _get_connections(self) -> list[ConnectionDefinition]: | ||
connections = [ConnectionDefinition( | ||
start="retriever", | ||
end="prompt_builder", | ||
input_config={"context": "retriever.result"}, | ||
), ConnectionDefinition( | ||
start="prompt_builder", | ||
end="generator", | ||
input_config={ | ||
"prompt": "prompt_builder.prompt", | ||
}, | ||
)] | ||
return connections | ||
|
||
def get_run_params(self, user_input: dict[str, Any]) -> dict[str, Any]: | ||
# query_text: str = "", | ||
# examples: str = "", | ||
# retriever_config: Optional[dict[str, Any]] = None, | ||
# return_context: bool | None = None, | ||
run_params = { | ||
"retriever": { | ||
"query_text": user_input["query_text"], | ||
**user_input.get("retriever_config", {}), | ||
}, | ||
"prompt_builder": { | ||
"query_text": user_input["query_text"], | ||
"examples": user_input.get("examples", ""), | ||
}, | ||
"generator": { | ||
} | ||
} | ||
return run_params |
57 changes: 57 additions & 0 deletions
57
...j_graphrag/experimental/pipeline/config/template_pipeline/simple_rag_pipeline_config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"version_": "1", | ||
"template_": "SimpleRAGPipeline", | ||
"neo4j_config": { | ||
"params_": { | ||
"uri": { | ||
"resolver_": "ENV", | ||
"var_": "NEO4J_URI" | ||
}, | ||
"user": { | ||
"resolver_": "ENV", | ||
"var_": "NEO4J_USER" | ||
}, | ||
"password": { | ||
"resolver_": "ENV", | ||
"var_": "NEO4J_PASSWORD" | ||
} | ||
} | ||
}, | ||
"llm_config": { | ||
"class_": "OpenAILLM", | ||
"params_": { | ||
"api_key": { | ||
"resolver_": "ENV", | ||
"var_": "OPENAI_API_KEY" | ||
}, | ||
"model_name": "gpt-4o", | ||
"model_params": { | ||
"temperature": 0, | ||
"max_tokens": 2000 | ||
} | ||
} | ||
}, | ||
"embedder_config": { | ||
"class_": "OpenAIEmbeddings", | ||
"params_": { | ||
"api_key": { | ||
"resolver_": "ENV", | ||
"var_": "OPENAI_API_KEY" | ||
} | ||
} | ||
}, | ||
"retriever": { | ||
"class_": "neo4j_graphrag.retrievers.VectorRetriever", | ||
"params_": { | ||
"driver": { | ||
"resolver_": "CONFIG_KEY", | ||
"key_": "neo4j_config.default" | ||
}, | ||
"index_name": "moviePlotsEmbedding", | ||
"embedder": { | ||
"resolver_": "CONFIG_KEY", | ||
"key_": "embedder_config.default" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters