File size: 3,067 Bytes
ff72db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from huggingface_hub import InferenceClient

class Evaluation:
    def __init__(self, model: str = "Qwen/Qwen2.5-72B-Instruct"):
        """
        Args:
            model (str): ์‚ฌ์šฉํ•  Hugging Face ๋ชจ๋ธ ์ด๋ฆ„ (๊ธฐ๋ณธ๊ฐ’: Qwen/Qwen2.5-72B-Instruct).
        """
        self.api_key = os.getenv("HF_API_KEY")
        if not self.api_key:
            raise ValueError("HF_API_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
        self.client = InferenceClient(api_key=self.api_key)
        self.model = model

    def evaluate(self, instruction: str, answer: str) -> str:
        """
        ์‚ฌ์šฉ์ž์˜ ๋‹ต๋ณ€๊ณผ ํ‰๊ฐ€ ๊ธฐ์ค€์„ ๊ธฐ๋ฐ˜์œผ๋กœ AI ๋ชจ๋ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

        Args:
            instruction (str): ํ‰๊ฐ€ ๊ธฐ์ค€์ด ํฌํ•จ๋œ ์ง€์นจ.
            answer (str): ์‚ฌ์šฉ์ž ๋‹ต๋ณ€.

        Returns:
            str: ํ‰๊ฐ€ ๊ฒฐ๊ณผ.
        """
        messages = [
            {"role": "system", "content": "์ˆ˜์—…๋„๊ตฌ ๊ตฌ์„ฑ ๋งˆ๋ฒ•์‚ฌ์ž…๋‹ˆ๋‹ค. ํ€ด์ฆˆ, ๊ณผ์ œ, ํ† ๋ก ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."},
            {"role": "user", "content": instruction},
        ]

        try:
            stream = self.client.chat.completions.create(
                model=self.model,
                messages=messages,
                temperature=0.2,
                max_tokens=2048,
                top_p=0.7,
                stream=True,
            )

            result = ""
            for chunk in stream:
                if "delta" in chunk.choices[0]:
                    result += chunk.choices[0].delta.content
                    print(f"Intermediate result: {result}")  # ๋””๋ฒ„๊น…์šฉ ์ถœ๋ ฅ

            return result.strip()

        except Exception as e:
            error_message = f"An error occurred during evaluation: {e}"
            print(error_message)  # ๋””๋ฒ„๊น…์šฉ ์ถœ๋ ฅ
            return error_message

    async def evaluate_stream(self, instruction: str):
        """
        ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ํ‰๊ฐ€ ๊ฒฐ๊ณผ๋ฅผ ์ŠคํŠธ๋ฆฌ๋ฐ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

        Args:
            instruction (str): ํ‰๊ฐ€ ๊ธฐ์ค€์ด ํฌํ•จ๋œ ์ง€์นจ.

        Yields:
            str: ์‹ค์‹œ๊ฐ„ ํ‰๊ฐ€ ๊ฒฐ๊ณผ.
        """
        messages = [
            {"role": "system", "content": "์„ ์ƒ๋‹˜์—๊ฒŒ ๊ผญ ํ•„์š”ํ•œ ์ˆ˜์—…๋„๊ตฌ ๊ตฌ์„ฑ ๋งˆ๋ฒ•์‚ฌ์ž…๋‹ˆ๋‹ค."},
            {"role": "user", "content": instruction},
        ]

        try:
            stream = self.client.chat.completions.create(
                model=self.model,
                messages=messages,
                temperature=0.2,
                max_tokens=2048,
                top_p=0.7,
                stream=True,
            )

            for chunk in stream:
                if "delta" in chunk.choices[0]:
                    content = chunk.choices[0].delta.content
                    print(f"Streaming result: {content}")  # ๋””๋ฒ„๊น…์šฉ ์ถœ๋ ฅ
                    yield content

        except Exception as e:
            error_message = f"Error: {e}"
            print(error_message)  # ๋””๋ฒ„๊น…์šฉ ์ถœ๋ ฅ
            yield error_message