Spaces:
Sleeping
Sleeping
File size: 2,279 Bytes
98eaa40 0c94c61 98eaa40 0c94c61 98eaa40 0c94c61 98eaa40 0c94c61 98eaa40 0c94c61 98eaa40 0c94c61 98eaa40 |
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 |
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)
|