import json from typing import Union def extract_json(input_string: str) -> Union[bool, dict]: """String to Json function""" # First, ensure we remove json wrapper input_string = input_string.replace("```json", "```").replace("```", "") # Now, ensure we have stripped everything so it is just json input_string_formatted = input_string.lstrip("{").rstrip("}") # Ensure we do not have the weird \_ behaviour that models sometimes include input_string_formatted = input_string_formatted.replace("\_", "_") try: return True, json.loads(input_string_formatted) except json.JSONDecodeError as e: print(f"Error parsing JSON Output: {input_string}. Error: {e}") return False, {} def format_chat_history_cohere(chat_history: list, background_info: dict) -> list: """Takes streamlit chat history, and converts to cohere format""" # TODO: Could use cohere to track history, maybe for the future new_output = [ { "role": "USER", "message": f"Hi there! Here is my CV! {background_info['cv']}.\n\n I'd like you to act as a senior technical recruiter. Critique my CV and its' suitability for the role. You may only ask me one question at a time. Be as critical of the CV as possible, really focus on the CV and its relevance to the role. Ask about inconsistencies, mistakes in grammar, things which are technically not quite right suggesting a misunderstanding, or ask me to justify claims I make in my CV. You could also ask specific technical details about the architectures I have used.", }, { "role": "CHATBOT", "message": f"Thanks. Can you send me the job posting?", }, { "role": "USER", "message": f"Here is the job posting: {background_info['job_posting']}", }, ] # Lazy approach to format it correctly for cohere input! for item in chat_history: new_output.append( { "role": "USER" if item["role"] == "user" else "CHATBOT", "message": item["message"], } ) return new_output if __name__ == "__main__": example_json = """ ```json { "dogs": "are blue?" } """ extract_json(example_json)