File size: 3,435 Bytes
fd5aa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from mistralai import Mistral, SDKError
from time import sleep
import json
import os

models = ["mistral-small-2409", "open-mistral-nemo"]

import random
def get_model():
    return random.choice(models)

def call_ai(prompt, json_mode):
    try:
        return _call_ai(prompt, json_mode)
    except SDKError as e:
        #Wait, then try again once
        sleep(11)
        return _call_ai(prompt, json_mode)
    except Exception as e:
        # Throw the error if it's not an SDKError
        raise

def _call_ai(prompt, json_mode):
    sleep(1.1)
    client = Mistral(api_key=os.getenv('MISTRAL_KEY'))

    extra_param = {}
    if json_mode:
        extra_param = { "response_format" : {"type": "json_object"} }

    chat_response = client.chat.complete(
        model = get_model(),
        messages = [
            {
                "role": "user",
                "content": prompt,
            },
        ],
        **extra_param
    )

    return chat_response.choices[0].message.content

def get_offer_information(company, offer):
    try:
        return _get_offer_information(company, offer)
    except json.decoder.JSONDecodeError as e:
        #try again once
        return _get_offer_information(company, offer)
    except Exception as e:
        # Throw the error if it's not an SDKError
        raise

def _get_offer_information(company, offer):
    prompt = """This is a job offer from the company '{}', make a JSON with this information:
- company_description (string): a description of the company in less than 15 words. 
- position_summary (string): a summary of the role in 3 bullet points
- language_requirements (string): the language requirements in French and English
- experience_requirements (string): the experience requirements
- is_an_internship (Boolean): true if it's an internship, false otherwise
- salary_range (string): the salary range in yearly salary if stated, write 'unknown' otherwise
- should_apply (Boolean): True if the offer requires up to 2 years of work experience and does not ask for other languages than English, French, Hindi or Nepali

Be concise in each answer. Answer in English.

Example:
{{
'company_description': 'Galileo Global Education: A leading international network of higher education institutions.',
'position_summary': 'Project Manager Marketing and Communication: Develop brand experience, manage marketing/communication plan, ensure brand image, monitor e-reputation, create content, and collaborate with digital team.',
'language_requirements': 'French Fluent and English Native',
'experience_requirements': 'Previous experience in a similar role, preferably in an agency.',
'is_an_internship': false,
'salary_range': '€38,000-€42,000',
'should_apply': true,
}}

Offer:
{}""".format(company, offer)
    result = call_ai(prompt, True)
    obj = json.loads(result)
    print(obj)
    #Check result
    if not "company_description" in obj:
        obj["company_description"] = ""
    if not "position_summary" in obj:
        obj["position_summary"] = ""
    if not "language_requirements" in obj:
        obj["language_requirements"] = ""
    if not "experience_requirements" in obj:
        obj["experience_requirements"] = ""
    if not "is_an_internship" in obj:
        obj["is_an_internship"] = False
    if not "salary_range" in obj:
        obj["salary_range"] = ""
    if not "should_apply" in obj:
        obj["should_apply"] = True
    
    return obj