|
|
"""
|
|
|
CRANE AI - Kod Yazım Modülü
|
|
|
"""
|
|
|
|
|
|
import re
|
|
|
from typing import Dict, Any
|
|
|
from core.base_module import BaseMicroModule
|
|
|
import logging
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class CodeModule(BaseMicroModule):
|
|
|
"""Kod yazımı için özelleşmiş modül"""
|
|
|
|
|
|
def __init__(self, config: Dict[str, Any]):
|
|
|
super().__init__(
|
|
|
model_id="deepseek-ai/deepseek-coder-1.3b-instruct",
|
|
|
config=config
|
|
|
)
|
|
|
|
|
|
|
|
|
self.code_keywords = {
|
|
|
"function", "class", "def", "import", "from", "return",
|
|
|
"if", "else", "for", "while", "try", "except", "with",
|
|
|
"python", "javascript", "java", "c++", "html", "css",
|
|
|
"kod", "kodu", "script", "fonksiyon", "sınıf", "algoritma",
|
|
|
"program", "yazılım", "debug", "hata", "fix", "düzelt"
|
|
|
}
|
|
|
|
|
|
|
|
|
self.programming_languages = {
|
|
|
"python", "javascript", "java", "c++", "c#", "php", "ruby",
|
|
|
"go", "rust", "kotlin", "swift", "typescript", "html", "css",
|
|
|
"sql", "bash", "powershell", "r", "matlab", "scala"
|
|
|
}
|
|
|
|
|
|
def can_handle(self, query: str, context: Dict[str, Any]) -> float:
|
|
|
"""Kod yazımı sorguları için uygunluk skoru"""
|
|
|
query_lower = query.lower()
|
|
|
|
|
|
|
|
|
code_score = 0
|
|
|
for keyword in self.code_keywords:
|
|
|
if keyword in query_lower:
|
|
|
code_score += 0.15
|
|
|
|
|
|
|
|
|
for lang in self.programming_languages:
|
|
|
if lang in query_lower:
|
|
|
code_score += 0.3
|
|
|
|
|
|
|
|
|
action_keywords = ["yaz", "oluştur", "geliştir", "kur", "hazırla", "tasarla"]
|
|
|
for action in action_keywords:
|
|
|
if action in query_lower:
|
|
|
code_score += 0.2
|
|
|
|
|
|
|
|
|
if any(char in query for char in ["()", "{}", "[]", "==", "!=", "<=", ">="]):
|
|
|
code_score += 0.15
|
|
|
|
|
|
|
|
|
if "```" in query or "def " in query or "class " in query:
|
|
|
code_score += 0.3
|
|
|
|
|
|
|
|
|
code_types = ["hesap", "makinesi", "calculator", "app", "uygulama", "website", "site"]
|
|
|
for code_type in code_types:
|
|
|
if code_type in query_lower:
|
|
|
code_score += 0.25
|
|
|
|
|
|
|
|
|
return min(code_score, 1.0)
|
|
|
|
|
|
async def process(self, query: str, context: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
"""Kod yazımı işlemi"""
|
|
|
try:
|
|
|
|
|
|
prompt = self._build_code_prompt(query, context)
|
|
|
|
|
|
|
|
|
response = await self.generate_response(
|
|
|
prompt,
|
|
|
max_tokens=self.config.get("max_tokens", 2048),
|
|
|
temperature=0.1
|
|
|
)
|
|
|
|
|
|
|
|
|
code_blocks = self._extract_code_blocks(response)
|
|
|
|
|
|
return {
|
|
|
"response": response,
|
|
|
"code_blocks": code_blocks,
|
|
|
"language": self._detect_language(query),
|
|
|
"module": "code_module",
|
|
|
"confidence": self.can_handle(query, context)
|
|
|
}
|
|
|
|
|
|
except Exception as e:
|
|
|
logger.error(f"Code processing error: {str(e)}")
|
|
|
return {
|
|
|
"error": str(e),
|
|
|
"module": "code_module"
|
|
|
}
|
|
|
|
|
|
def _build_code_prompt(self, query: str, context: Dict[str, Any]) -> str:
|
|
|
"""Kod yazımı için prompt hazırlar"""
|
|
|
|
|
|
|
|
|
language = self._detect_language(query)
|
|
|
|
|
|
|
|
|
prompt = f"""Sen bir uzman programcısın. Kullanıcının sorusunu anla ve en iyi kodu yaz.
|
|
|
|
|
|
Kullanıcı sorusu: {query}
|
|
|
|
|
|
Lütfen:
|
|
|
1. Temiz, okunabilir kod yaz
|
|
|
2. Kod açıklamalarını Türkçe yap
|
|
|
3. En iyi pratikleri kullan
|
|
|
4. Gerekirse örnek kullanımı göster
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
if language:
|
|
|
prompt += f"Programlama dili: {language}\n"
|
|
|
|
|
|
|
|
|
if context.get("history"):
|
|
|
prompt += f"Önceki konuşma: {context['history'][-1]}\n"
|
|
|
|
|
|
return prompt
|
|
|
|
|
|
def _detect_language(self, query: str) -> str:
|
|
|
"""Sorgudan programlama dilini tespit eder"""
|
|
|
query_lower = query.lower()
|
|
|
|
|
|
for lang in self.programming_languages:
|
|
|
if lang in query_lower:
|
|
|
return lang
|
|
|
|
|
|
|
|
|
return "python"
|
|
|
|
|
|
def _extract_code_blocks(self, response: str) -> list:
|
|
|
"""Yanıttan kod bloklarını ayıklar"""
|
|
|
code_blocks = []
|
|
|
|
|
|
|
|
|
pattern = r'```(\w+)?\n(.*?)\n```'
|
|
|
matches = re.findall(pattern, response, re.DOTALL)
|
|
|
|
|
|
for match in matches:
|
|
|
lang, code = match
|
|
|
code_blocks.append({
|
|
|
"language": lang or "text",
|
|
|
"code": code.strip()
|
|
|
})
|
|
|
|
|
|
|
|
|
inline_pattern = r'`([^`]+)`'
|
|
|
inline_matches = re.findall(inline_pattern, response)
|
|
|
|
|
|
for code in inline_matches:
|
|
|
if len(code) > 5:
|
|
|
code_blocks.append({
|
|
|
"language": "inline",
|
|
|
"code": code.strip()
|
|
|
})
|
|
|
|
|
|
return code_blocks |