Skip to content

Commit

Permalink
refactor: make use of logging rather then prints
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Nov 26, 2024
1 parent 2580d1e commit aa9fb54
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
13 changes: 6 additions & 7 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,20 @@ def extract_text(file: UploadFile) -> str:
@app.post("/plan-prat", response_model=PlanPratResponse)
def plan_prat(question: PlanPratRequest) -> PlanPratResponse:
"""
PlanPrat a query.
PlanPrat a query.
Args:
question (PlanPratRequest): The query to PlanPrat.
Returns:
PlanPratResponse: The PlanPrat response.
backend/src/main.py
Args:
question (PlanPratRequest): The query to PlanPrat.
Returns:
PlanPratResponse: The PlanPrat response.
backend/src/main.py
"""

logger.info(f"Query: {question}")
if not question.query:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Query is empty"
)
print(f"Query: {question.query}", flush=True)

response = invoke_plan_agent(question.query)
return PlanPratResponse(answer=response)
Expand Down
36 changes: 19 additions & 17 deletions backend/src/services/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def summarize_application_documents(documents: list[str]) -> list[str]:


def fill_out_checklist_responder(state):
print("\n---Filling out Checklist---")
logger.info("\n---Filling out Checklist---")
user_applications = state["user_application_documents"]
checklist = state["checklist"]
retrieval_state = state["retrieval_state"]
Expand All @@ -230,13 +230,13 @@ def fill_out_checklist_responder(state):
# Determine if laws are available
laws_available = bool(laws_and_regulations.strip())

print(f"Checklist: {checklist}", flush=True)
logger.info(f"Checklist: {checklist}")
for idx, checkpoint in enumerate(checklist):
# Extract the checkpoint text from the tuple
# checkpoint_text = checkpoint[idx]
checkpoint: Sjekkpunkt
print(f"Processing checkpoint {idx + 1}", flush=True)
print(f"Checkpoint: {checkpoint}", flush=True)
logger.info(f"Processing checkpoint {idx + 1}")
logger.info(f"Checkpoint: {checkpoint}")
checkpoint_text = checkpoint.model_dump()

# If there is feedback for this checkpoint, use it
Expand Down Expand Up @@ -344,7 +344,7 @@ def fill_out_checklist_responder(state):
need_retrieval = True
except json.JSONDecodeError:
# If parsing fails, mark as Uncertain and set retrieval flag only if laws are not available
print(f"Error parsing LLM response after cleanup: {response}")
logger.error(f"Error parsing LLM response after cleanup: {response}")
marked_checkpoint = MarkedCheckpoint(
check_point_name=checkpoint.Navn,
status="Uncertain",
Expand All @@ -359,14 +359,14 @@ def fill_out_checklist_responder(state):
state["should_retrieve"] = need_retrieval

if need_retrieval:
print("Writing questions for retrieval.")
logger.info("Writing questions for retrieval.")
uncertain_checkpoints = [
cp.check_point_name for cp in marked_checklist if cp.status == "Uncertain"
]
response = llm.predict(
f"For the following checkpoints marked as 'Uncertain', ask max 3 questions to clarify the applicability of laws and regulations:\n\n{uncertain_checkpoints}"
)
print(f"Question for retrieval: {response}")
logger.info(f"Question for retrieval: {response}")
state["retrieval_state"]["question"] = response
state["retrieval_performed"] = True # Mark that retrieval has been performed

Expand All @@ -386,10 +386,10 @@ def fill_out_checklist_responder(state):
def decide_next_step_after_fill_out(state):
"""Decide whether to proceed to 'retrieve' or 'marked_checklist_revisor'."""
if state["should_retrieve"]:
print("---DECISION: Need more information, proceeding to 'retrieve'---")
logger.info("---DECISION: Need more information, proceeding to 'retrieve'---")
return "retrieve"
else:
print(
logger.info(
"---DECISION: Enough information, proceeding to 'marked_checklist_revisor'---"
)
return "marked_checklist_revisor"
Expand All @@ -399,27 +399,29 @@ def decide_to_continue_after_revisor(state):
"""Decide whether to continue processing or end."""
# Check if the revision count has reached the limit
if state.get("revision_count", 0) >= 3:
print(
logger.info(
f"---DECISION: Maximum revision count reached ({state['revision_count']}), ending process---"
)
return END

if state.get("should_continue"):
# Check if retrieval has already been performed
if state.get("retrieval_performed", False):
print("---DECISION: Retrieval already performed, ending process---")
logger.info("---DECISION: Retrieval already performed, ending process---")
return END
else:
print("---DECISION: Looping back to 'fill_out_checklist_responder'---")
logger.info(
"---DECISION: Looping back to 'fill_out_checklist_responder'---"
)
return "fill_out_checklist_responder"
else:
print("---DECISION: Process complete---")
logger.info("---DECISION: Process complete---")
return END


def marked_checklist_revisor(state):
"""Node that revises the marked checklist and provides feedback."""
print("\n---Marked Checklist Revisor---")
logger.info("\n---Marked Checklist Revisor---")
marked_checklist: list[MarkedCheckpoint] = state["marked_checklist"]
checklist: list[Sjekkpunkt] = state["checklist"]
application_text = "\n\n".join(state["user_application_documents"])
Expand Down Expand Up @@ -482,17 +484,17 @@ def marked_checklist_revisor(state):

if "All good" in response.strip():
# No revisions needed
print("No revisions needed.")
logger.info("No revisions needed.")
state["should_continue"] = False
else:
try:
revised_items = json.loads(response)
# Store the feedback in the state
state["revisor_feedback"] = revised_items
state["should_continue"] = True
print("Feedback provided by revisor.")
logger.info("Feedback provided by revisor.")
except json.JSONDecodeError:
print(
logger.error(
f"Could not parse revision response after cleanup. LLM Response:\n{response}"
)
state["should_continue"] = False # End process if parsing failed
Expand Down
8 changes: 5 additions & 3 deletions backend/src/services/agent_parts/checklist_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import requests
import json
from pydantic import BaseModel
from typing import List, Optional

logger = logging.getLogger(__name__)


class Lovhjemmel(BaseModel):
Lovhjemmel: str
Expand Down Expand Up @@ -43,13 +46,12 @@ def retrieve_current_national_checklist() -> list[Sjekkpunkt]:
data = response.json()
json_data = [json.dumps(item) for item in data]
checklist = []
print(len(json_data))
logger.info(f"Amount of items: {len(json_data)}")
for item in json_data:
try:
checklist.append(Sjekkpunkt.model_validate_json(item))
except Exception as e:
print(f"Error: {e}")
print(f"Item: {item}")
logger.error(f"Error: {e} for Item: {item}")

unique_checklist = set()
for item in set(checklist):
Expand Down
27 changes: 15 additions & 12 deletions backend/src/services/agent_parts/crag.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma

from src.services.agent_parts.generator import llm, embedder, web_search_tool

logger = logging.getLogger(__name__)

urls = [
"https://lovdata.no/dokument/NL/lov/2008-06-27-71/*#*",
"https://www.dibk.no/regelverk/byggteknisk-forskrift-tek17",
Expand Down Expand Up @@ -119,9 +122,9 @@ def retrieve(state):
Returns:
state (dict): New key added to state, documents, that contains retrieved documents
"""
print("---RETRIEVE---")
logger.info("---RETRIEVE---")
question = state["retrieval_state"]["question"]
print(f"Question: {question}")
logger.info(f"Question: {question}")

# Retrieval
documents = retriever.get_relevant_documents(question)
Expand All @@ -140,7 +143,7 @@ def generate(state):
Returns:
state (dict): New key added to state, generation, that contains LLM generation
"""
print("---GENERATE---")
logger.info("---GENERATE---")
question = state["retrieval_state"]["question"]
documents = state["retrieval_state"]["documents"]

Expand All @@ -166,7 +169,7 @@ def grade_documents(state):
state (dict): Updates documents key with only filtered relevant documents
"""

print("---CHECK DOCUMENT RELEVANCE TO QUESTION---")
logger.info("---CHECK DOCUMENT RELEVANCE TO QUESTION---")
question = state["retrieval_state"]["question"]
documents = state["retrieval_state"]["documents"]

Expand All @@ -179,10 +182,10 @@ def grade_documents(state):
)
grade = score.binary_score
if grade == "yes":
print("---GRADE: DOCUMENT RELEVANT---")
logger.info("---GRADE: DOCUMENT RELEVANT---")
filtered_docs.append(d)
else:
print("---GRADE: DOCUMENT NOT RELEVANT---")
logger.info("---GRADE: DOCUMENT NOT RELEVANT---")
web_search = "Yes"
continue
retrieval_state = {
Expand All @@ -205,7 +208,7 @@ def transform_query(state):
state (dict): Updates question key with a re-phrased question
"""

print("---TRANSFORM QUERY---")
logger.info("---TRANSFORM QUERY---")
question = state["retrieval_state"]["question"]
documents = state["retrieval_state"]["documents"]

Expand All @@ -227,13 +230,13 @@ def web_search(state):
state (dict): Updates documents key with appended web results
"""

print("---WEB SEARCH---")
logger.info("---WEB SEARCH---")
question = state["retrieval_state"]["question"]
documents = state["retrieval_state"]["documents"]

# Web search
docs = web_search_tool.invoke({"query": question})
print(f"Web search results: {docs}")
logger.info(f"Web search results: {docs}")
web_results = ""
for d in docs:
if isinstance(d, str):
Expand Down Expand Up @@ -262,19 +265,19 @@ def decide_to_generate(state):
str: Binary decision for next node to call
"""

print("---ASSESS GRADED DOCUMENTS---")
logger.info("---ASSESS GRADED DOCUMENTS---")
state["retrieval_state"]["question"]
web_search = state["retrieval_state"]["web_search"]
state["retrieval_state"]["documents"]

if web_search == "Yes":
# All documents have been filtered check_relevance
# We will re-generate a new query
print(
logger.info(
"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, TRANSFORM QUERY---"
)
return "transform_query"
else:
# We have relevant documents, so generate answer
print("---DECISION: GENERATE---")
logger.info("---DECISION: GENERATE---")
return "generate"

0 comments on commit aa9fb54

Please sign in to comment.