File size: 20,262 Bytes
70d06c8
 
 
 
 
 
 
 
aacc458
dbe6919
aacc458
dbe6919
aacc458
 
70d06c8
 
 
 
 
 
 
 
aacc458
70d06c8
 
 
 
 
 
dbe6919
70d06c8
 
 
 
 
aacc458
dbe6919
70d06c8
 
aacc458
70d06c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aacc458
 
70d06c8
 
 
 
aacc458
70d06c8
 
 
aacc458
 
70d06c8
 
 
 
aacc458
70d06c8
 
 
 
aacc458
70d06c8
 
 
 
 
 
 
 
 
 
 
aacc458
 
70d06c8
 
aacc458
dbe6919
70d06c8
 
 
 
aacc458
70d06c8
aacc458
 
 
70d06c8
 
 
 
dbe6919
aacc458
70d06c8
 
 
 
aacc458
 
 
70d06c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aacc458
70d06c8
 
 
 
 
 
 
dbe6919
70d06c8
 
 
 
 
 
 
 
 
 
 
 
 
aacc458
70d06c8
 
 
 
dbe6919
 
 
 
 
 
 
 
 
 
 
 
 
 
70d06c8
 
 
 
 
 
 
 
 
 
dbe6919
aacc458
 
70d06c8
 
 
dbe6919
70d06c8
 
dbe6919
 
 
70d06c8
 
aacc458
 
 
 
 
70d06c8
aacc458
 
 
 
 
dbe6919
aacc458
 
 
 
 
 
 
 
dbe6919
70d06c8
 
dbe6919
aacc458
70d06c8
 
 
 
 
 
 
 
 
 
 
 
 
 
dbe6919
70d06c8
 
 
 
 
dbe6919
70d06c8
aacc458
70d06c8
 
 
aacc458
dbe6919
 
70d06c8
aacc458
 
70d06c8
 
 
 
 
aacc458
 
 
 
 
dbe6919
aacc458
 
 
dbe6919
70d06c8
dbe6919
70d06c8
aacc458
70d06c8
aacc458
 
 
 
dbe6919
aacc458
 
 
 
 
 
 
 
 
 
70d06c8
dbe6919
aacc458
70d06c8
 
 
 
 
 
 
 
 
 
aacc458
 
70d06c8
 
 
 
aacc458
 
 
 
 
 
 
 
 
 
 
 
70d06c8
 
 
 
 
 
 
 
 
 
 
aacc458
 
70d06c8
 
 
 
aacc458
 
 
 
 
 
 
 
 
 
 
70d06c8
aacc458
70d06c8
aacc458
 
70d06c8
 
 
 
 
 
aacc458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d06c8
aacc458
 
 
 
70d06c8
aacc458
70d06c8
 
dbe6919
70d06c8
 
 
 
aacc458
dbe6919
70d06c8
 
aacc458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d06c8
aacc458
dbe6919
70d06c8
 
aacc458
70d06c8
aacc458
 
 
 
 
 
 
dbe6919
70d06c8
 
aacc458
 
70d06c8
 
 
 
 
aacc458
 
 
 
 
 
70d06c8
 
 
 
 
 
 
dbe6919
70d06c8
 
 
 
 
 
 
 
 
 
 
 
 
 
dbe6919
70d06c8
 
aacc458
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python
# coding: utf-8

import re
import time
from random import shuffle, sample
from langgraph.checkpoint.sqlite import SqliteSaver

# Remove ChatGroq import
# from langchain_groq import ChatGroq
# Add ChatGoogleGenerativeAI import
from langchain_google_genai import ChatGoogleGenerativeAI
import os # Add os import

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langchain_community.graphs import Neo4jGraph
from langchain_community.chains.graph_qa.cypher_utils import CypherQueryCorrector, Schema
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import Field
from pydantic import BaseModel


from langgraph.graph import StateGraph

from llmlingua import PromptCompressor

from ki_gen.prompts import (
    CYPHER_GENERATION_PROMPT,
    CONCEPT_SELECTION_PROMPT,
    BINARY_GRADER_PROMPT,
    SCORE_GRADER_PROMPT,
    RELEVANT_CONCEPTS_PROMPT,
)
# Import get_model which now handles Gemini
from ki_gen.utils import ConfigSchema, DocRetrieverState, get_model, format_doc


# ... (extract_cypher remains the same)
def extract_cypher(text: str) -> str:
    """Extract Cypher code from a text.

    Args:
        text: Text to extract Cypher code from.

    Returns:
        Cypher code extracted from the text.
    """
    # The pattern to find Cypher code enclosed in triple backticks
    pattern_1 = r"```cypher\n(.*?)```"
    pattern_2 = r"```\n(.*?)```"

    # Find all matches in the input text
    matches_1 = re.findall(pattern_1, text, re.DOTALL)
    matches_2 = re.findall(pattern_2, text, re.DOTALL)
    return [
        matches_1[0] if matches_1 else text,
        matches_2[0] if matches_2 else text,
        text
    ]

# Update default model and use get_model
def get_cypher_gen_chain(model: str = "gemini-2.0-flash"):
    """
    Returns cypher gen chain using specified model for generation
    This is used when the 'auto' cypher generation method has been configured
    """
    llm_cypher_gen = get_model(model)
    cypher_gen_chain = CYPHER_GENERATION_PROMPT | llm_cypher_gen | StrOutputParser() | extract_cypher
    return cypher_gen_chain

# Update default model and use get_model
def get_concept_selection_chain(model: str = "gemini-2.0-flash"):
    """
    Returns a chain to select the most relevant topic using specified model for generation.
    This is used when the 'guided' cypher generation method has been configured
    """
    llm_topic_selection = get_model(model)
    print(f"FOUND LLM TOPIC SELECTION FOR THE CONCEPT SELECTION PROMPT : {llm_topic_selection}")
    topic_selection_chain = CONCEPT_SELECTION_PROMPT | llm_topic_selection | StrOutputParser()
    return topic_selection_chain

# ... (get_concepts remains the same)
def get_concepts(graph: Neo4jGraph):
    concept_cypher = "MATCH (c:Concept) return c"
    if isinstance(graph, Neo4jGraph):
        concepts = graph.query(concept_cypher)
    else:
        user_input = input("Topics : ")
        concepts = eval(user_input)

    concepts_name = [concept['c']['name'] for concept in concepts]
    return concepts_name


# Update to use get_model, remove Groq error handling
def get_related_concepts(graph: Neo4jGraph, question: str):
    concepts = get_concepts(graph)
    # Use get_model
    llm = get_model()
    print(f"this is the llm variable : {llm}")
    def parse_answer(llm_answer : str):
        try:
            print(f"This the llm_answer : {llm_answer}")
            # Adjust parsing if Gemini output format differs
            return re.split("\n(?:\d)+\.\s", llm_answer.split("Concepts:")[1])[1:]
        except Exception as e:
             print(f"Error parsing LLM concept answer: {e}")
             return [] # Return empty list on parsing error
    related_concepts_chain = RELEVANT_CONCEPTS_PROMPT | llm | StrOutputParser() | parse_answer

    print(f"This is the question of the user : {question}")
    print(f"This is the concepts of the user : {concepts}")

    # Remove specific Groq error handling block
    try:
        related_concepts_raw = related_concepts_chain.invoke({"user_query" : question, "concepts" : '\n'.join(concepts)})
        print(f"related_concepts_raw : {related_concepts_raw}")
    except Exception as e:
        # Add generic error handling/logging for Gemini if needed
        print(f"Error invoking related concepts chain: {e}")
        related_concepts_raw = [] # Assign empty list on error

    # We clean up the list we received from the LLM in case there were some hallucinations
    related_concepts_cleaned = []
    for related_concept in related_concepts_raw:
        # If the concept returned from the LLM is in the list we keep it
        if related_concept in concepts:
            related_concepts_cleaned.append(related_concept)
        else:
            # The LLM sometimes only forgets a few words from the concept name
            # We check if the generated concept is a substring of an existing one and if it is the case add it to the list
            for concept in concepts:
                if related_concept in concept:
                    related_concepts_cleaned.append(concept)
                    break

    # TODO : Add concepts found via similarity search
    return related_concepts_cleaned

# ... (build_concept_string, get_global_concepts remain the same)
def build_concept_string(graph: Neo4jGraph, concept_list: list[str]):
    concept_string = ""
    for concept in concept_list:
        concept_description_query = f"""
MATCH (c:Concept {{name: "{concept}" }}) RETURN c.description
"""
        concept_description = graph.query(concept_description_query)[0]['c.description']
        concept_string += f"name: {concept}\ndescription: {concept_description}\n\n"
    return concept_string

def get_global_concepts(graph: Neo4jGraph):
    concept_cypher = "MATCH (gc:GlobalConcept) return gc"
    if isinstance(graph, Neo4jGraph):
        concepts = graph.query(concept_cypher)
    else:
        user_input = input("Topics : ")
        concepts = eval(user_input)

    concepts_name = [concept['gc']['name'] for concept in concepts]
    return concepts_name

# Update concept selection error handling
def generate_cypher(state: DocRetrieverState, config: ConfigSchema):
    """
    The node where the cypher is generated
    """
    graph = config["configurable"].get("graph")

    # --- Correction Applied Here ---
    # Use .get() for safer access to 'query'
    question = state.get('query')
    if not question:
        # Handle the case where query is missing
        print("Error: 'query' key not found in state for generate_cypher node.")
        # Return an empty list or appropriate error state
        # This prevents the KeyError and stops processing for this branch if query is missing
        return {"cyphers": []}
    # --- End of Correction ---


    related_concepts = get_related_concepts(graph, question)
    cyphers = []

    if config["configurable"].get("cypher_gen_method") == 'auto':
        cypher_gen_chain = get_cypher_gen_chain()
        cyphers = cypher_gen_chain.invoke({
            "schema": graph.schema,
            "question": question,
            "concepts": related_concepts
        })

    # Remove specific Groq error handling block
    try:
        if config["configurable"].get("cypher_gen_method") == 'guided':
            concept_selection_chain = get_concept_selection_chain()
            print(f"Concept selection chain is : {concept_selection_chain}")
            # Ensure 'current_plan_step' is also safely accessed if needed here, though it's used later
            selected_topic = concept_selection_chain.invoke({"question" : question, "concepts": get_concepts(graph)})
            print(f"Selected topic are : {selected_topic}")
            # Safely get 'current_plan_step', defaulting to 0 if not found
            current_plan_step = state.get('current_plan_step', 0)
            cyphers = [generate_cypher_from_topic(selected_topic, current_plan_step)]
            print(f"Cyphers are : {cyphers}")

    except Exception as e:
         # Add generic error handling/logging for Gemini if needed
         print(f"Error during guided cypher generation: {e}")
         cyphers = [] # Assign empty list on error

    if config["configurable"].get("validate_cypher"):
        # Ensure graph schema is correctly fetched if needed
        if graph and hasattr(graph, 'structured_schema'):
            corrector_schema = [Schema(el["start"], el["type"], el["end"]) for el in graph.structured_schema.get("relationships", [])]
            cypher_corrector = CypherQueryCorrector(corrector_schema)
            # Apply corrector only if cyphers were generated
            if cyphers:
                try:
                    cyphers = [cypher_corrector(cypher) for cypher in cyphers]
                except Exception as corr_e:
                    print(f"Error during cypher correction: {corr_e}")
                    # Decide how to handle correction errors, maybe keep original cyphers
        else:
            print("Warning: Cypher validation skipped, graph or schema unavailable.")


    return {"cyphers" : cyphers}


# ... (generate_cypher_from_topic, get_docs remain the same)
def generate_cypher_from_topic(selected_concept: str, plan_step: int):
    """
    Helper function used when the 'guided' cypher generation method has been configured
    """

    print(f"L.176 PLAN STEP : {plan_step}")
    cypher_el = "(n) return n.title, n.description"
    match plan_step:
        case 0:
            cypher_el = "(ts:TechnicalSpecification) RETURN ts.title, ts.scope, ts.description"
        case 1:
            cypher_el = "(rp:ResearchPaper) RETURN rp.title, rp.abstract"
        case 2:
            cypher_el = "(ki:KeyIssue) RETURN ki.description"
    return f"MATCH (c:Concept {{name:'{selected_concept}'}})-[:RELATED_TO]-{cypher_el}"

def get_docs(state:DocRetrieverState, config:ConfigSchema):
    """
    This node retrieves docs from the graph using the generated cypher
    """
    graph = config["configurable"].get("graph")
    output = []
    if graph is not None and state.get("cyphers"): # Check if cyphers exist
        for cypher in state["cyphers"]:
            try:
                output = graph.query(cypher)
                # Assuming the first successful query is sufficient
                if output:
                    break
            except Exception as e:
                print(f"Failed to retrieve docs with cypher '{cypher}': {e}")
                # Continue to try next cypher if one fails

    # Clean up the docs we received as there may be duplicates depending on the cypher query
    all_docs = []
    for doc in output:
        unwinded_doc = {}
        # Ensure doc is a dictionary before iterating
        if isinstance(doc, dict):
            for key in doc:
                if isinstance(doc[key], dict):
                    # If a value is a dict, treat it as a separate document
                    all_docs.append(doc[key])
                else:
                    unwinded_doc.update({key: doc[key]})
        # Add the unwinded parts if any keys were not dictionaries
        if unwinded_doc:
            all_docs.append(unwinded_doc)

    filtered_docs = []
    seen_docs = set() # Use a set for faster duplicate checking based on a unique identifier

    for doc in all_docs:
         # Create a tuple of items to check for duplicates, assuming dicts are hashable
         # If dicts contain unhashable types (like lists), convert them to strings or use a primary key
        try:
            doc_tuple = tuple(sorted(doc.items()))
            if doc_tuple not in seen_docs:
                filtered_docs.append(doc)
                seen_docs.add(doc_tuple)
        except TypeError:
             # Handle cases where doc items are not hashable (e.g., contain lists/dicts)
             # Fallback: convert doc to string for uniqueness check (less reliable)
             doc_str = str(sorted(doc.items()))
             if doc_str not in seen_docs:
                  filtered_docs.append(doc)
                  seen_docs.add(doc_str)


    return {"docs": filtered_docs}


# Data model
class GradeDocumentsBinary(BaseModel):
    """Binary score for relevance check on retrieved documents."""

    binary_score: str = Field(
        description="Documents are relevant to the question, 'yes' or 'no'"
    )

# Update default model and use get_model
def get_binary_grader(model="gemini-2.0-flash"):
    """
    Returns a binary grader to evaluate relevance of documents using specified model for generation
    This is used when the 'binary' evaluation method has been configured
    """
    llm_grader_binary = get_model(model)
    # Check if the model supports structured output, otherwise use standard invocation
    try:
        # Attempt to get structured output
        structured_llm_grader_binary = llm_grader_binary.with_structured_output(GradeDocumentsBinary)
        retrieval_grader_binary = BINARY_GRADER_PROMPT | structured_llm_grader_binary
    except NotImplementedError:
         print(f"Warning: Model {model} may not support structured output directly for binary grading. Falling back.")
         # Fallback: parse the string output if structured output fails
         from langchain_core.output_parsers import SimpleJsonOutputParser
         # You might need to adjust the prompt to explicitly ask for JSON
         retrieval_grader_binary = BINARY_GRADER_PROMPT | llm_grader_binary | SimpleJsonOutputParser() # Or StrOutputParser and manual parsing

    return retrieval_grader_binary


class GradeDocumentsScore(BaseModel):
    """Score for relevance check on retrieved documents."""

    score: float = Field(
        description="Documents are relevant to the question, score between 0 (completely irrelevant) and 1 (perfectly relevant)"
    )

# Update default model and use get_model
def get_score_grader(model="gemini-2.0-flash"):
    """
    Returns a score grader to evaluate relevance of documents using specified model for generation
    This is used when the 'score' evaluation method has been configured
    """
    llm_grader_score = get_model(model)
    # Check if the model supports structured output
    try:
        structured_llm_grader_score = llm_grader_score.with_structured_output(GradeDocumentsScore)
        retrieval_grader_score = SCORE_GRADER_PROMPT | structured_llm_grader_score
    except NotImplementedError:
        print(f"Warning: Model {model} may not support structured output directly for score grading. Falling back.")
        # Fallback: parse the string output if structured output fails
        from langchain_core.output_parsers import SimpleJsonOutputParser
        # Adjust prompt if needed
        retrieval_grader_score = SCORE_GRADER_PROMPT | llm_grader_score | SimpleJsonOutputParser() # Or StrOutputParser and manual parsing

    return retrieval_grader_score

# Update default model
def eval_doc(doc, query, method="binary", threshold=0.7, eval_model="gemini-2.0-flash"):
    '''
    doc : the document to evaluate
    query : the query to which to doc shoud be relevant
    method : "binary" or "score"
    threshold : for "score" method, score above which a doc is considered relevant
    '''
    try:
        if method == "binary":
            retrieval_grader_binary = get_binary_grader(model=eval_model)
            result = retrieval_grader_binary.invoke({"question": query, "document":doc})
            # Handle both structured and parsed output
            binary_score = result.binary_score if isinstance(result, GradeDocumentsBinary) else result.get("binary_score", "no")
            return 1 if (binary_score.lower() == 'yes') else 0
        elif method == "score":
            retrieval_grader_score = get_score_grader(model=eval_model)
            result = retrieval_grader_score.invoke({"query": query, "document":doc})
            # Handle both structured and parsed output
            score = result.score if isinstance(result, GradeDocumentsScore) else result.get("score")
            if score is not None:
                return score if float(score) >= threshold else 0
            else:
                print("Warning: Couldn't parse score, marking document as relevant by default.")
                return 1 # Default to relevant if score parsing fails
        else:
            raise ValueError("Invalid method")
    except Exception as e:
        print(f"Error evaluating document: {e}")
        return 0 # Default to irrelevant on error

# Update default model
def eval_docs(state: DocRetrieverState, config: ConfigSchema):
    """
    This node performs evaluation of the retrieved docs and
    """

    eval_method =  config["configurable"].get("eval_method") or "binary"
    MAX_DOCS = config["configurable"].get("max_docs") or 15
    # Update default model name
    eval_model_name = config["configurable"].get("eval_model") or "gemini-2.0-flash"
    valid_doc_scores = []

    # Ensure 'docs' exists and is a list
    docs_to_evaluate = state.get("docs", [])
    if not isinstance(docs_to_evaluate, list):
        print("Warning: 'docs' is not a list, skipping evaluation.")
        docs_to_evaluate = []

    # Sample safely
    sample_size = min(25, len(docs_to_evaluate))
    sampled_docs = sample(docs_to_evaluate, sample_size) if sample_size > 0 else []


    for doc in sampled_docs:
         # Ensure doc is not None before formatting
        if doc is None:
            print("Warning: Encountered None document during evaluation, skipping.")
            continue

        formatted_doc_str = format_doc(doc)
        # Add basic check for empty formatted doc
        if not formatted_doc_str.strip():
             print(f"Warning: Skipping empty formatted document: {doc}")
             continue

        score = eval_doc(
                        doc=formatted_doc_str,
                        query=state["query"], # This line assumes "query" exists in state
                        method=eval_method,
                        threshold=config["configurable"].get("eval_threshold") or 0.7,
                        eval_model=eval_model_name # Pass the eval_model name
                        )
        # Ensure score is numeric before appending
        if isinstance(score, (int, float)):
            if score > 0: # Only add if relevant (score > 0 or binary score == 1)
                 valid_doc_scores.append((doc, score))
        else:
             print(f"Warning: Received non-numeric score ({score}) for doc {doc}, skipping.")


    if eval_method == 'score':
        # Get at most MAX_DOCS items with the highest score if score method was used
        valid_docs_sorted = sorted(valid_doc_scores, key=lambda x: x[1], reverse=True) # Sort descending
        valid_docs = [valid_doc[0] for valid_doc in valid_docs_sorted[:MAX_DOCS]]
    else:
        # Get at mots MAX_DOCS items at random if binary method was used
        shuffle(valid_doc_scores)
        valid_docs = [valid_doc[0] for valid_doc in valid_doc_scores[:MAX_DOCS]]

    # Ensure existing valid_docs is a list before concatenating
    existing_valid_docs = state.get("valid_docs", [])
    if not isinstance(existing_valid_docs, list):
        existing_valid_docs = []

    return {"valid_docs": valid_docs + existing_valid_docs}



def build_data_retriever_graph(memory):
    """
    Builds the data_retriever graph
    """
    #with SqliteSaver.from_conn_string(":memory:") as memory :

    graph_builder_doc_retriever = StateGraph(DocRetrieverState)

    graph_builder_doc_retriever.add_node("generate_cypher", generate_cypher)
    graph_builder_doc_retriever.add_node("get_docs", get_docs)
    graph_builder_doc_retriever.add_node("eval_docs", eval_docs)


    graph_builder_doc_retriever.add_edge("__start__", "generate_cypher")
    graph_builder_doc_retriever.add_edge("generate_cypher", "get_docs")
    graph_builder_doc_retriever.add_edge("get_docs", "eval_docs")
    graph_builder_doc_retriever.add_edge("eval_docs", "__end__")

    graph_doc_retriever = graph_builder_doc_retriever.compile(checkpointer=memory)

    return graph_doc_retriever

# Remove Groq specific error handling function
# def error_concept_groq(msg,concepts,groq,question): ...