File size: 1,615 Bytes
d60934b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from mistralai import Mistral
import os
import orjson
import time

MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")


class ScoringService:
    def __init__(self: "ScoringService"):
        self.client = Mistral(api_key=MISTRAL_API_KEY)
        self.model = "mistral-small-2409"
        self.max_retries = 3

    def is_similar(
        self: "ScoringService", password: str, guess: str, theme: str
    ) -> bool:
        messages = [
            {
                "role": "system",
                "content": """
                Return a similarity score between the two given words, relatively to a theme. Return the score in the range [0, 1] in a JSON format with the key 'score'
                """,
            },
            {
                "role": "user",
                "content": f"Answer: {password}\nGuess: {guess}\nTheme: {theme}",
            },
        ]

        for attempt in range(self.max_retries):
            try:
                response = self.client.chat.complete(
                    model=self.model,
                    messages=messages,
                    temperature=0.0,
                    response_format={
                        "type": "json_object",
                    },
                )

                # Parse the response with orjson
                parsed_response = orjson.loads(response.choices[0].message.content)
                return parsed_response["score"]
            except orjson.JSONDecodeError as e:
                if attempt < self.max_retries - 1:
                    time.sleep(1)
                else:
                    raise e

        return 0.5