File size: 1,998 Bytes
98eaa40
0c94c61
98eaa40
06d98e4
 
98eaa40
0c94c61
98eaa40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c94c61
98eaa40
 
 
06d98e4
 
 
98eaa40
 
 
06d98e4
98eaa40
 
 
06d98e4
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
65
66
67
68
import json
from typing import Union

from utils.interview_messages import first_conversation_message


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": first_conversation_message.replace(
                "<cv>", background_info["cv"]
            ),
        },
        {
            "role": "CHATBOT",
            "message": f"Thanks. Can you send me the job posting so I can reference that while I think of some questions to ask you?",
        },
        {
            "role": "USER",
            "message": f"Here is the contents of the job posting that I'm interested in: {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)