File size: 4,149 Bytes
37d3a3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import openai;
import json, os,sys
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ.get("OPENAI_API_KEY")
from Candidate import JobCandidate
def printc(obj, color="cyan"):
    color_code = {
        "black": "30", "red": "31", "green": "32", "yellow": "33",
        "blue": "34", "magenta": "35", "cyan": "36", "white": "37"
    }
    colored_text = f"\033[{color_code[color]}m{obj}\033[0m" if color in color_code else obj
    print(colored_text)


LLM=os.environ.get("COMPARATOR_LLM","gpt-4-0613")
# LLM=os.environ.get("COMPARATOR_LLM","gpt-3.5-turbo-1106")
def getContent(resumeA: str, resumeB: str) -> str:
    return (
        "Given the following two SWE candidates, choose between the two. Here is the rubric: "
        + get_rubric()
        + "Candidate A: "
        + "\nRESUME:\n" +resumeA+"\nEND Resume\n"
        + " END OF Candidate A"
        + "\n\nCandidate B: "
        + "\nRESUME:\n" +resumeB+"\nEND Resume\n"
        + " END OF Candidate B"
    )



def compare_resumes(content:str, nameA="", nameB=""):
    choice =0
    response = openai.ChatCompletion.create(
        model=LLM,
        messages=[{"role": "user", "content": content}],
        
        functions=[
            {
                "name": "selectCanidate",
                "description": "choose between the two canidates",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "choice_num": {
                        "type": "integer",
                        "description": "1 for Candidate A is the best fit, 2 for Candidate B is the best fit",
                        "required": ["choice_num"],
                        },
                        "justifcation": {
                        "type": "string",
                        "description": "justifcation for why you chose the candidate max 25 words",
                        "required": ["justifcation"],
                        },
                    }
                },
            }
        ],
        function_call="auto",


    )

    message = response["choices"][0]["message"]

    if message.get("function_call"):
        function_name = message["function_call"]["name"]
        try:
            function_args = json.loads(message["function_call"]["arguments"])
            choice = (int(function_args["choice_num"]))
        except:
            printc("eroor","red")
            printc(message["function_call"],'red')

            return 1
        if function_name == "selectCanidate":

            if choice==1:
                printc(nameA+" wins over "+nameB,"cyan")
            elif choice==2:
                printc(nameB+" wins over "+nameA,"green")

            printc(function_args["justifcation"],"yellow")

    return choice
    

def get_rubric():
    text = open("rubric.txt","r").read()
    return "\nRubric:\n" +str(text)+"\nEND Rubric\n"





def comp(candidateA:JobCandidate, candidateB:JobCandidate, rub_id:int=0 ) -> int:
    comp_table= json.load(open("comparisons.json","r"))
    tag= (candidateA.email+"#"+candidateB.email+"#"+str(rub_id))
    inv_tag= (candidateB.email+"#"+candidateA.email+"#"+str(rub_id))
    if tag in comp_table:
        return comp_table[tag]
    elif inv_tag in comp_table:
        return comp_table[inv_tag] * -1
    else:
        choice = compare_resumes(getContent(candidateA.resume_text, candidateB.resume_text), candidateA.name, candidateB.name)   
        if choice == 1:
            choice = -1
        elif choice == 2:
            choice = 1
        comp_table[tag]=choice

        json.dump(comp_table, open("comparisons.json","w"))
        
        return choice


def bubble_sort(candidates: list) -> list:
    n = len(candidates)
    for i in range(n):
        swapped = False
        for j in range(0, n-i-1):
            if candidates[j].email == candidates[j+1].email:
                continue
            elif comp(candidates[j], candidates[j+1]) > 0:
                candidates[j], candidates[j+1] = candidates[j+1], candidates[j]
                swapped = True
        if not swapped:
            break


    return candidates