demo / app /modules /evaluation.py
tekville's picture
Initial commit
ff72db3
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