Spaces:
Sleeping
Sleeping
Commit ·
f972f9f
1
Parent(s): fd18bec
updated agent
Browse files
agent.py
CHANGED
|
@@ -1,41 +1,32 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import fitz
|
| 3 |
import faiss
|
| 4 |
-
import torch
|
| 5 |
import sqlite3
|
| 6 |
import numpy as np
|
| 7 |
-
|
|
|
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
-
from torch.quantization import quantize_dynamic
|
| 10 |
|
| 11 |
class CodingAgent:
|
| 12 |
def __init__(self):
|
| 13 |
-
# Load
|
| 14 |
-
|
| 15 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 16 |
-
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
self.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
self.
|
| 25 |
-
"text-generation",
|
| 26 |
-
model=self.model,
|
| 27 |
-
tokenizer=self.tokenizer,
|
| 28 |
-
max_new_tokens=128,
|
| 29 |
-
do_sample=False,
|
| 30 |
-
device=-1
|
| 31 |
-
)
|
| 32 |
|
| 33 |
# Embedding model + FAISS index
|
| 34 |
self.embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 35 |
self.index = faiss.IndexFlatL2(384)
|
| 36 |
self.docs = []
|
| 37 |
|
| 38 |
-
# SQLite
|
| 39 |
self.conn = sqlite3.connect("memory.db", check_same_thread=False)
|
| 40 |
self.conn.execute(
|
| 41 |
"""CREATE TABLE IF NOT EXISTS memory (id INTEGER PRIMARY KEY, query TEXT, response TEXT)"""
|
|
@@ -70,7 +61,7 @@ class CodingAgent:
|
|
| 70 |
self.docs.extend(chunks)
|
| 71 |
return f"Added {len(chunks)} chunks."
|
| 72 |
|
| 73 |
-
def retrieve_context(self, query, top_k=
|
| 74 |
if self.index.ntotal == 0:
|
| 75 |
return ""
|
| 76 |
query_emb = self.embed_chunks([query])[0]
|
|
@@ -78,7 +69,7 @@ class CodingAgent:
|
|
| 78 |
return "\n\n".join(self.docs[i] for i in I[0])
|
| 79 |
|
| 80 |
def answer(self, query):
|
| 81 |
-
# Check memory
|
| 82 |
cursor = self.conn.execute(
|
| 83 |
"SELECT response FROM memory WHERE query = ?", (query,)
|
| 84 |
)
|
|
@@ -88,19 +79,21 @@ class CodingAgent:
|
|
| 88 |
|
| 89 |
context = self.retrieve_context(query)
|
| 90 |
prompt = (
|
| 91 |
-
f"
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
-
# Generate response
|
| 94 |
-
output = self.llm(prompt)[0]['generated_text']
|
| 95 |
-
response = output.split("### Assistant:")[-1].strip()
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
self.conn.execute(
|
| 99 |
"INSERT INTO memory (query, response) VALUES (?, ?)",
|
| 100 |
-
(query,
|
| 101 |
)
|
| 102 |
self.conn.commit()
|
| 103 |
-
return
|
| 104 |
|
| 105 |
def clear_context(self):
|
| 106 |
self.conn.execute("DELETE FROM memory")
|
|
|
|
| 1 |
+
# agent.py — Gemini-based Coding Agent with PDF/.py RAG + Memory (.env support)
|
| 2 |
import os
|
| 3 |
import fitz
|
| 4 |
import faiss
|
|
|
|
| 5 |
import sqlite3
|
| 6 |
import numpy as np
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 10 |
|
| 11 |
class CodingAgent:
|
| 12 |
def __init__(self):
|
| 13 |
+
# Load .env file
|
| 14 |
+
load_dotenv()
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Setup Gemini LLM
|
| 17 |
+
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 18 |
+
if not self.api_key:
|
| 19 |
+
raise ValueError("GEMINI_API_KEY not found in environment or .env file.")
|
| 20 |
|
| 21 |
+
genai.configure(api_key=self.api_key)
|
| 22 |
+
self.model = genai.GenerativeModel("gemini-1.5-flash")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Embedding model + FAISS index
|
| 25 |
self.embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 26 |
self.index = faiss.IndexFlatL2(384)
|
| 27 |
self.docs = []
|
| 28 |
|
| 29 |
+
# SQLite memory
|
| 30 |
self.conn = sqlite3.connect("memory.db", check_same_thread=False)
|
| 31 |
self.conn.execute(
|
| 32 |
"""CREATE TABLE IF NOT EXISTS memory (id INTEGER PRIMARY KEY, query TEXT, response TEXT)"""
|
|
|
|
| 61 |
self.docs.extend(chunks)
|
| 62 |
return f"Added {len(chunks)} chunks."
|
| 63 |
|
| 64 |
+
def retrieve_context(self, query, top_k=2):
|
| 65 |
if self.index.ntotal == 0:
|
| 66 |
return ""
|
| 67 |
query_emb = self.embed_chunks([query])[0]
|
|
|
|
| 69 |
return "\n\n".join(self.docs[i] for i in I[0])
|
| 70 |
|
| 71 |
def answer(self, query):
|
| 72 |
+
# Check memory first
|
| 73 |
cursor = self.conn.execute(
|
| 74 |
"SELECT response FROM memory WHERE query = ?", (query,)
|
| 75 |
)
|
|
|
|
| 79 |
|
| 80 |
context = self.retrieve_context(query)
|
| 81 |
prompt = (
|
| 82 |
+
f"You are a helpful coding assistant.\n\n"
|
| 83 |
+
f"Context (from uploaded docs):\n{context}\n\n"
|
| 84 |
+
f"User question: {query}\n\n"
|
| 85 |
+
f"Answer with code or explanation where needed."
|
| 86 |
)
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
response = self.model.generate_content(prompt)
|
| 89 |
+
answer = response.text.strip()
|
| 90 |
+
|
| 91 |
self.conn.execute(
|
| 92 |
"INSERT INTO memory (query, response) VALUES (?, ?)",
|
| 93 |
+
(query, answer)
|
| 94 |
)
|
| 95 |
self.conn.commit()
|
| 96 |
+
return answer
|
| 97 |
|
| 98 |
def clear_context(self):
|
| 99 |
self.conn.execute("DELETE FROM memory")
|