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