imkrish commited on
Commit
ee48fd8
·
1 Parent(s): b445b86

feat: Update environment configuration and integrate Google Generative AI for document processing

Browse files
.env CHANGED
@@ -6,3 +6,4 @@ CHUNK_SIZE=1000
6
  CHUNK_OVERLAP=100
7
  RATE_LIMIT=20/minute
8
  EMBEDDING_MODEL=all-MiniLM-L6-v2
 
 
6
  CHUNK_OVERLAP=100
7
  RATE_LIMIT=20/minute
8
  EMBEDDING_MODEL=all-MiniLM-L6-v2
9
+ HUGGINGFACEHUB_API_TOKEN=your_hf_token_here
.env.example CHANGED
@@ -1,5 +1,4 @@
1
- HUGGINGFACEHUB_API_TOKEN=your_hf_token_here
2
- HF_MODEL=mistralai/Mistral-7B-Instruct-v0.2
3
  CHUNK_SIZE=800
4
  CHUNK_OVERLAP=120
5
  TOP_K_DEFAULT=4
 
1
+ GOOGLE_API_KEY=your_gemini_api_key_here
 
2
  CHUNK_SIZE=800
3
  CHUNK_OVERLAP=120
4
  TOP_K_DEFAULT=4
app/__pycache__/config.cpython-39.pyc CHANGED
Binary files a/app/__pycache__/config.cpython-39.pyc and b/app/__pycache__/config.cpython-39.pyc differ
 
app/__pycache__/main.cpython-39.pyc CHANGED
Binary files a/app/__pycache__/main.cpython-39.pyc and b/app/__pycache__/main.cpython-39.pyc differ
 
app/__pycache__/rag.cpython-39.pyc CHANGED
Binary files a/app/__pycache__/rag.cpython-39.pyc and b/app/__pycache__/rag.cpython-39.pyc differ
 
app/config.py CHANGED
@@ -13,5 +13,4 @@ CHUNK_SIZE = int(os.getenv("CHUNK_SIZE", "800"))
13
  CHUNK_OVERLAP = int(os.getenv("CHUNK_OVERLAP", "120"))
14
  TOP_K_DEFAULT = int(os.getenv("TOP_K_DEFAULT", "4"))
15
 
16
- HF_MODEL = os.getenv("HF_MODEL", "mistralai/Mistral-7B-Instruct-v0.2")
17
- HF_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "")
 
13
  CHUNK_OVERLAP = int(os.getenv("CHUNK_OVERLAP", "120"))
14
  TOP_K_DEFAULT = int(os.getenv("TOP_K_DEFAULT", "4"))
15
 
16
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
 
app/rag.py CHANGED
@@ -7,7 +7,7 @@ from typing import List, Dict
7
 
8
  import faiss
9
  import numpy as np
10
- import requests
11
  from sentence_transformers import SentenceTransformer
12
  from PyPDF2 import PdfReader
13
 
@@ -18,8 +18,7 @@ from .config import (
18
  DATA_DIR,
19
  CHUNK_SIZE,
20
  CHUNK_OVERLAP,
21
- HF_MODEL,
22
- HF_API_TOKEN,
23
  )
24
 
25
  _index_lock = Lock()
@@ -166,9 +165,9 @@ def generate_answer(question: str, contexts: List[Dict]) -> str:
166
  context_text = "\n\n".join(
167
  f"[Source: {c['source']}] {c['chunk']}" for c in contexts
168
  )
169
- if not HF_API_TOKEN:
170
  return (
171
- "No HF token configured. Here are the most relevant excerpts:\n\n"
172
  + context_text
173
  )
174
 
@@ -178,28 +177,16 @@ def generate_answer(question: str, contexts: List[Dict]) -> str:
178
  f"Context:\n{context_text}\n\nQuestion: {question}\nAnswer:"
179
  )
180
 
181
- headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
182
- payload = {
183
- "inputs": prompt,
184
- "parameters": {
185
- "max_new_tokens": 256,
186
- "temperature": 0.2,
187
- "return_full_text": False,
188
- },
189
- }
190
- url = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
191
- response = requests.post(url, headers=headers, json=payload, timeout=60)
192
- if response.status_code != 200:
193
  return (
194
- "LLM generation failed. Here are the most relevant excerpts:\n\n"
195
  + context_text
196
  )
197
- data = response.json()
198
- if isinstance(data, list) and data:
199
- return data[0].get("generated_text", "").strip() or context_text
200
- if isinstance(data, dict) and "generated_text" in data:
201
- return data["generated_text"].strip() or context_text
202
- return context_text
203
 
204
 
205
  def ingest_document(path: Path, doc_id: str, source: str) -> Dict:
 
7
 
8
  import faiss
9
  import numpy as np
10
+ import google.generativeai as genai
11
  from sentence_transformers import SentenceTransformer
12
  from PyPDF2 import PdfReader
13
 
 
18
  DATA_DIR,
19
  CHUNK_SIZE,
20
  CHUNK_OVERLAP,
21
+ GOOGLE_API_KEY,
 
22
  )
23
 
24
  _index_lock = Lock()
 
165
  context_text = "\n\n".join(
166
  f"[Source: {c['source']}] {c['chunk']}" for c in contexts
167
  )
168
+ if not GOOGLE_API_KEY:
169
  return (
170
+ "No Gemini API key configured. Here are the most relevant excerpts:\n\n"
171
  + context_text
172
  )
173
 
 
177
  f"Context:\n{context_text}\n\nQuestion: {question}\nAnswer:"
178
  )
179
 
180
+ try:
181
+ genai.configure(api_key=GOOGLE_API_KEY)
182
+ model = genai.GenerativeModel("gemini-1.5-flash")
183
+ response = model.generate_content(prompt)
184
+ return response.text.strip() or context_text
185
+ except Exception as e:
 
 
 
 
 
 
186
  return (
187
+ f"LLM generation failed: {str(e)}. Here are the most relevant excerpts:\n\n"
188
  + context_text
189
  )
 
 
 
 
 
 
190
 
191
 
192
  def ingest_document(path: Path, doc_id: str, source: str) -> Dict:
requirements.txt CHANGED
@@ -5,6 +5,6 @@ pydantic==2.8.2
5
  sentence-transformers==3.0.1
6
  faiss-cpu==1.8.0.post1
7
  PyPDF2==3.0.1
8
- requests==2.32.3
9
  streamlit==1.37.1
10
  python-multipart==0.0.9
 
5
  sentence-transformers==3.0.1
6
  faiss-cpu==1.8.0.post1
7
  PyPDF2==3.0.1
8
+ google-generativeai>=0.3.0
9
  streamlit==1.37.1
10
  python-multipart==0.0.9