Spaces:
Running
Running
Upload config.py
Browse files
config.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Configuration file for the Law RAG Chatbot application
|
3 |
+
"""
|
4 |
+
|
5 |
+
import os
|
6 |
+
from typing import Optional
|
7 |
+
from pathlib import Path
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
load_dotenv()
|
10 |
+
# Load environment variables from .env file if it exists
|
11 |
+
def load_dotenv():
|
12 |
+
"""Load environment variables from .env file"""
|
13 |
+
env_file = Path('.env')
|
14 |
+
if env_file.exists():
|
15 |
+
with open(env_file, 'r') as f:
|
16 |
+
for line in f:
|
17 |
+
line = line.strip()
|
18 |
+
if line and not line.startswith('#') and '=' in line:
|
19 |
+
key, value = line.split('=', 1)
|
20 |
+
os.environ[key] = value
|
21 |
+
|
22 |
+
# Load .env file
|
23 |
+
load_dotenv()
|
24 |
+
|
25 |
+
# Hugging Face Configuration
|
26 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
27 |
+
HF_DATASET_NAME = "ymoslem/Law-StackExchange"
|
28 |
+
|
29 |
+
# Groq Configuration
|
30 |
+
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
31 |
+
GROQ_MODEL = "llama3-8b-8192" # or "mixtral-8x7b-32768"
|
32 |
+
|
33 |
+
# Embedding Configuration
|
34 |
+
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
35 |
+
EMBEDDING_DIMENSION = 384
|
36 |
+
|
37 |
+
# ChromaDB Configuration
|
38 |
+
CHROMA_PERSIST_DIR = "./chroma_db"
|
39 |
+
CHROMA_COLLECTION_NAME = "law_stackexchange"
|
40 |
+
|
41 |
+
# FastAPI Configuration
|
42 |
+
API_TITLE = "Law RAG Chatbot API"
|
43 |
+
API_VERSION = "1.0.0"
|
44 |
+
API_DESCRIPTION = "RAG-based legal assistance chatbot using Law-StackExchange data"
|
45 |
+
HOST = "0.0.0.0"
|
46 |
+
PORT = 8000
|
47 |
+
|
48 |
+
# RAG Configuration
|
49 |
+
CHUNK_SIZE = 1000
|
50 |
+
CHUNK_OVERLAP = 200
|
51 |
+
TOP_K_RETRIEVAL = 8 # Increased from 5
|
52 |
+
MAX_TOKENS = 4096
|
53 |
+
TEMPERATURE = 0.1
|
54 |
+
DEFAULT_CONTEXT_LENGTH = 5 # New default context length
|
55 |
+
|
56 |
+
# Token Management Configuration
|
57 |
+
MAX_CONTEXT_TOKENS = 4000 # Maximum tokens for context (reserve space for prompt)
|
58 |
+
MAX_PROMPT_TOKENS = 6000 # Maximum total prompt tokens (Groq limit)
|
59 |
+
MAX_SOURCES = 5 # Maximum number of sources to include
|
60 |
+
MAX_SEARCH_VARIATIONS = 2 # Maximum search variations to try
|
61 |
+
MAX_LEGAL_CONCEPTS = 2 # Maximum legal concepts to extract
|
62 |
+
|
63 |
+
# Dataset Configuration
|
64 |
+
DATASET_SPLIT = "train"
|
65 |
+
CACHE_DIR = ".cache"
|
66 |
+
|
67 |
+
# Error Messages
|
68 |
+
ERROR_MESSAGES = {
|
69 |
+
"no_hf_token": "Hugging Face token not found. Set HF_TOKEN environment variable.",
|
70 |
+
"no_groq_key": "Groq API key not found. Set GROQ_API_KEY environment variable.",
|
71 |
+
"auth_failed": "Authentication failed: {}",
|
72 |
+
"dataset_load_failed": "Failed to load dataset: {}",
|
73 |
+
"embedding_failed": "Failed to create embeddings: {}",
|
74 |
+
"vector_db_failed": "Failed to setup vector database: {}",
|
75 |
+
"llm_failed": "Failed to initialize LLM: {}"
|
76 |
+
}
|
77 |
+
|
78 |
+
# API Response Models
|
79 |
+
class ChatRequest:
|
80 |
+
question: str
|
81 |
+
context_length: Optional[int] = 3
|
82 |
+
|
83 |
+
class ChatResponse:
|
84 |
+
answer: str
|
85 |
+
sources: list
|
86 |
+
confidence: float
|
87 |
+
processing_time: float
|