Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- requirements.txt +1 -1
- src/llm.py +14 -15
requirements.txt
CHANGED
|
@@ -5,7 +5,7 @@ faiss-cpu
|
|
| 5 |
fastapi
|
| 6 |
uvicorn
|
| 7 |
python-dotenv
|
| 8 |
-
|
| 9 |
dvc
|
| 10 |
mlflow
|
| 11 |
optuna
|
|
|
|
| 5 |
fastapi
|
| 6 |
uvicorn
|
| 7 |
python-dotenv
|
| 8 |
+
openai
|
| 9 |
dvc
|
| 10 |
mlflow
|
| 11 |
optuna
|
src/llm.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
"""
|
| 2 |
-
LLM module. Single
|
| 3 |
|
| 4 |
-
WHY
|
| 5 |
WHY temperature=0.1? Lower = more deterministic, less hallucination.
|
| 6 |
WHY one call per query? Multi-step chains add latency and failure points.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
| 10 |
import logging
|
| 11 |
-
from
|
| 12 |
-
import httpx
|
| 13 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
|
@@ -17,31 +16,31 @@ load_dotenv()
|
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
-
api_key = os.getenv("
|
| 21 |
-
logger.info(f"
|
| 22 |
|
| 23 |
-
_client =
|
| 24 |
api_key=api_key,
|
| 25 |
-
|
| 26 |
)
|
| 27 |
-
logger.info("
|
| 28 |
|
| 29 |
|
| 30 |
def call_llm_raw(messages: list) -> str:
|
| 31 |
"""
|
| 32 |
-
Call
|
| 33 |
Used by V2 agent for Pass 1 and Pass 3.
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
response = _client.chat.completions.create(
|
| 37 |
-
model="
|
| 38 |
messages=messages,
|
| 39 |
temperature=0.3,
|
| 40 |
max_tokens=1500
|
| 41 |
)
|
| 42 |
return response.choices[0].message.content
|
| 43 |
except Exception as e:
|
| 44 |
-
logger.error(f"
|
| 45 |
raise
|
| 46 |
|
| 47 |
|
|
@@ -51,7 +50,7 @@ def call_llm_raw(messages: list) -> str:
|
|
| 51 |
)
|
| 52 |
def call_llm(query: str, context: str) -> str:
|
| 53 |
"""
|
| 54 |
-
Call
|
| 55 |
Retries 3 times with exponential backoff.
|
| 56 |
"""
|
| 57 |
try:
|
|
@@ -64,7 +63,7 @@ Answer based only on the excerpts above. Cite judgment IDs.
|
|
| 64 |
Use proper markdown formatting."""
|
| 65 |
|
| 66 |
response = _client.chat.completions.create(
|
| 67 |
-
model="
|
| 68 |
messages=[
|
| 69 |
{"role": "system", "content": "You are NyayaSetu, an Indian legal research assistant. Answer only from provided excerpts. Cite judgment IDs. End with: NOTE: This is not legal advice."},
|
| 70 |
{"role": "user", "content": user_message}
|
|
@@ -75,5 +74,5 @@ Use proper markdown formatting."""
|
|
| 75 |
|
| 76 |
return response.choices[0].message.content
|
| 77 |
except Exception as e:
|
| 78 |
-
logger.error(f"
|
| 79 |
raise
|
|
|
|
| 1 |
"""
|
| 2 |
+
LLM module. Single DeepSeek API call with tenacity retry.
|
| 3 |
|
| 4 |
+
WHY DeepSeek? Free tier, cost-effective inference.
|
| 5 |
WHY temperature=0.1? Lower = more deterministic, less hallucination.
|
| 6 |
WHY one call per query? Multi-step chains add latency and failure points.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
| 10 |
import logging
|
| 11 |
+
from openai import OpenAI
|
|
|
|
| 12 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 13 |
from dotenv import load_dotenv
|
| 14 |
|
|
|
|
| 16 |
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
+
api_key = os.getenv("DEEPSEEK_API_KEY")
|
| 20 |
+
logger.info(f"DEEPSEEK_API_KEY loaded: {bool(api_key)} (length: {len(api_key) if api_key else 0})")
|
| 21 |
|
| 22 |
+
_client = OpenAI(
|
| 23 |
api_key=api_key,
|
| 24 |
+
base_url="https://api.deepseek.com/v1"
|
| 25 |
)
|
| 26 |
+
logger.info("DeepSeek client initialized successfully")
|
| 27 |
|
| 28 |
|
| 29 |
def call_llm_raw(messages: list) -> str:
|
| 30 |
"""
|
| 31 |
+
Call DeepSeek with pre-built messages list.
|
| 32 |
Used by V2 agent for Pass 1 and Pass 3.
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
response = _client.chat.completions.create(
|
| 36 |
+
model="deepseek-chat",
|
| 37 |
messages=messages,
|
| 38 |
temperature=0.3,
|
| 39 |
max_tokens=1500
|
| 40 |
)
|
| 41 |
return response.choices[0].message.content
|
| 42 |
except Exception as e:
|
| 43 |
+
logger.error(f"DeepSeek API error in call_llm_raw: {type(e).__name__}: {str(e)}", exc_info=True)
|
| 44 |
raise
|
| 45 |
|
| 46 |
|
|
|
|
| 50 |
)
|
| 51 |
def call_llm(query: str, context: str) -> str:
|
| 52 |
"""
|
| 53 |
+
Call DeepSeek. Used by V1 agent.
|
| 54 |
Retries 3 times with exponential backoff.
|
| 55 |
"""
|
| 56 |
try:
|
|
|
|
| 63 |
Use proper markdown formatting."""
|
| 64 |
|
| 65 |
response = _client.chat.completions.create(
|
| 66 |
+
model="deepseek-chat",
|
| 67 |
messages=[
|
| 68 |
{"role": "system", "content": "You are NyayaSetu, an Indian legal research assistant. Answer only from provided excerpts. Cite judgment IDs. End with: NOTE: This is not legal advice."},
|
| 69 |
{"role": "user", "content": user_message}
|
|
|
|
| 74 |
|
| 75 |
return response.choices[0].message.content
|
| 76 |
except Exception as e:
|
| 77 |
+
logger.error(f"DeepSeek API error in call_llm: {type(e).__name__}: {str(e)}", exc_info=True)
|
| 78 |
raise
|