graphrag_sdk 0.3.1__tar.gz → 0.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/PKG-INFO +42 -3
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/README.md +41 -2
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/agents/kg_agent.py +1 -1
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/chat_session.py +36 -10
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/fixtures/prompts.py +3 -3
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/kg.py +58 -10
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/steps/graph_query_step.py +9 -11
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/steps/qa_step.py +5 -6
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/pyproject.toml +1 -1
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/LICENSE +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/__init__.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/agents/__init__.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/agents/agent.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/attribute.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/__init__.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/csv.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/html.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/jsonl.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/pdf.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/text.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/document_loaders/url.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/entity.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/fixtures/regex.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/helpers.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/model_config.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/models/__init__.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/models/azure_openai.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/models/gemini.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/models/model.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/models/ollama.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/models/openai.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/ontology.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/__init__.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/execution_plan.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/orchestrator.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/orchestrator_decision.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/orchestrator_runner.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/step.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/step_result.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/steps/__init__.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/steps/agent.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/steps/parallel.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/steps/summary.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/steps/user_input.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/relation.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/source.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/steps/Step.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/steps/create_ontology_step.py +0 -0
- {graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/steps/extract_data_step.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: graphrag_sdk
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
4
4
|
Summary:
|
|
5
5
|
Home-page: http://RAG-SDK.readthedocs.io
|
|
6
6
|
License: MIT
|
|
@@ -210,9 +210,9 @@ After generating the initial ontology, you can review it and make any necessary
|
|
|
210
210
|
|
|
211
211
|
Once you are satisfied with the ontology, you can proceed to use it for creating and managing your Knowledge Graph (KG).
|
|
212
212
|
|
|
213
|
-
### Knowledge Graph
|
|
213
|
+
### Knowledge Graph Agent
|
|
214
214
|
|
|
215
|
-
Now, you can use the SDK to create a Knowledge Graph (KG) from your sources and ontology.
|
|
215
|
+
Now, you can use the SDK to create a Knowledge Graph (KG) from your sources and ontology for Q&A.
|
|
216
216
|
|
|
217
217
|
```python
|
|
218
218
|
# After approving the ontology, load it from disk.
|
|
@@ -231,6 +231,45 @@ kg.process_sources(sources)
|
|
|
231
231
|
|
|
232
232
|
You can update the KG at any time by processing more sources with the `process_sources` method.
|
|
233
233
|
|
|
234
|
+
#### Configurable Prompts
|
|
235
|
+
When creating your Knowledge Graph (KG) agent, you can customize the prompts to tailor its behavior. This step is optional but can enhance functionality.
|
|
236
|
+
|
|
237
|
+
There are five types of prompts:
|
|
238
|
+
|
|
239
|
+
1. **`cypher_system_instruction`**
|
|
240
|
+
- System instructions for the Cypher generation step.
|
|
241
|
+
- **Note:** Ensure your prompt includes `{ontology}`.
|
|
242
|
+
|
|
243
|
+
2. **`qa_system_instruction`**
|
|
244
|
+
- System instructions for the Q&A step.
|
|
245
|
+
|
|
246
|
+
3. **`cypher_gen_prompt`**
|
|
247
|
+
- The prompt used during the Cypher generation step.
|
|
248
|
+
- **Note:** Include `{question}` in your prompt.
|
|
249
|
+
|
|
250
|
+
4. **`cypher_gen_prompt_history`**
|
|
251
|
+
- The prompt for Cypher generation when history needs to be considered.
|
|
252
|
+
- **Note:** Include `{question}` and `{last_answer}` in your prompt.
|
|
253
|
+
|
|
254
|
+
5. **`qa_prompt`**
|
|
255
|
+
- The prompt used during the Q&A step.
|
|
256
|
+
- **Note:** Include `{question}`, `{context}`, and `{cypher}` in your prompt.
|
|
257
|
+
|
|
258
|
+
Here’s an example configuration:
|
|
259
|
+
|
|
260
|
+
```python
|
|
261
|
+
kg = KnowledgeGraph(
|
|
262
|
+
name="kg_name",
|
|
263
|
+
model_config=KnowledgeGraphModelConfig.with_model(model),
|
|
264
|
+
ontology=ontology,
|
|
265
|
+
cypher_system_instruction=cypher_system_instruction,
|
|
266
|
+
qa_system_instruction=qa_system_instruction,
|
|
267
|
+
cypher_gen_prompt=cypher_gen_prompt,
|
|
268
|
+
cypher_gen_prompt_history=cypher_gen_prompt_history,
|
|
269
|
+
qa_prompt=qa_prompt
|
|
270
|
+
)
|
|
271
|
+
```
|
|
272
|
+
|
|
234
273
|
### Graph RAG
|
|
235
274
|
|
|
236
275
|
At this point, you have a Knowledge Graph that can be queried using this SDK. Use the method `chat_session` for start a conversation.
|
|
@@ -170,9 +170,9 @@ After generating the initial ontology, you can review it and make any necessary
|
|
|
170
170
|
|
|
171
171
|
Once you are satisfied with the ontology, you can proceed to use it for creating and managing your Knowledge Graph (KG).
|
|
172
172
|
|
|
173
|
-
### Knowledge Graph
|
|
173
|
+
### Knowledge Graph Agent
|
|
174
174
|
|
|
175
|
-
Now, you can use the SDK to create a Knowledge Graph (KG) from your sources and ontology.
|
|
175
|
+
Now, you can use the SDK to create a Knowledge Graph (KG) from your sources and ontology for Q&A.
|
|
176
176
|
|
|
177
177
|
```python
|
|
178
178
|
# After approving the ontology, load it from disk.
|
|
@@ -191,6 +191,45 @@ kg.process_sources(sources)
|
|
|
191
191
|
|
|
192
192
|
You can update the KG at any time by processing more sources with the `process_sources` method.
|
|
193
193
|
|
|
194
|
+
#### Configurable Prompts
|
|
195
|
+
When creating your Knowledge Graph (KG) agent, you can customize the prompts to tailor its behavior. This step is optional but can enhance functionality.
|
|
196
|
+
|
|
197
|
+
There are five types of prompts:
|
|
198
|
+
|
|
199
|
+
1. **`cypher_system_instruction`**
|
|
200
|
+
- System instructions for the Cypher generation step.
|
|
201
|
+
- **Note:** Ensure your prompt includes `{ontology}`.
|
|
202
|
+
|
|
203
|
+
2. **`qa_system_instruction`**
|
|
204
|
+
- System instructions for the Q&A step.
|
|
205
|
+
|
|
206
|
+
3. **`cypher_gen_prompt`**
|
|
207
|
+
- The prompt used during the Cypher generation step.
|
|
208
|
+
- **Note:** Include `{question}` in your prompt.
|
|
209
|
+
|
|
210
|
+
4. **`cypher_gen_prompt_history`**
|
|
211
|
+
- The prompt for Cypher generation when history needs to be considered.
|
|
212
|
+
- **Note:** Include `{question}` and `{last_answer}` in your prompt.
|
|
213
|
+
|
|
214
|
+
5. **`qa_prompt`**
|
|
215
|
+
- The prompt used during the Q&A step.
|
|
216
|
+
- **Note:** Include `{question}`, `{context}`, and `{cypher}` in your prompt.
|
|
217
|
+
|
|
218
|
+
Here’s an example configuration:
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
kg = KnowledgeGraph(
|
|
222
|
+
name="kg_name",
|
|
223
|
+
model_config=KnowledgeGraphModelConfig.with_model(model),
|
|
224
|
+
ontology=ontology,
|
|
225
|
+
cypher_system_instruction=cypher_system_instruction,
|
|
226
|
+
qa_system_instruction=qa_system_instruction,
|
|
227
|
+
cypher_gen_prompt=cypher_gen_prompt,
|
|
228
|
+
cypher_gen_prompt_history=cypher_gen_prompt_history,
|
|
229
|
+
qa_prompt=qa_prompt
|
|
230
|
+
)
|
|
231
|
+
```
|
|
232
|
+
|
|
194
233
|
### Graph RAG
|
|
195
234
|
|
|
196
235
|
At this point, you have a Knowledge Graph that can be queried using this SDK. Use the method `chat_session` for start a conversation.
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
from falkordb import Graph
|
|
1
2
|
from graphrag_sdk.ontology import Ontology
|
|
3
|
+
from graphrag_sdk.steps.qa_step import QAStep
|
|
2
4
|
from graphrag_sdk.model_config import KnowledgeGraphModelConfig
|
|
3
5
|
from graphrag_sdk.steps.graph_query_step import GraphQueryGenerationStep
|
|
4
|
-
from graphrag_sdk.steps.qa_step import QAStep
|
|
5
|
-
from graphrag_sdk.fixtures.prompts import GRAPH_QA_SYSTEM, CYPHER_GEN_SYSTEM
|
|
6
|
-
from falkordb import Graph
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
class ChatSession:
|
|
@@ -25,7 +24,9 @@ class ChatSession:
|
|
|
25
24
|
>>> chat_session.send_message("What is the capital of France?")
|
|
26
25
|
"""
|
|
27
26
|
|
|
28
|
-
def __init__(self, model_config: KnowledgeGraphModelConfig, ontology: Ontology, graph: Graph
|
|
27
|
+
def __init__(self, model_config: KnowledgeGraphModelConfig, ontology: Ontology, graph: Graph,
|
|
28
|
+
cypher_system_instruction: str, qa_system_instruction: str,
|
|
29
|
+
cypher_gen_prompt: str, qa_prompt: str, cypher_gen_prompt_history: str):
|
|
29
30
|
"""
|
|
30
31
|
Initializes a new ChatSession object.
|
|
31
32
|
|
|
@@ -44,14 +45,21 @@ class ChatSession:
|
|
|
44
45
|
self.model_config = model_config
|
|
45
46
|
self.graph = graph
|
|
46
47
|
self.ontology = ontology
|
|
48
|
+
cypher_system_instruction = cypher_system_instruction.format(ontology=str(ontology.to_json()))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
self.cypher_prompt = cypher_gen_prompt
|
|
52
|
+
self.qa_prompt = qa_prompt
|
|
53
|
+
self.cypher_prompt_with_history = cypher_gen_prompt_history
|
|
54
|
+
|
|
47
55
|
self.cypher_chat_session = (
|
|
48
56
|
model_config.cypher_generation.with_system_instruction(
|
|
49
|
-
|
|
57
|
+
cypher_system_instruction
|
|
50
58
|
).start_chat()
|
|
51
59
|
)
|
|
52
60
|
self.qa_chat_session = model_config.qa.with_system_instruction(
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
qa_system_instruction
|
|
62
|
+
).start_chat()
|
|
55
63
|
self.last_answer = None
|
|
56
64
|
|
|
57
65
|
def send_message(self, message: str):
|
|
@@ -62,24 +70,42 @@ class ChatSession:
|
|
|
62
70
|
message (str): The message to send.
|
|
63
71
|
|
|
64
72
|
Returns:
|
|
65
|
-
|
|
73
|
+
dict: The response to the message in the following format:
|
|
74
|
+
{"question": message,
|
|
75
|
+
"response": answer,
|
|
76
|
+
"context": context,
|
|
77
|
+
"cypher": cypher}
|
|
66
78
|
"""
|
|
67
79
|
cypher_step = GraphQueryGenerationStep(
|
|
68
80
|
graph=self.graph,
|
|
69
81
|
chat_session=self.cypher_chat_session,
|
|
70
82
|
ontology=self.ontology,
|
|
71
83
|
last_answer=self.last_answer,
|
|
84
|
+
cypher_prompt=self.cypher_prompt,
|
|
85
|
+
cypher_prompt_with_history=self.cypher_prompt_with_history
|
|
72
86
|
)
|
|
73
87
|
|
|
74
88
|
(context, cypher) = cypher_step.run(message)
|
|
75
89
|
|
|
76
90
|
if not cypher or len(cypher) == 0:
|
|
77
|
-
return
|
|
91
|
+
return {
|
|
92
|
+
"question": message,
|
|
93
|
+
"response": "I am sorry, I could not find the answer to your question",
|
|
94
|
+
"context": None,
|
|
95
|
+
"cypher": None
|
|
96
|
+
}
|
|
78
97
|
|
|
79
98
|
qa_step = QAStep(
|
|
80
99
|
chat_session=self.qa_chat_session,
|
|
100
|
+
qa_prompt=self.qa_prompt,
|
|
81
101
|
)
|
|
82
102
|
|
|
83
103
|
answer = qa_step.run(message, cypher, context)
|
|
84
104
|
self.last_answer = answer
|
|
85
|
-
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
"question": message,
|
|
108
|
+
"response": answer,
|
|
109
|
+
"context": context,
|
|
110
|
+
"cypher": cypher
|
|
111
|
+
}
|
|
@@ -429,7 +429,7 @@ Single-Pair minimal-weight paths: Find minimal-weight paths between a pair of en
|
|
|
429
429
|
Single-Source minimal-weight paths: Find minimal-weight paths from a given source entity using algo.SSpaths().
|
|
430
430
|
|
|
431
431
|
Ontology:
|
|
432
|
-
|
|
432
|
+
{ontology}
|
|
433
433
|
|
|
434
434
|
|
|
435
435
|
For example, given the question "Which managers own Neo4j stocks?", the OpenCypher statement should look like this:
|
|
@@ -442,7 +442,7 @@ RETURN m, s
|
|
|
442
442
|
|
|
443
443
|
CYPHER_GEN_PROMPT = """
|
|
444
444
|
Using the ontology provided, generate an OpenCypher statement to query the graph database returning all relevant entities, relationships, and attributes to answer the question below.
|
|
445
|
-
If you cannot generate a OpenCypher statement for any reason, return an empty
|
|
445
|
+
If you cannot generate a OpenCypher statement for any reason, return an empty response.
|
|
446
446
|
Respect the order of the relationships, the arrows should always point from the "source" to the "target".
|
|
447
447
|
Please think if your answer is a valid Cypher query, and correct it if it is not.
|
|
448
448
|
|
|
@@ -482,8 +482,8 @@ The information part contains the provided information that you must use to cons
|
|
|
482
482
|
The provided information is authoritative, you must never doubt it or try to use your internal knowledge to correct it.
|
|
483
483
|
Make the answer sound as a response to the question. Do not mention that you based the result on the given information.
|
|
484
484
|
Do not answer more than the question asks for.
|
|
485
|
-
Here is an example:
|
|
486
485
|
|
|
486
|
+
Here is an example:
|
|
487
487
|
Question: Which managers own Neo4j stocks?
|
|
488
488
|
Context:[manager:CTL LLC, manager:JANE STREET GROUP LLC]
|
|
489
489
|
Helpful Answer: CTL LLC, JANE STREET GROUP LLC owns Neo4j stocks.
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import logging
|
|
2
|
-
|
|
2
|
+
import warnings
|
|
3
3
|
from falkordb import FalkorDB
|
|
4
|
+
from graphrag_sdk.ontology import Ontology
|
|
4
5
|
from graphrag_sdk.source import AbstractSource
|
|
5
|
-
from graphrag_sdk.model_config import KnowledgeGraphModelConfig
|
|
6
|
-
from graphrag_sdk.steps.extract_data_step import ExtractDataStep
|
|
7
|
-
from graphrag_sdk.steps.graph_query_step import GraphQueryGenerationStep
|
|
8
|
-
from graphrag_sdk.fixtures.prompts import GRAPH_QA_SYSTEM, CYPHER_GEN_SYSTEM
|
|
9
|
-
from graphrag_sdk.steps.qa_step import QAStep
|
|
10
6
|
from graphrag_sdk.chat_session import ChatSession
|
|
11
|
-
from graphrag_sdk.helpers import map_dict_to_cypher_properties
|
|
12
7
|
from graphrag_sdk.attribute import AttributeType, Attribute
|
|
13
|
-
from graphrag_sdk.
|
|
8
|
+
from graphrag_sdk.helpers import map_dict_to_cypher_properties
|
|
9
|
+
from graphrag_sdk.model_config import KnowledgeGraphModelConfig
|
|
10
|
+
from graphrag_sdk.steps.extract_data_step import ExtractDataStep
|
|
11
|
+
from graphrag_sdk.fixtures.prompts import (GRAPH_QA_SYSTEM, CYPHER_GEN_SYSTEM,
|
|
12
|
+
CYPHER_GEN_PROMPT, GRAPH_QA_PROMPT, CYPHER_GEN_PROMPT_WITH_HISTORY)
|
|
14
13
|
|
|
15
14
|
logger = logging.getLogger(__name__)
|
|
16
15
|
logger.setLevel(logging.DEBUG)
|
|
@@ -31,6 +30,11 @@ class KnowledgeGraph:
|
|
|
31
30
|
port: int = 6379,
|
|
32
31
|
username: str | None = None,
|
|
33
32
|
password: str | None = None,
|
|
33
|
+
cypher_system_instruction: str = None,
|
|
34
|
+
qa_system_instruction: str = None,
|
|
35
|
+
cypher_gen_prompt: str = None,
|
|
36
|
+
qa_prompt: str = None,
|
|
37
|
+
cypher_gen_prompt_history: str = None,
|
|
34
38
|
):
|
|
35
39
|
"""
|
|
36
40
|
Initialize Knowledge Graph
|
|
@@ -43,6 +47,11 @@ class KnowledgeGraph:
|
|
|
43
47
|
username (str|None): FalkorDB username.
|
|
44
48
|
password (str|None): FalkorDB password.
|
|
45
49
|
ontology (Ontology|None): Ontology to use.
|
|
50
|
+
cypher_system_instruction (str|None): Cypher system instruction. Make sure you have {ontology} in the instruction.
|
|
51
|
+
qa_system_instruction (str|None): QA system instruction.
|
|
52
|
+
cypher_gen_prompt (str|None): Cypher generation prompt. Make sure you have {question} in the prompt.
|
|
53
|
+
qa_prompt (str|None): QA prompt. Make sure you have {question}, {context} and {cypher} in the prompt.
|
|
54
|
+
cypher_gen_prompt_history (str|None): Cypher generation prompt with history. Make sure you have {question} and {last_answer} in the prompt.
|
|
46
55
|
"""
|
|
47
56
|
|
|
48
57
|
if not isinstance(name, str) or name == "":
|
|
@@ -57,6 +66,44 @@ class KnowledgeGraph:
|
|
|
57
66
|
self._model_config = model_config
|
|
58
67
|
self.sources = set([])
|
|
59
68
|
|
|
69
|
+
if cypher_system_instruction is None:
|
|
70
|
+
cypher_system_instruction = CYPHER_GEN_SYSTEM
|
|
71
|
+
else:
|
|
72
|
+
if "{ontology}" not in cypher_system_instruction:
|
|
73
|
+
warnings.warn("Cypher system instruction should contain {ontology}", category=UserWarning)
|
|
74
|
+
|
|
75
|
+
if qa_system_instruction is None:
|
|
76
|
+
qa_system_instruction = GRAPH_QA_SYSTEM
|
|
77
|
+
|
|
78
|
+
if cypher_gen_prompt is None:
|
|
79
|
+
cypher_gen_prompt = CYPHER_GEN_PROMPT
|
|
80
|
+
else:
|
|
81
|
+
if "{question}" not in cypher_gen_prompt:
|
|
82
|
+
raise Exception("Cypher generation prompt should contain {question}")
|
|
83
|
+
|
|
84
|
+
if qa_prompt is None:
|
|
85
|
+
qa_prompt = GRAPH_QA_PROMPT
|
|
86
|
+
else:
|
|
87
|
+
if "{question}" not in qa_prompt or "{context}" not in qa_prompt:
|
|
88
|
+
raise Exception("QA prompt should contain {question} and {context}")
|
|
89
|
+
if "{cypher}" not in qa_prompt:
|
|
90
|
+
warnings.warn("QA prompt should contain {cypher}", category=UserWarning)
|
|
91
|
+
|
|
92
|
+
if cypher_gen_prompt_history is None:
|
|
93
|
+
cypher_gen_prompt_history = CYPHER_GEN_PROMPT_WITH_HISTORY
|
|
94
|
+
else:
|
|
95
|
+
if "{question}" not in cypher_gen_prompt_history:
|
|
96
|
+
raise Exception("Cypher generation prompt with history should contain {question}")
|
|
97
|
+
if "{last_answer}" not in cypher_gen_prompt_history:
|
|
98
|
+
warnings.warn("Cypher generation prompt with history should contain {last_answer}", category=UserWarning)
|
|
99
|
+
|
|
100
|
+
# Assign the validated values
|
|
101
|
+
self.cypher_system_instruction = cypher_system_instruction
|
|
102
|
+
self.qa_system_instruction = qa_system_instruction
|
|
103
|
+
self.cypher_gen_prompt = cypher_gen_prompt
|
|
104
|
+
self.qa_prompt = qa_prompt
|
|
105
|
+
self.cypher_gen_prompt_history = cypher_gen_prompt_history
|
|
106
|
+
|
|
60
107
|
# Attributes
|
|
61
108
|
|
|
62
109
|
@property
|
|
@@ -135,8 +182,9 @@ class KnowledgeGraph:
|
|
|
135
182
|
setattr(self, key, None)
|
|
136
183
|
|
|
137
184
|
def chat_session(self) -> ChatSession:
|
|
138
|
-
|
|
139
|
-
|
|
185
|
+
chat_session = ChatSession(self._model_config, self.ontology, self.graph, self.cypher_system_instruction,
|
|
186
|
+
self.qa_system_instruction, self.cypher_gen_prompt, self.qa_prompt, self.cypher_gen_prompt_history)
|
|
187
|
+
return chat_session
|
|
140
188
|
def add_node(self, entity: str, attributes: dict):
|
|
141
189
|
"""
|
|
142
190
|
Add a node to the knowledge graph, checking if it matches the ontology
|
|
@@ -1,21 +1,15 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from falkordb import Graph
|
|
1
3
|
from graphrag_sdk.steps.Step import Step
|
|
2
4
|
from graphrag_sdk.ontology import Ontology
|
|
3
5
|
from graphrag_sdk.models import (
|
|
4
6
|
GenerativeModelChatSession,
|
|
5
7
|
)
|
|
6
|
-
from graphrag_sdk.fixtures.prompts import (
|
|
7
|
-
CYPHER_GEN_SYSTEM,
|
|
8
|
-
CYPHER_GEN_PROMPT,
|
|
9
|
-
CYPHER_GEN_PROMPT_WITH_ERROR,
|
|
10
|
-
CYPHER_GEN_PROMPT_WITH_HISTORY,
|
|
11
|
-
)
|
|
12
|
-
import logging
|
|
13
8
|
from graphrag_sdk.helpers import (
|
|
14
9
|
extract_cypher,
|
|
15
10
|
validate_cypher,
|
|
16
11
|
stringify_falkordb_response,
|
|
17
12
|
)
|
|
18
|
-
from falkordb import Graph
|
|
19
13
|
|
|
20
14
|
logger = logging.getLogger(__name__)
|
|
21
15
|
logger.setLevel(logging.DEBUG)
|
|
@@ -33,22 +27,26 @@ class GraphQueryGenerationStep(Step):
|
|
|
33
27
|
chat_session: GenerativeModelChatSession,
|
|
34
28
|
config: dict = None,
|
|
35
29
|
last_answer: str = None,
|
|
30
|
+
cypher_prompt: str = None,
|
|
31
|
+
cypher_prompt_with_history: str = None,
|
|
36
32
|
) -> None:
|
|
37
33
|
self.ontology = ontology
|
|
38
34
|
self.config = config or {}
|
|
39
35
|
self.graph = graph
|
|
40
36
|
self.chat_session = chat_session
|
|
41
37
|
self.last_answer = last_answer
|
|
38
|
+
self.cypher_prompt = cypher_prompt
|
|
39
|
+
self.cypher_prompt_with_history = cypher_prompt_with_history
|
|
42
40
|
|
|
43
41
|
def run(self, question: str, retries: int = 10):
|
|
44
42
|
cypher = ""
|
|
45
43
|
for i in range(retries):
|
|
46
44
|
try:
|
|
47
45
|
cypher_prompt = (
|
|
48
|
-
(
|
|
46
|
+
(self.cypher_prompt.format(question=question)
|
|
49
47
|
if self.last_answer is None
|
|
50
|
-
else
|
|
51
|
-
)
|
|
48
|
+
else self.cypher_prompt_with_history.format(question=question, last_answer=self.last_answer))
|
|
49
|
+
)
|
|
52
50
|
logger.debug(f"Cypher Prompt: {cypher_prompt}")
|
|
53
51
|
cypher_statement_response = self.chat_session.send_message(
|
|
54
52
|
cypher_prompt,
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
+
import logging
|
|
1
2
|
from graphrag_sdk.steps.Step import Step
|
|
2
3
|
from graphrag_sdk.models import GenerativeModelChatSession
|
|
3
4
|
|
|
4
|
-
from graphrag_sdk.fixtures.prompts import GRAPH_QA_SYSTEM, GRAPH_QA_PROMPT
|
|
5
|
-
import logging
|
|
6
|
-
|
|
7
5
|
logger = logging.getLogger(__name__)
|
|
8
6
|
logger.setLevel(logging.DEBUG)
|
|
9
7
|
|
|
@@ -17,17 +15,18 @@ class QAStep(Step):
|
|
|
17
15
|
self,
|
|
18
16
|
chat_session: GenerativeModelChatSession,
|
|
19
17
|
config: dict = None,
|
|
18
|
+
qa_prompt: str = None,
|
|
20
19
|
) -> None:
|
|
21
20
|
self.config = config or {}
|
|
22
21
|
self.chat_session = chat_session
|
|
22
|
+
self.qa_prompt = qa_prompt
|
|
23
23
|
|
|
24
24
|
def run(self, question: str, cypher: str, context: str):
|
|
25
|
-
|
|
26
|
-
qa_prompt = GRAPH_QA_PROMPT.format(
|
|
25
|
+
qa_prompt = self.qa_prompt.format(
|
|
27
26
|
context=context, cypher=cypher, question=question
|
|
28
27
|
)
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
logger.debug(f"QA Prompt: {qa_prompt}")
|
|
31
30
|
qa_response = self.chat_session.send_message(qa_prompt)
|
|
32
31
|
|
|
33
32
|
return qa_response.text
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{graphrag_sdk-0.3.1 → graphrag_sdk-0.3.2}/graphrag_sdk/orchestrator/orchestrator_decision.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|