From 9baf460c870c1d8b648b2c5de9efe617f82c6d93 Mon Sep 17 00:00:00 2001 From: Khanh Dinh Date: Fri, 3 Jan 2025 11:28:40 +0100 Subject: [PATCH] removed commented code and prints --- api.py | 14 +----- proposer.py | 124 ---------------------------------------------------- utils.py | 57 +----------------------- 3 files changed, 3 insertions(+), 192 deletions(-) diff --git a/api.py b/api.py index 07573b9..92440f1 100644 --- a/api.py +++ b/api.py @@ -23,11 +23,8 @@ def fetch_okrs(user_input: str): #system_prompt = settings["system_prompt"] #input_template = settings["input_template"] - print("fetch_okr-user_input:", user_input) - print("input_template:", INPUT_TEMPLATE) user_prompt = construct_prompt(prompt_template=PROMPT_TEMPLATE, user_input=user_input) - print("user_prompt:", user_prompt) headers = {"api-key": api_key, "Content-Type": "application/json"} body = { "messages": [ @@ -35,18 +32,11 @@ def fetch_okrs(user_input: str): {"role": "user", "content": user_prompt} ] } - #print("system prompt:", system_prompt) - print("request body:", body) + try: response = requests.post(url=api_url, headers=headers, json=body) response.raise_for_status() return response.json() except Exception as e: st.error(f"Error fetching data from API: {e}") - return None - - -#from config import INPUT_TEMPLATE -#result = fetch_okrs(user_input=INPUT_TEMPLATE) -#objective = result['choices'][0]['message']['content'] -#print(type(objective)) \ No newline at end of file + return None \ No newline at end of file diff --git a/proposer.py b/proposer.py index 2c4439e..c467df3 100644 --- a/proposer.py +++ b/proposer.py @@ -1,127 +1,3 @@ -'''import streamlit as st - -from api import fetch_okrs -from config import SYSTEM_PROMPT, INPUT_TEMPLATE, PROMPT_TEMPLATE -from utils import construct_prompt, extract_llm_response - -def proposer_page(): - # Streamlit App Layout - st.title("AO/PM OKR Proposer") - - # Input Section and Buttons Row - st.subheader("Enter your idea or goal:") - user_input = st.text_area( - "Input your idea here:", - value=st.session_state.get("user_input", INPUT_TEMPLATE.strip()), - height=300, - ) - - print("user_input:", user_input) - - generate_okrs_clicked = st.button("Generate OKR Proposal") - #col1, col2 = st.columns([1, 1]) - #with col1: - # if st.button("Reset All"): - # st.session_state.clear() - #with col2: - - - if generate_okrs_clicked: - if not user_input.strip(): - st.warning("Please provide some input before generating OKRs.") - else: - #user_input = st.session_state.get("user_input") - with st.spinner("Generating OKRs..."): - # Construct prompt and call API - #prompt = construct_prompt(prompt_template=PROMPT_TEMPLATE, user_input=user_input) - response = fetch_okrs(user_input=user_input) - #print("user_input:", user_input) - if response: - # Extract Objective and Key Results from response - print(response) - objective, key_results, hint = extract_llm_response(response) - - st.session_state["objective"] = objective - st.session_state["key_results"] = key_results - st.session_state["hint"] = hint - - #st.subheader("Hint to improve the OKR proposal") - #st.text(hint) - - # Display Results Only if an OKR Has Been Generated - if "objective" in st.session_state and "key_results" in st.session_state: - - # Display Objective Field with Responsibles Input Below It - st.subheader("Proposal Objective:") - objective_text = st.text_area( - "Proposal Objective:", - value=st.session_state.get("objective", ""), - height=100, - ) - - responsible_for_objective = st.text_input( - "Responsibles for Objective (comma-separated):", - value="", - placeholder="e.g., Khanh Dinh, John Doe", - key="responsibles_objective" - ) - - # Display Key Results with Responsibles Below Each One - st.subheader("Proposal Key Results:") - key_result_boxes = [] - responsibles_for_key_results = [] - - for i, kr in enumerate(st.session_state["key_results"], start=1): - kr_text = st.text_area(f"Key Result {i}:", value=kr, key=f"kr_{i}") - responsible_for_kr = st.text_input( - f"Responsibles for Key Result {i} (comma-separated):", - value="", - placeholder="e.g., Khanh Dinh", - key=f"responsibles_kr_{i}" - ) - - key_result_boxes.append(kr_text) - responsibles_for_key_results.append(responsible_for_kr) - - # Finalize Button Center-Aligned - #finalize_col = st.columns([3, 2, 3])[1] - #with finalize_col: - if st.button("Finalize"): - finalized_objective = objective_text.strip() - finalized_key_results = [st.session_state[f"kr_{i+1}"].strip() for i in range(len(key_result_boxes))] - - # Append initials of responsibles to Objective and Key Results - responsibles_list_objective = [name.strip() for name in responsible_for_objective.split(",") if name.strip()] - initials_objective = [f"[{''.join([part[0] for part in name.split()]).upper()}]" for name in responsibles_list_objective] - initials_str_objective = ", ".join(initials_objective) - - finalized_objective = f"{initials_str_objective} {finalized_objective}" - - finalized_key_results_with_initials = [] - for i, kr in enumerate(finalized_key_results): - responsibles_list_kr = [name.strip() for name in responsibles_for_key_results[i].split(",") if name.strip()] - initials_kr = [f"{''.join([part[0] for part in name.split()]).upper()}" for name in responsibles_list_kr] - initials_str_kr = ", ".join(initials_kr) - finalized_key_results_with_initials.append(f"KR{i+1}: [{initials_str_kr}] {kr}") - - # Display finalized data in non-editable format (full width) - st.subheader("Finalized Objective:") - st.code( - body=finalized_objective, - language=None, - wrap_lines=True - ) - - st.subheader("Finalized Key Results:") - for kr in finalized_key_results_with_initials: - st.code( - body=kr, - language=None, - wrap_lines=True - ) - -''' - import streamlit as st from api import fetch_okrs from config import INPUT_TEMPLATE, team diff --git a/utils.py b/utils.py index be70ad9..9c92ad5 100644 --- a/utils.py +++ b/utils.py @@ -1,66 +1,11 @@ import json import streamlit as st +import re # Function to construct the prompt def construct_prompt(prompt_template: str, user_input: str) -> str: return prompt_template.format(user_input=user_input) -'''# Function to extract and parse JSON response -def extract_llm_response(response): - print("response:", response) - try: - raw_message_content = response["choices"][0]["message"]["content"] - print("raw_message_content:", raw_message_content) - # Clean and parse the JSON content - cleaned_content = raw_message_content.replace("`", "").split("json")[-1] - - # for debugging - #if debug: - # print("cleaned:", '-'*50) - # print(cleaned_content.strip()) - - def parse_json_content(cleaned_content: str): - """ - Parses the cleaned content to extract valid JSON data. - - Args: - cleaned_content (str): The raw content containing JSON data. - - Returns: - dict or list: The parsed JSON object. - """ - import re - - # Step 1: Strip unwanted characters and clean the content - cleaned_content = cleaned_content.strip() - - # Step 2: Use regex to extract only the valid JSON block (e.g., starts with [ or {) - json_match = re.search(r"(\{.*\}|\[.*\])", cleaned_content, re.DOTALL) - - if not json_match: - raise ValueError("No valid JSON found in the content.") - - # Step 3: Extract and parse the valid JSON - valid_json = json_match.group(0) # Extract matched JSON block - try: - extracted_data = json.loads(valid_json) - except json.JSONDecodeError as e: - raise ValueError(f"Failed to decode JSON. Error: {e}\nContent:\n{valid_json}") - - return extracted_data - - parsed_data = parse_json_content(cleaned_content=cleaned_content) - print("parsed_data:",parsed_data) - print("debug:", parsed_data.get("objective", "")) - - #parsed_data = json.loads(cleaned_content) - return parsed_data.get("objective", ""), parsed_data.get("key_results", []) - except Exception as e: - st.error(f"Error parsing API response: {e}") - return "", []''' - -import json -import re def parse_json_content(cleaned_content: str): """ -- 2.45.2