Spaces:
Running
Running
Upload llm.py
Browse files- src/apps/utils/llm.py +128 -152
src/apps/utils/llm.py
CHANGED
|
@@ -1,152 +1,128 @@
|
|
| 1 |
-
import openai
|
| 2 |
-
import os
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
|
| 5 |
-
# Explicitly load .env from the src/apps directory
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
-
|
| 27 |
-
-
|
| 28 |
-
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
-
|
| 33 |
-
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
-
|
| 38 |
-
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
-
|
| 43 |
-
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
-
|
| 48 |
-
-
|
| 49 |
-
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
-
|
| 54 |
-
-
|
| 55 |
-
-
|
| 56 |
-
-
|
| 57 |
-
|
| 58 |
-
**
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
**
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# Sequentially try fallbacks
|
| 131 |
-
for fallback_model in fallback_models:
|
| 132 |
-
try:
|
| 133 |
-
print(f"Attempting fallback to {fallback_model}...")
|
| 134 |
-
completion = client.chat.completions.create(
|
| 135 |
-
model=fallback_model,
|
| 136 |
-
messages=messages,
|
| 137 |
-
temperature=0,
|
| 138 |
-
stream=True,
|
| 139 |
-
max_tokens=1024
|
| 140 |
-
)
|
| 141 |
-
return completion
|
| 142 |
-
except Exception as fallback_error:
|
| 143 |
-
print(f"Fallback to {fallback_model} failed: {fallback_error}")
|
| 144 |
-
continue
|
| 145 |
-
|
| 146 |
-
# If all fail, re-raise the original error or a final one
|
| 147 |
-
print("All LLM attempts failed.")
|
| 148 |
-
raise e
|
| 149 |
-
|
| 150 |
-
def nemotron_llama_raw(query, context, chat_history, role="General"):
|
| 151 |
-
# This is a legacy alias if needed by other modules
|
| 152 |
-
return nemotron_llama(query, context, chat_history, role)
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
# Explicitly load .env from the src/apps directory
|
| 6 |
+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 7 |
+
env_path = os.path.join(BASE_DIR, '.env')
|
| 8 |
+
load_dotenv(env_path)
|
| 9 |
+
|
| 10 |
+
def nemotron_llama(query, context, chat_history, role="General"):
|
| 11 |
+
|
| 12 |
+
prompt_template = """You are a multi-role expert AI assistant named "Law Bot" with strict role-based reasoning.
|
| 13 |
+
Answer federal and state law questions ONLY based on the provided context.
|
| 14 |
+
|
| 15 |
+
## Role Behavior Rules:
|
| 16 |
+
Current Active Role: {role}
|
| 17 |
+
|
| 18 |
+
1. Judge Mode:
|
| 19 |
+
- Answer like an experienced judge.
|
| 20 |
+
- Focus on legality, neutrality, precedents, logic, and final judgment.
|
| 21 |
+
- Avoid emotional language and advocacy.
|
| 22 |
+
- Think professionally, critically, and decisively.
|
| 23 |
+
|
| 24 |
+
2. Advocate Mode:
|
| 25 |
+
- Answer like a skilled advocate/lawyer.
|
| 26 |
+
- Focus on arguments, strategies, loopholes, and persuasion.
|
| 27 |
+
- Slightly less neutral than Judge mode.
|
| 28 |
+
- More practical and tactical.
|
| 29 |
+
|
| 30 |
+
3. Woman Mode:
|
| 31 |
+
- Answer strictly from a woman’s perspective.
|
| 32 |
+
- Consider safety, social reality, emotional intelligence, and lived experience.
|
| 33 |
+
- Do not generalize or switch to male viewpoints.
|
| 34 |
+
|
| 35 |
+
4. Minor Mode:
|
| 36 |
+
- Use very simple language with short explanations.
|
| 37 |
+
- Focus only on what is necessary and appropriate for a minor.
|
| 38 |
+
- No complex terms, no adult framing.
|
| 39 |
+
|
| 40 |
+
5. Student Mode:
|
| 41 |
+
- Answer based on student needs.
|
| 42 |
+
- Be clear, structured, and learning-focused.
|
| 43 |
+
- Use examples, steps, and explanations helpful for studying or exams.
|
| 44 |
+
|
| 45 |
+
6. Citizen Mode:
|
| 46 |
+
- Answer as a helpful legal guide for a common citizen.
|
| 47 |
+
- Focus on practical rights, duties, and actionable steps.
|
| 48 |
+
- Explain legal jargon in simple, everyday language.
|
| 49 |
+
- Be empathetic but objective and informative.
|
| 50 |
+
|
| 51 |
+
## Mandatory Performance Requirements:
|
| 52 |
+
- Prioritize clarity over verbosity.
|
| 53 |
+
- Responses must be fast and concise.
|
| 54 |
+
- Avoid unnecessary explanations unless asked.
|
| 55 |
+
- Optimize reasoning speed and reduce delay.
|
| 56 |
+
- Cite your answer with Title and Page Number from the context at the very end of your response in this EXACT format:
|
| 57 |
+
**Title**: [Name]
|
| 58 |
+
**Page Numbers**: [Number]
|
| 59 |
+
- Cite your answer with Title and Page Number from the context at the very end of your response in this EXACT format:
|
| 60 |
+
**Title**: [Name]
|
| 61 |
+
**Page Numbers**: [Number]
|
| 62 |
+
- You are currently acting as {role}. You MUST stay in this character. Do NOT switch roles or ask for clarification.
|
| 63 |
+
|
| 64 |
+
Context: {context}
|
| 65 |
+
Chat History: {chat_history}
|
| 66 |
+
"""
|
| 67 |
+
# print(f"DEBUG: LLM Prompt Configured for Role: {role}")
|
| 68 |
+
formatted_prompt = prompt_template.format(role=role, context=context, chat_history=chat_history)
|
| 69 |
+
|
| 70 |
+
# Merge system prompt into user message to support models that reject 'system' role
|
| 71 |
+
messages = [
|
| 72 |
+
{"role": "user", "content": f"{formatted_prompt}\n\nUser Query: {query}"}
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
# Use OPENROUTER_API_KEY from .env
|
| 76 |
+
api_key = os.getenv("OPENROUTER_API_KEY", "").strip()
|
| 77 |
+
|
| 78 |
+
# Emergency cleanup for common copy-paste errors
|
| 79 |
+
if "sk-or-v1-sk-or-v1-" in api_key:
|
| 80 |
+
api_key = api_key.replace("sk-or-v1-sk-or-v1-", "sk-or-v1-")
|
| 81 |
+
|
| 82 |
+
if not api_key:
|
| 83 |
+
api_key = os.getenv("API_KEY", "").strip()
|
| 84 |
+
|
| 85 |
+
if not api_key:
|
| 86 |
+
raise ValueError(
|
| 87 |
+
"Set OPENROUTER_API_KEY in src/apps/.env (get a key at https://openrouter.ai/keys)"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# List of models to try sequentially - helps avoid 429 errors
|
| 91 |
+
models = [
|
| 92 |
+
"google/gemma-3-4b-it:free",
|
| 93 |
+
"mistralai/mistral-small-3.1-24b-instruct:free",
|
| 94 |
+
"meta-llama/llama-3.2-3b-instruct:free",
|
| 95 |
+
"qwen/qwen3-coder:free"
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
client = openai.OpenAI(
|
| 99 |
+
base_url="https://openrouter.ai/api/v1",
|
| 100 |
+
api_key=api_key,
|
| 101 |
+
default_headers={
|
| 102 |
+
"HTTP-Referer": os.getenv("APP_URL", "http://localhost:8000"), # Required for some free models
|
| 103 |
+
"X-Title": "Law Bot AI"
|
| 104 |
+
}
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Try all models in order
|
| 108 |
+
for current_model in models:
|
| 109 |
+
try:
|
| 110 |
+
print(f"Attempting model: {current_model}...")
|
| 111 |
+
completion = client.chat.completions.create(
|
| 112 |
+
model=current_model,
|
| 113 |
+
messages=messages,
|
| 114 |
+
temperature=0,
|
| 115 |
+
stream=True,
|
| 116 |
+
max_tokens=1024
|
| 117 |
+
)
|
| 118 |
+
return completion
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Error in {current_model}: {e}")
|
| 121 |
+
continue # Try next fallback model
|
| 122 |
+
|
| 123 |
+
# If all models fail, raise the last error
|
| 124 |
+
raise Exception("All LLM models are currently rate-limited or unavailable. Please try again in 1 minute.")
|
| 125 |
+
|
| 126 |
+
def nemotron_llama_raw(query, context, chat_history, role="General"):
|
| 127 |
+
# This is a legacy alias if needed by other modules
|
| 128 |
+
return nemotron_llama(query, context, chat_history, role)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|