Spaces:
Sleeping
Sleeping
merge1
Browse files- utils/deepseek_client.py +86 -38
utils/deepseek_client.py
CHANGED
|
@@ -5,6 +5,7 @@ DeepSeek API ν΄λΌμ΄μΈνΈ λͺ¨λ
|
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
import logging
|
|
|
|
| 8 |
from typing import List, Dict, Any, Optional, Union
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
import requests
|
|
@@ -82,22 +83,42 @@ class DeepSeekLLM:
|
|
| 82 |
|
| 83 |
# API μμ² λ³΄λ΄κΈ°
|
| 84 |
endpoint = f"{self.api_base}/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
response = requests.post(
|
| 86 |
endpoint,
|
| 87 |
headers=headers,
|
| 88 |
-
json=data
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
-
# μλ΅
|
| 92 |
-
response.
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
except requests.exceptions.RequestException as e:
|
| 96 |
logger.error(f"DeepSeek API μμ² μ€ν¨: {e}")
|
| 97 |
-
|
| 98 |
-
except
|
| 99 |
-
logger.error(f"DeepSeek API
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
def generate(
|
| 103 |
self,
|
|
@@ -135,16 +156,31 @@ class DeepSeekLLM:
|
|
| 135 |
**kwargs
|
| 136 |
)
|
| 137 |
|
| 138 |
-
# μλ΅
|
| 139 |
-
if
|
| 140 |
-
logger.error("
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
-
logger.error(f"ν
μ€νΈ μμ± μ€
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
def rag_generate(
|
| 150 |
self,
|
|
@@ -177,39 +213,51 @@ class DeepSeekLLM:
|
|
| 177 |
- κ²μ λ΄μ©μ κ·Έλλ‘ λ³΅μ¬νμ§ λ§κ³ , μμ°μ€λ¬μ΄ νκ΅μ΄λ‘ λ΅λ³μ μμ±νμΈμ.
|
| 178 |
- λ΅λ³μ κ°κ²°νκ³ μ ννκ² μ 곡νμΈμ."""
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
| 198 |
|
| 199 |
<context>
|
| 200 |
{context_text}
|
| 201 |
</context>
|
| 202 |
|
| 203 |
μ κ²μ κ²°κ³Όλ₯Ό μ°Έκ³ νμ¬ μ§λ¬Έμ λ΅λ³ν΄ μ£ΌμΈμ."""
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
| 207 |
prompt=prompt,
|
| 208 |
system_prompt=system_prompt,
|
| 209 |
temperature=temperature,
|
| 210 |
max_tokens=max_tokens,
|
| 211 |
**kwargs
|
| 212 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
except Exception as e:
|
| 214 |
-
logger.error(f"RAG ν
μ€νΈ μμ± μ€
|
| 215 |
-
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
import logging
|
| 8 |
+
import traceback
|
| 9 |
from typing import List, Dict, Any, Optional, Union
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
import requests
|
|
|
|
| 83 |
|
| 84 |
# API μμ² λ³΄λ΄κΈ°
|
| 85 |
endpoint = f"{self.api_base}/v1/chat/completions"
|
| 86 |
+
|
| 87 |
+
# λλ²κΉ
: API μμ² λ°μ΄ν° λ‘κΉ
(λ―Όκ° μ 보 μ μΈ)
|
| 88 |
+
debug_data = data.copy()
|
| 89 |
+
debug_data["messages"] = f"[{len(data['messages'])}κ° λ©μμ§]"
|
| 90 |
+
logger.debug(f"DeepSeek API μμ² λ°μ΄ν°: {json.dumps(debug_data)}")
|
| 91 |
+
|
| 92 |
response = requests.post(
|
| 93 |
endpoint,
|
| 94 |
headers=headers,
|
| 95 |
+
json=data,
|
| 96 |
+
timeout=30 # 30μ΄ νμμμ μ€μ
|
| 97 |
)
|
| 98 |
|
| 99 |
+
# μλ΅ μν μ½λ νμΈ
|
| 100 |
+
if not response.ok:
|
| 101 |
+
logger.error(f"DeepSeek API μ€λ₯: μν μ½λ {response.status_code}")
|
| 102 |
+
logger.error(f"μλ΅ λ΄μ©: {response.text}")
|
| 103 |
+
return {"error": f"API μ€λ₯: μν μ½λ {response.status_code}", "detail": response.text}
|
| 104 |
+
|
| 105 |
+
# μλ΅ νμ±
|
| 106 |
+
try:
|
| 107 |
+
result = response.json()
|
| 108 |
+
logger.debug(f"API μλ΅ κ΅¬μ‘°: {list(result.keys())}")
|
| 109 |
+
return result
|
| 110 |
+
except json.JSONDecodeError as e:
|
| 111 |
+
logger.error(f"DeepSeek API JSON νμ± μ€ν¨: {e}")
|
| 112 |
+
logger.error(f"μλ³Έ μλ΅: {response.text[:500]}...")
|
| 113 |
+
return {"error": "API μλ΅μ νμ±ν μ μμ΅λλ€", "detail": str(e)}
|
| 114 |
|
| 115 |
except requests.exceptions.RequestException as e:
|
| 116 |
logger.error(f"DeepSeek API μμ² μ€ν¨: {e}")
|
| 117 |
+
return {"error": f"API μμ² μ€ν¨: {str(e)}"}
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logger.error(f"DeepSeek API νΈμΆ μ€ μμμΉ λͺ»ν μ€λ₯: {e}")
|
| 120 |
+
logger.error(traceback.format_exc())
|
| 121 |
+
return {"error": f"μμμΉ λͺ»ν μ€λ₯: {str(e)}"}
|
| 122 |
|
| 123 |
def generate(
|
| 124 |
self,
|
|
|
|
| 156 |
**kwargs
|
| 157 |
)
|
| 158 |
|
| 159 |
+
# μ€λ₯ μλ΅ νμΈ
|
| 160 |
+
if "error" in response:
|
| 161 |
+
logger.error(f"ν
μ€νΈ μμ± μ€ API μ€λ₯: {response['error']}")
|
| 162 |
+
error_detail = response.get("detail", "")
|
| 163 |
+
return f"API μ€λ₯: {response['error']} {error_detail}"
|
| 164 |
+
|
| 165 |
+
# μλ΅ νμ κ²μ¦
|
| 166 |
+
if 'choices' not in response or not response['choices']:
|
| 167 |
+
logger.error(f"API μλ΅μ 'choices' νλκ° μμ΅λλ€: {response}")
|
| 168 |
+
return "μλ΅ νμ μ€λ₯: μμ±λ ν
μ€νΈλ₯Ό μ°Ύμ μ μμ΅λλ€."
|
| 169 |
|
| 170 |
+
# λ©μμ§ μ»¨ν
μΈ νμΈ
|
| 171 |
+
choice = response['choices'][0]
|
| 172 |
+
if 'message' not in choice or 'content' not in choice['message']:
|
| 173 |
+
logger.error(f"API μλ΅μ μμ νλκ° μμ΅λλ€: {choice}")
|
| 174 |
+
return "μλ΅ νμ μ€λ₯: λ©μμ§ λ΄μ©μ μ°Ύμ μ μμ΅λλ€."
|
| 175 |
+
|
| 176 |
+
generated_text = choice['message']['content'].strip()
|
| 177 |
+
logger.info(f"ν
μ€νΈ μμ± μλ£ (κΈΈμ΄: {len(generated_text)})")
|
| 178 |
+
return generated_text
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
+
logger.error(f"ν
μ€νΈ μμ± μ€ μμΈ λ°μ: {e}")
|
| 182 |
+
logger.error(traceback.format_exc())
|
| 183 |
+
return f"μ€λ₯ λ°μ: {str(e)}"
|
| 184 |
|
| 185 |
def rag_generate(
|
| 186 |
self,
|
|
|
|
| 213 |
- κ²μ λ΄μ©μ κ·Έλλ‘ λ³΅μ¬νμ§ λ§κ³ , μμ°μ€λ¬μ΄ νκ΅μ΄λ‘ λ΅λ³μ μμ±νμΈμ.
|
| 214 |
- λ΅λ³μ κ°κ²°νκ³ μ ννκ² μ 곡νμΈμ."""
|
| 215 |
|
| 216 |
+
try:
|
| 217 |
+
# μ€μ: 컨ν
μ€νΈ κΈΈμ΄ μ ν
|
| 218 |
+
max_context = 10
|
| 219 |
+
if len(context) > max_context:
|
| 220 |
+
logger.warning(f"컨ν
μ€νΈκ° λ무 κΈΈμ΄ μ²μ {max_context}κ°λ§ μ¬μ©ν©λλ€.")
|
| 221 |
+
context = context[:max_context]
|
| 222 |
+
|
| 223 |
+
# κ° μ»¨ν
μ€νΈ μ‘μΈμ€
|
| 224 |
+
limited_context = []
|
| 225 |
+
for i, doc in enumerate(context):
|
| 226 |
+
# κ° λ¬Έμλ₯Ό 1000μλ‘ μ ν
|
| 227 |
+
if len(doc) > 1000:
|
| 228 |
+
logger.warning(f"λ¬Έμ {i+1}μ κΈΈμ΄κ° μ νλμμ΅λλ€ ({len(doc)} -> 1000)")
|
| 229 |
+
doc = doc[:1000] + "...(μλ΅)"
|
| 230 |
+
limited_context.append(doc)
|
| 231 |
+
|
| 232 |
+
context_text = "\n\n".join([f"λ¬Έμ {i+1}: {doc}" for i, doc in enumerate(limited_context)])
|
| 233 |
+
|
| 234 |
+
prompt = f"""μ§λ¬Έ: {query}
|
| 235 |
|
| 236 |
<context>
|
| 237 |
{context_text}
|
| 238 |
</context>
|
| 239 |
|
| 240 |
μ κ²μ κ²°κ³Όλ₯Ό μ°Έκ³ νμ¬ μ§λ¬Έμ λ΅λ³ν΄ μ£ΌμΈμ."""
|
| 241 |
+
|
| 242 |
+
logger.info(f"RAG ν둬ννΈ μμ± μλ£ (κΈΈμ΄: {len(prompt)})")
|
| 243 |
+
|
| 244 |
+
result = self.generate(
|
| 245 |
prompt=prompt,
|
| 246 |
system_prompt=system_prompt,
|
| 247 |
temperature=temperature,
|
| 248 |
max_tokens=max_tokens,
|
| 249 |
**kwargs
|
| 250 |
)
|
| 251 |
+
|
| 252 |
+
# κ²°κ³Όκ° μ€λ₯ λ©μμ§μΈμ§ νμΈ
|
| 253 |
+
if result.startswith("μ€λ₯") or result.startswith("API μ€λ₯") or result.startswith("μλ΅ νμ μ€λ₯"):
|
| 254 |
+
logger.error(f"RAG μμ± κ²°κ³Όκ° μ€λ₯λ₯Ό ν¬ν¨ν©λλ€: {result}")
|
| 255 |
+
# μ’ λ μ¬μ©μ μΉνμ μΈ μ€λ₯ λ©μμ§ λ°ν
|
| 256 |
+
return "μ£μ‘ν©λλ€. νμ¬ μλ΅μ μμ±νλλ° λ¬Έμ κ° λ°μνμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 257 |
+
|
| 258 |
+
return result
|
| 259 |
+
|
| 260 |
except Exception as e:
|
| 261 |
+
logger.error(f"RAG ν
μ€νΈ μμ± μ€ μμΈ λ°μ: {str(e)}")
|
| 262 |
+
logger.error(traceback.format_exc())
|
| 263 |
+
return "μ£μ‘ν©λλ€. μλ΅ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|