Spaces:
Sleeping
Sleeping
File size: 3,031 Bytes
9c90775 | 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 | from langgraph.graph import StateGraph, START, END
from src.memory import memory
from src.states.Main_State import State
from src.nodes.main_nodes import (
orchastrator_node,
query_generation_node,
retreiver_node,
is_retreived_data_enough,
web_search_node,
document_refiner,
get_chat_node_content,
chat_node
)
graph = StateGraph(State)
# ===================== Adding Nodes =====================
graph.add_node(
"orchastrator",
orchastrator_node
)
graph.add_node(
"query_generation",
query_generation_node
)
graph.add_node(
"retreiver",
retreiver_node
)
graph.add_node(
"relevance_checker",
is_retreived_data_enough
)
graph.add_node(
"web_search",
web_search_node
)
graph.add_node(
"document_refiner",
document_refiner
)
graph.add_node(
"context_builder",
get_chat_node_content
)
graph.add_node(
"chat",
chat_node
)
# ===================== Adding Edges =====================
graph.add_edge(
START,
"orchastrator"
)
graph.add_conditional_edges(
"orchastrator",
lambda state: (
"query_generation"
if state["require_db_search"]
else "chat"
),
{
"query_generation": "query_generation",
"chat": "chat"
}
)
graph.add_edge(
"query_generation",
"retreiver"
)
graph.add_edge(
"retreiver",
"relevance_checker"
)
graph.add_conditional_edges(
"relevance_checker",
lambda state: (
"document_refiner"
if state["relevance"] == "CORRECT" or state["relevance"] == "AMBIGUOUS"
else "web_search"
),
{
"document_refiner": "document_refiner",
"web_search": "web_search"
}
)
graph.add_edge(
"document_refiner",
"context_builder"
)
graph.add_edge(
"web_search",
"context_builder"
)
graph.add_edge(
"context_builder",
"chat"
)
graph.add_edge(
"chat",
END
)
graph = graph.compile(
checkpointer=memory
)
try:
graph.get_graph().draw_mermaid_png(
output_file_path="graph_visualization.png"
)
except Exception as e:
logging.exception(
f"Failed to generate graph visualization: {e}"
)
async def deleteThread(thread_id: str):
try:
cp = memory
state = await cp.aget_tuple(config={'configurable': {'thread_id': thread_id}})
if state is None:
logging.info(f"Thread {thread_id} not found, nothing to delete.")
return False
await cp.adelete_thread(thread_id=thread_id)
logging.info(f"Thread {thread_id} deleted successfully.")
return True
except Exception as e:
logging.error(f"Error deleting thread {thread_id}: {e}")
return False
async def load_conversation(thread_id):
try:
state = graph.get_state(config={'configurable': {'thread_id': thread_id}})
return state.values.get('messages', [])
except Exception as e:
logging.error(f"Error loading conversation: {e}")
return [] |