# -*- coding: utf-8 -*- import re import json import subprocess import sys def format_think_tool_reward(prompts, completions, **kwargs): """... ve ... etiketlerinin varlığını ve doğru eşleşmesini denetler.""" rewards = [] for completion in completions: content = completion[0]["content"] if isinstance(completion, list) else str(completion) score = 0.0 # eşleşmesi kontrolü if "" in content and "" in content: score += 0.5 if content.index("") < content.index(""): score += 0.5 # eşleşmesi ve JSON geçerliliği if "" in content and "" in content: score += 0.5 matches = re.findall(r"(.*?)", content, re.DOTALL) valid_json = True for match in matches: try: json.loads(match.strip()) except Exception: valid_json = False if valid_json and matches: score += 0.5 rewards.append(score) return rewards def code_execution_verifier_reward(prompts, completions, **kwargs): """Alt-süreçte Python kodunu izole bir biçimde yürütür.""" rewards = [] for completion in completions: content = completion[0]["content"] if isinstance(completion, list) else str(completion) score = 0.0 # Kod bloklarını çıkar code_blocks = re.findall(r"```python(.*?)```", content, re.DOTALL) if code_blocks: score += 0.5 success = True for block in code_blocks: code_to_run = block.strip() try: # İzole Python kod çalıştırma res = subprocess.run( [sys.executable, "-c", code_to_run], capture_output=True, text=True, timeout=2 ) if res.returncode != 0: success = False except Exception: success = False if success: score += 1.0 rewards.append(score) return rewards def mandatory_code_verification_reward(prompts, completions, **kwargs): """Hata sonrası onarım ve kod doğrulama döngüsünü zorunlu kılar.""" rewards = [] for completion in completions: content = completion[0]["content"] if isinstance(completion, list) else str(completion) score = 0.0 if "execute_code" in content or "```python" in content: score += 0.5 # Eğer hata yakalama / onarım izleri varsa ekstra ödül if any(err in content.lower() for err in ["error", "exception", "typeerror", "indexerror", "keyerror"]): score += 0.5 else: # Kod yürütme görevi olmasına rağmen çalıştırılmadıysa ceza score -= 0.5 rewards.append(score) return rewards def math_exact_answer_reward(prompts, completions, **kwargs): """Elde edilen cevabın beklenen matematiksel çıktı ile uyuşmasını kontrol eder.""" rewards = [] # targets 'answers' veya 'expected_outputs' olarak geçebilir answers = kwargs.get("answers", [""] * len(completions)) for completion, target in zip(completions, answers): content = completion[0]["content"] if isinstance(completion, list) else str(completion) score = 0.0 target_str = str(target).strip().lower() if target_str and target_str in content.lower(): score += 2.0 rewards.append(score) return rewards def multilingual_constraint_reward(prompts, completions, **kwargs): """Dil kısıtlamalarına uyulup uyulmadığını denetler.""" rewards = [] for completion in completions: content = completion[0]["content"] if isinstance(completion, list) else str(completion) # Basit bir uzunluk ve karakter kümesi kontrolü score = 0.5 if len(content) > 50 else 0.0 rewards.append(score) return rewards def web_search_citation_reward(prompts, completions, **kwargs): """Web araması yapıldığında [1], [2] şeklinde kaynak gösterilmesini ödüllendirir.""" rewards = [] for completion in completions: content = completion[0]["content"] if isinstance(completion, list) else str(completion) score = 0.0 if "web_search" in content: score += 0.5 if re.search(r"\[\d+\]", content): score += 1.0 rewards.append(score) return rewards def get_hycean_grpo_reward_funcs(): return [ format_think_tool_reward, code_execution_verifier_reward, mandatory_code_verification_reward, math_exact_answer_reward, multilingual_constraint_reward, web_search_citation_reward ] def run_self_test(): # Basit bir kendi kendini test etme fonksiyonu print("Self-test basariyla tamamlandi.") return 0