Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #12

Merged
merged 60 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
2831066
added toy_model dir to .gitignore
sedorfer Mar 29, 2024
dc831d9
removed toymodels from gitignore
sedorfer May 14, 2024
842826a
Added functions from tutorial
sedorfer May 14, 2024
a62348a
Implemented a first parser for the files into a list
sedorfer May 28, 2024
7d28c65
added new features to the ClassAndFunctionVisitor
sedorfer May 31, 2024
249b0d2
Restructured CFVisitor and included more functionalities
sedorfer Jun 2, 2024
58b1088
added more file cleaning and decorator information
sedorfer Jun 2, 2024
f5b497b
Added ast.call parser
sedorfer Jun 2, 2024
3cac7d5
added another dir to tryout the class
sedorfer Jun 2, 2024
3a01ede
Merge pull request #1 from molinfo-vienna/feat-ClassAndFunctionVisitor
sedorfer Jun 2, 2024
fd01365
restructured code
sedorfer Jun 15, 2024
1e9030b
refactored file parser into a class
sedorfer Jun 15, 2024
ae6166b
Added class for knowledge graph handeling and added methods for creat…
sedorfer Jun 16, 2024
b42962b
WIP GraphManagers
sedorfer Jun 18, 2024
d79e816
Updated Knowledge Graph Manager to handle most of the Doc properties
sedorfer Jul 18, 2024
45843b1
Merge pull request #2 from molinfo-vienna/feat-graphCreation
sedorfer Aug 7, 2024
1f5700d
initial version of a rag system
sedorfer Aug 20, 2024
8927d37
first dashboard for the rag
sedorfer Aug 20, 2024
3b637c5
return the final rag output
sedorfer Aug 20, 2024
aaa207a
Added second model and changed prompt structure
sedorfer Sep 30, 2024
b6e0660
add comment parsing
sedorfer Oct 21, 2024
7546254
Merge pull request #3 from molinfo-vienna/feat-docparser
sedorfer Oct 21, 2024
60d7d55
debug parameter and function relationship
sedorfer Oct 21, 2024
239a0ac
debug parameter comment
sedorfer Oct 21, 2024
3055958
Merge pull request #4 from molinfo-vienna/feat-docparser
sedorfer Oct 21, 2024
fff6215
debug parameter and function relationship
sedorfer Oct 21, 2024
241d97b
Merge branch 'feat-knowledgeGraph' of https://github.com/molinfo-vien…
sedorfer Oct 21, 2024
a168151
incorporate comments into knowledge graph
sedorfer Oct 22, 2024
3a0233b
Merge pull request #5 from molinfo-vienna/feat-knowledgeGraph
sedorfer Oct 22, 2024
65d8cfb
Merge pull request #6 from molinfo-vienna/feat_rag
sedorfer Oct 22, 2024
8148eb7
first benchmark version
sedorfer Oct 27, 2024
db0d496
renaming of benchmark results
sedorfer Oct 27, 2024
aa06a71
first benchmark and change of benchmark questions
sedorfer Nov 10, 2024
c35f6aa
Merge pull request #7 from molinfo-vienna/feat-benchmarking
sedorfer Nov 10, 2024
4d728cb
dashboard changes
sedorfer Nov 17, 2024
ef9620b
benchmark updates and restructuring
sedorfer Nov 17, 2024
4a1de03
rag structure update
sedorfer Nov 17, 2024
a9c1b5f
Styling and main components of dashboard
sedorfer Nov 22, 2024
e43387d
update of dashboard navbar
sedorfer Nov 23, 2024
21119f3
first batch benchmarking
sedorfer Dec 1, 2024
e2471e7
benchmarks
sedorfer Dec 15, 2024
292bb5a
type hinting and commenting
sedorfer Dec 15, 2024
010e684
refactoring and type hinting for knowledge graph classes
sedorfer Dec 15, 2024
8d448ce
refactoring of the rag system
sedorfer Dec 28, 2024
8db36b7
Merge pull request #8 from molinfo-vienna/refactoring
sedorfer Dec 28, 2024
bc0edac
Merge branch 'development' into benchmarking
sedorfer Dec 28, 2024
6aa8cb8
Merge pull request #9 from molinfo-vienna/benchmarking
sedorfer Dec 28, 2024
236b5b9
manual benchmark
sedorfer Dec 30, 2024
aa924d0
first code for evaluation
sedorfer Jan 2, 2025
156dc6f
first part of new evaluation
sedorfer Jan 3, 2025
faf125a
second round of benchmarking
sedorfer Jan 4, 2025
02d2f14
last benchmarking
sedorfer Jan 4, 2025
b0cdf5d
last benchmarking
sedorfer Jan 4, 2025
30cff02
benchmark evaluation
sedorfer Jan 6, 2025
8435bf5
added citation
sedorfer Jan 15, 2025
0944337
Merge pull request #10 from molinfo-vienna/benchmarking
sedorfer Jan 15, 2025
5366748
Merge branch 'development' into feat-dashboard
sedorfer Jan 15, 2025
67c9093
Merge pull request #11 from molinfo-vienna/feat-dashboard
sedorfer Jan 15, 2025
83d4fde
updates of the dashboard
sedorfer Jan 16, 2025
8810363
put sensitive data into config file
sedorfer Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sys
sys.path.insert(0, os.path.abspath('..'))

import graphRAG
import graphRAG_system


# -- Project information -----------------------------------------------------
Expand Down
136 changes: 136 additions & 0 deletions graphRAG/benchmark_questions.txt

Large diffs are not rendered by default.

1,167 changes: 1,167 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_1.json

Large diffs are not rendered by default.

904 changes: 904 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_10.json

Large diffs are not rendered by default.

1,354 changes: 1,354 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_100.json

Large diffs are not rendered by default.

1,102 changes: 1,102 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_11.json

Large diffs are not rendered by default.

1,229 changes: 1,229 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_12.json

Large diffs are not rendered by default.

1,046 changes: 1,046 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_13.json

Large diffs are not rendered by default.

1,130 changes: 1,130 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_14.json

Large diffs are not rendered by default.

1,052 changes: 1,052 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_15.json

Large diffs are not rendered by default.

1,332 changes: 1,332 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_16.json

Large diffs are not rendered by default.

1,069 changes: 1,069 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_17.json

Large diffs are not rendered by default.

1,345 changes: 1,345 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_18.json

Large diffs are not rendered by default.

1,112 changes: 1,112 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_19.json

Large diffs are not rendered by default.

820 changes: 820 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_2.json

Large diffs are not rendered by default.

1,133 changes: 1,133 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_20.json

Large diffs are not rendered by default.

935 changes: 935 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_21.json

Large diffs are not rendered by default.

1,302 changes: 1,302 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_22.json

Large diffs are not rendered by default.

1,333 changes: 1,333 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_23.json

Large diffs are not rendered by default.

978 changes: 978 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_24.json

Large diffs are not rendered by default.

1,013 changes: 1,013 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_25.json

Large diffs are not rendered by default.

1,274 changes: 1,274 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_26.json

Large diffs are not rendered by default.

1,261 changes: 1,261 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_27.json

Large diffs are not rendered by default.

983 changes: 983 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_28.json

Large diffs are not rendered by default.

1,032 changes: 1,032 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_29.json

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_3.json

Large diffs are not rendered by default.

1,178 changes: 1,178 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_30.json

Large diffs are not rendered by default.

898 changes: 898 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_31.json

Large diffs are not rendered by default.

1,052 changes: 1,052 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_32.json

Large diffs are not rendered by default.

1,349 changes: 1,349 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_33.json

Large diffs are not rendered by default.

1,147 changes: 1,147 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_34.json

Large diffs are not rendered by default.

1,115 changes: 1,115 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_35.json

Large diffs are not rendered by default.

1,128 changes: 1,128 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_36.json

Large diffs are not rendered by default.

1,201 changes: 1,201 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_37.json

Large diffs are not rendered by default.

1,211 changes: 1,211 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_38.json

Large diffs are not rendered by default.

1,047 changes: 1,047 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_39.json

Large diffs are not rendered by default.

1,242 changes: 1,242 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_4.json

Large diffs are not rendered by default.

1,085 changes: 1,085 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_40.json

Large diffs are not rendered by default.

1,261 changes: 1,261 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_41.json

Large diffs are not rendered by default.

1,324 changes: 1,324 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_42.json

Large diffs are not rendered by default.

1,265 changes: 1,265 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_43.json

Large diffs are not rendered by default.

1,273 changes: 1,273 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_44.json

Large diffs are not rendered by default.

894 changes: 894 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_45.json

Large diffs are not rendered by default.

1,046 changes: 1,046 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_46.json

Large diffs are not rendered by default.

962 changes: 962 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_47.json

Large diffs are not rendered by default.

1,025 changes: 1,025 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_48.json

Large diffs are not rendered by default.

1,157 changes: 1,157 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_49.json

Large diffs are not rendered by default.

1,055 changes: 1,055 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_5.json

Large diffs are not rendered by default.

1,133 changes: 1,133 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_50.json

Large diffs are not rendered by default.

922 changes: 922 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_51.json

Large diffs are not rendered by default.

1,322 changes: 1,322 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_52.json

Large diffs are not rendered by default.

1,138 changes: 1,138 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_53.json

Large diffs are not rendered by default.

1,189 changes: 1,189 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_54.json

Large diffs are not rendered by default.

1,477 changes: 1,477 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_55.json

Large diffs are not rendered by default.

1,200 changes: 1,200 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_56.json

Large diffs are not rendered by default.

1,316 changes: 1,316 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_57.json

Large diffs are not rendered by default.

1,156 changes: 1,156 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_58.json

Large diffs are not rendered by default.

1,160 changes: 1,160 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_59.json

Large diffs are not rendered by default.

1,256 changes: 1,256 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_6.json

Large diffs are not rendered by default.

1,249 changes: 1,249 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_60.json

Large diffs are not rendered by default.

896 changes: 896 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_61.json

Large diffs are not rendered by default.

1,072 changes: 1,072 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_62.json

Large diffs are not rendered by default.

1,088 changes: 1,088 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_63.json

Large diffs are not rendered by default.

1,337 changes: 1,337 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_64.json

Large diffs are not rendered by default.

1,367 changes: 1,367 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_65.json

Large diffs are not rendered by default.

1,220 changes: 1,220 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_66.json

Large diffs are not rendered by default.

967 changes: 967 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_67.json

Large diffs are not rendered by default.

930 changes: 930 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_68.json

Large diffs are not rendered by default.

1,283 changes: 1,283 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_69.json

Large diffs are not rendered by default.

1,244 changes: 1,244 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_7.json

Large diffs are not rendered by default.

1,028 changes: 1,028 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_70.json

Large diffs are not rendered by default.

1,044 changes: 1,044 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_71.json

Large diffs are not rendered by default.

1,160 changes: 1,160 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_72.json

Large diffs are not rendered by default.

977 changes: 977 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_73.json

Large diffs are not rendered by default.

1,161 changes: 1,161 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_74.json

Large diffs are not rendered by default.

897 changes: 897 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_75.json

Large diffs are not rendered by default.

928 changes: 928 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_76.json

Large diffs are not rendered by default.

1,193 changes: 1,193 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_77.json

Large diffs are not rendered by default.

1,070 changes: 1,070 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_78.json

Large diffs are not rendered by default.

973 changes: 973 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_79.json

Large diffs are not rendered by default.

1,211 changes: 1,211 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_8.json

Large diffs are not rendered by default.

1,004 changes: 1,004 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_80.json

Large diffs are not rendered by default.

1,054 changes: 1,054 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_81.json

Large diffs are not rendered by default.

1,168 changes: 1,168 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_82.json

Large diffs are not rendered by default.

1,068 changes: 1,068 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_83.json

Large diffs are not rendered by default.

906 changes: 906 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_84.json

Large diffs are not rendered by default.

1,048 changes: 1,048 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_85.json

Large diffs are not rendered by default.

1,033 changes: 1,033 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_86.json

Large diffs are not rendered by default.

890 changes: 890 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_87.json

Large diffs are not rendered by default.

1,229 changes: 1,229 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_88.json

Large diffs are not rendered by default.

1,493 changes: 1,493 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_89.json

Large diffs are not rendered by default.

1,213 changes: 1,213 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_9.json

Large diffs are not rendered by default.

1,183 changes: 1,183 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_90.json

Large diffs are not rendered by default.

1,253 changes: 1,253 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_91.json

Large diffs are not rendered by default.

1,319 changes: 1,319 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_92.json

Large diffs are not rendered by default.

1,085 changes: 1,085 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_93.json

Large diffs are not rendered by default.

912 changes: 912 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_94.json

Large diffs are not rendered by default.

1,034 changes: 1,034 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_95.json

Large diffs are not rendered by default.

1,206 changes: 1,206 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_96.json

Large diffs are not rendered by default.

1,411 changes: 1,411 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_97.json

Large diffs are not rendered by default.

1,181 changes: 1,181 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_98.json

Large diffs are not rendered by default.

919 changes: 919 additions & 0 deletions graphRAG/benchmark_results/benchmark_results_99.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions graphRAG/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

neo4j_uri = "neo4j+s://4de35fba.databases.neo4j.io"
neo4j_user = "neo4j"
neo4j_password = "87YkRGzIftmB-QU8CvYcLNzHZeFAZkeEQpwtZTEa4PU"
# neo4j_password = os.getenv("NEO4J_PW") in the future, you should use this and set the password as an environment variable
# but to make it easily runnable i left it like this
36 changes: 0 additions & 36 deletions graphRAG/data/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions graphRAG/data/look_and_say.dat

This file was deleted.

74 changes: 74 additions & 0 deletions graphRAG/evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import json
import copy
from utils.evaluation_utils import calculate_metrics, print_metrics, print_comparison_dict, compare_result


def testset_evaluation(mode: str, directory: str) -> None:
# evaluates the different categories of the testset evaluation for either "manual" mode or "automated" for all the files in the directory
if mode not in ["manual", "automated"]:
raise ValueError("Mode must be one of the following: manual, automated.")
categories = ["cypher", "context", "answer", "code", "overall"]
metrics_template = {cat: {"accuracy": [], "precision": [], "recall": [], "f1": [], "counts": []} for cat in categories} # template to store the results in
metrics_specific = copy.deepcopy(metrics_template)
metrics_general = copy.deepcopy(metrics_template)

files = sorted([f for f in os.listdir(directory) if f.endswith('.json')], key=lambda x: int(x.split('_')[2].split('.')[0])) # sort the result files by ending


if mode == "manual":
files = files[:20]
for file in files:
file_path = os.path.join(directory, file)
with open(file_path, "r") as f:
results = json.load(f)

# calculate the metrics for the specific and the general questions
calculate_metrics(results[:17], metrics_specific, categories, mode)
calculate_metrics(results[17:], metrics_general, categories, mode)

print_metrics("SPECIFIC", metrics_specific)
print_metrics("GENERAL", metrics_general)


def compare_manual_automated(directory: str) -> None:
# compares the result of the automated evaluation with that of the manual one for each category
categories = ["cypher", "context", "answer", "code", "overall"]
# the manual evaluation is the 'ground truth' and the dict will show how often a score has been correctly classified/missclassified as something else
comparison_template = {cat: {"tp": {"tp": 0, "tn": 0, "fp": 0, "fn": 0}, "tn": {"tp": 0, "tn": 0, "fp": 0, "fn": 0}, "fp": {"tp": 0, "tn": 0, "fp": 0, "fn": 0}, "fn": {"tp": 0, "tn": 0, "fp": 0, "fn": 0}} for cat in categories}
comparison_specific = copy.deepcopy(comparison_template)
comparison_general = copy.deepcopy(comparison_template)

files = sorted([f for f in os.listdir(directory) if f.endswith('.json')], key=lambda x: int(x.split('_')[2].split('.')[0]))

for file in files[:20]:
file_path = os.path.join(directory, file)
with open(file_path, "r") as f:
results = json.load(f)

for q in results[:17]:
if "score_context_manual:" in q.keys():
q["score_context_manual"] = q["score_context_manual:"] # correct a naming error
for cat in categories:
compare_result(cat, comparison_specific, q[f"score_{cat}_manual"], q[f"score_{cat}_automated"])
for q in results[17: ]:
if "score_context_manual:" in q.keys():
q["score_context_manual"] = q["score_context_manual:"]
for cat in categories:
compare_result(cat, comparison_general, q[f"score_{cat}_manual"], q[f"score_{cat}_automated"])

print("#### SPECIFIC:")
print_comparison_dict(comparison_specific)
print("#### GENERAL:")
print_comparison_dict(comparison_general)




if __name__ == "__main__":
directory = "/data/shared/projects/graphRAG/graphRAG/graphRAG/evaluation/benchmark_results"
# print("######### MANUAL ########")
# testset_evaluation("manual", directory)
# print("######### AUTOMATED ########")
# testset_evaluation("automated", directory)
compare_manual_automated(directory=directory)
48 changes: 48 additions & 0 deletions graphRAG/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from transformers.pipelines.text_generation import TextGenerationPipeline

def generate_answer_qwen(user_prompt: str, system_prompt: str, pipe: TextGenerationPipeline, **kwargs: dict) -> str:
# https://medium.com/@silviaonofrei/code-llamas-knowledge-of-neo4j-s-cypher-query-language-54783d2ad421
full_prompt = f"System: {system_prompt}\nUser: {user_prompt}\nAssistant:" # combine the system and user prompt into a from that is easily understoof by Qwen

if "max_new_tokens" not in kwargs:
kwargs["max_new_tokens"] = 512 # Set a default max_new_tokens if not provided

# generate the answer
output = pipe(full_prompt,
do_sample=True, # enables sampling and a more varied generation
top_k=5, # take the top 5 most likely tokens at each generation step
top_p=0.9,
temperature = 0.7, # controls randomness of sampling
**kwargs
)

output = output[0]["generated_text"] # Extract the relevant part of the generated text
return output.split('Assistant:', 1)[1].strip() # takes only the portion of the text after the "Assistant: "


def generate_rag_prompt(retrieved_context: str, cypher_query: str) -> str:
# takes the cypher query and retrieved context and creates a system prompt
# positive examples are added as well as clear instructions
return """

You are a highly intelligent assistant. Your job is to answer user questions using *only* the information from the retrieved context provided from a Neo4j knowledge graph database. If appropriate, add a small python example for the retrieved context, but no cypher queries. Only add this Python code if it is appropriate for the question.
The retrieved context is the result of a cypher query.
Ensure that your response strictly relies on the retrieved context, and do not add any information from other sources.

Cypher query:
{cypher_query}
Retrieved Context:
{retrieved_context}

### Example 1:
Q: What methods does AromaticSubstructure have?
Cypher query: MATCH (c:Class {{name: 'AromaticSubstructure'}})-[:HAS]->(f:Function) RETURN f.name, f.comment
Retrieved Context: [['f.name', 'f.comment'], ['__init__', 'Constructs an empty <tt>AromaticSubstructure</tt> instance.'], ['__init__', 'Construct a <tt>AromaticSubstructure</tt> instance that consists of the aromatic atoms and bonds of the molecular graph <em>molgraph</em>.'], ['perceive', 'Replaces the currently stored atoms and bonds by the set of aromatic atoms and bonds of the molecular graph <em>molgraph</em>.']]
A: AromaticSubstructure has the following methods:
- __init__: Constructs an empty <tt>AromaticSubstructure</tt> instance.
- __init__: Construct a <tt>AromaticSubstructure</tt> instance that consists of the aromatic atoms and bonds of the molecular graph <em>molgraph</em>.
- perceive: Replaces the currently stored atoms and bonds by the set of aromatic atoms and bonds of the molecular graph <em>molgraph</em>.
""".format(retrieved_context = retrieved_context, cypher_query = cypher_query)



101 changes: 81 additions & 20 deletions graphRAG/graphRAG.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,90 @@
"""Provide the primary functions."""
import os
os.environ['HF_HOME'] = '/data/local/sschoendorfer'
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"

from utils.rag_utils import initialize_neo4j, get_kg_schema, get_pipeline_from_model
from retriever import retrieve_context
from generator import generate_rag_prompt, generate_answer_qwen
import json
import re
from transformers.pipelines.text_generation import TextGenerationPipeline

def canvas(with_attribution=True):
"""
Placeholder function to show example docstring (NumPy format).
# Base idea from this article:
# https://medium.com/@silviaonofrei/code-llamas-knowledge-of-neo4j-s-cypher-query-language-54783d2ad421

Replace this function and doc string for your own project.

Parameters
----------
with_attribution : bool, Optional, default: True
Set whether or not to display who the quote is from.
def question_rag(user_prompt: str, pipe_cypher: TextGenerationPipeline, pipe_answer: TextGenerationPipeline) -> tuple[str, str]:
# function to pass a user prompt to the Graph RAG system
driver = initialize_neo4j() # initialize the neo4j driver to communicate with the Knowledge Graph
schema = get_kg_schema() # get the KG schema
try:
query_result, cypher_query = retrieve_context(driver, user_prompt, pipe_cypher, schema)
except Exception as e:
print("Exception while retrieving context: ", e)
query_result = "Context could not be retrieved" # if there is an exception, the query was not functional
cypher_query = "None" # set it to None to flag for non-runnable queries during benchmarking

system_prompt_rag = generate_rag_prompt(query_result, cypher_query) # get the final system prompt for the rag

Returns
-------
quote : str
Compiled string including quote and optional attribution.
"""
final_answer = generate_answer_qwen(user_prompt, system_prompt_rag, pipe_answer) # generate the final answer

quote = "The code is but a canvas to our imagination."
if with_attribution:
quote += "\n\t- Adapted from Henry David Thoreau"
return quote
return cypher_query, query_result, final_answer


def benchmark_rag(pipe_cypher: TextGenerationPipeline, pipe_answer: TextGenerationPipeline) -> None:
# function for running the benchmark questions
with open("/data/shared/projects/graphRAG/graphRAG/graphRAG/benchmark_questions.txt", "r") as f:
testset = f.read()

pattern = r"Q:\s*(.+?)\nQuery:\s*(.+?)\nC:\s*(.+?)\nA:\s*(.+?)(?=\nQ:|\Z)"

# Find all matches
matches = re.findall(pattern, testset, re.DOTALL)

# Parse into a list of dictionaries
parsed_questions = [
{"Question": match[0].strip(), "Query": match[1].strip(), "Context": match[2].strip(), "Answer": match[3].strip()}
for match in matches
]

for i in range(97, 100):
benchmark = []
for question in parsed_questions:
cypher_query, query_result, final_answer = question_rag(question["Question"], pipe_cypher, pipe_answer)
benchmark.append({"user_prompt": question["Question"], "cypher_query": cypher_query,
"retrieved_context": query_result,
"final_answer": final_answer,
"model_cypher": question["Query"], "model_answer": question["Answer"],
"model_context": question["Context"],
"score_cypher_automated": "",
"score_context_automated":"",
"score_answer_automated": "",
"score_code_automated":"",
"score_overall_automated": "",
"score_cypher_manual": "",
"score_context_manual":"",
"score_answer_manual": "",
"score_code_manual":"",
"score_overall_manual": ""
})

with open(f"/data/shared/projects/graphRAG/graphRAG/graphRAG/benchmark_results/benchmark_results_{i+1}.json", "w") as file:
json.dump(benchmark, file, indent=4)


if __name__ == "__main__":
# Do something if this file is invoked on its own
print(canvas())
model_cypher = "codellama/CodeLlama-13b-Instruct-hf"

model_answer = "Qwen/Qwen2.5-7B-Instruct"

pipe_cypher = get_pipeline_from_model(model_cypher)

pipe_answer = get_pipeline_from_model(model_answer)

_, _, answer = question_rag("How can I read in molecules", pipe_cypher, pipe_answer)

print(answer)

# benchmark_rag(pipe_cypher, pipe_answer)


Loading
Loading