Spaces:
Sleeping
Sleeping
Commit ·
09c52fb
1
Parent(s): 5ea7454
added prompt compression agent
Browse files
agent.py
CHANGED
|
@@ -6,13 +6,12 @@ import numpy as np
|
|
| 6 |
import google.generativeai as genai
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 9 |
|
| 10 |
class CodingAgent:
|
| 11 |
def __init__(self):
|
| 12 |
-
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
|
| 16 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 17 |
if not self.api_key:
|
| 18 |
raise ValueError("GEMINI_API_KEY not found in environment or .env file.")
|
|
@@ -20,17 +19,17 @@ class CodingAgent:
|
|
| 20 |
genai.configure(api_key=self.api_key)
|
| 21 |
self.model = genai.GenerativeModel("gemini-1.5-flash")
|
| 22 |
|
| 23 |
-
|
| 24 |
self.embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 25 |
self.index = faiss.IndexFlatL2(384)
|
| 26 |
self.docs = []
|
| 27 |
|
| 28 |
-
|
| 29 |
self.conn = sqlite3.connect("memory.db", check_same_thread=False)
|
| 30 |
self.conn.execute(
|
| 31 |
"""CREATE TABLE IF NOT EXISTS memory (id INTEGER PRIMARY KEY, query TEXT, response TEXT)"""
|
| 32 |
)
|
| 33 |
|
|
|
|
|
|
|
| 34 |
def embed_chunks(self, texts):
|
| 35 |
return self.embedder.encode(texts, convert_to_numpy=True)
|
| 36 |
|
|
@@ -67,6 +66,13 @@ class CodingAgent:
|
|
| 67 |
D, I = self.index.search(np.array([query_emb]), top_k)
|
| 68 |
return "\n\n".join(self.docs[i] for i in I[0])
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def answer(self, query):
|
| 71 |
# Check memory first
|
| 72 |
cursor = self.conn.execute(
|
|
@@ -77,9 +83,11 @@ class CodingAgent:
|
|
| 77 |
return f"[From memory] {result[0]}"
|
| 78 |
|
| 79 |
context = self.retrieve_context(query)
|
|
|
|
|
|
|
| 80 |
prompt = (
|
| 81 |
f"You are a helpful coding assistant.\n\n"
|
| 82 |
-
f"Context (from uploaded docs):\n{
|
| 83 |
f"User question: {query}\n\n"
|
| 84 |
f"Answer with code or explanation where needed."
|
| 85 |
)
|
|
|
|
| 6 |
import google.generativeai as genai
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
+
from transformers import pipeline # added for summarization
|
| 10 |
|
| 11 |
class CodingAgent:
|
| 12 |
def __init__(self):
|
|
|
|
| 13 |
load_dotenv()
|
| 14 |
|
|
|
|
| 15 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 16 |
if not self.api_key:
|
| 17 |
raise ValueError("GEMINI_API_KEY not found in environment or .env file.")
|
|
|
|
| 19 |
genai.configure(api_key=self.api_key)
|
| 20 |
self.model = genai.GenerativeModel("gemini-1.5-flash")
|
| 21 |
|
|
|
|
| 22 |
self.embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 23 |
self.index = faiss.IndexFlatL2(384)
|
| 24 |
self.docs = []
|
| 25 |
|
|
|
|
| 26 |
self.conn = sqlite3.connect("memory.db", check_same_thread=False)
|
| 27 |
self.conn.execute(
|
| 28 |
"""CREATE TABLE IF NOT EXISTS memory (id INTEGER PRIMARY KEY, query TEXT, response TEXT)"""
|
| 29 |
)
|
| 30 |
|
| 31 |
+
self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # added
|
| 32 |
+
|
| 33 |
def embed_chunks(self, texts):
|
| 34 |
return self.embedder.encode(texts, convert_to_numpy=True)
|
| 35 |
|
|
|
|
| 66 |
D, I = self.index.search(np.array([query_emb]), top_k)
|
| 67 |
return "\n\n".join(self.docs[i] for i in I[0])
|
| 68 |
|
| 69 |
+
def compress_context(self, context, token_limit=2000):
|
| 70 |
+
"""Summarizes context if it exceeds token limit."""
|
| 71 |
+
if len(context.split()) < token_limit:
|
| 72 |
+
return context
|
| 73 |
+
summary = self.summarizer(context, max_length=200, min_length=50, do_sample=False)[0]['summary_text']
|
| 74 |
+
return summary
|
| 75 |
+
|
| 76 |
def answer(self, query):
|
| 77 |
# Check memory first
|
| 78 |
cursor = self.conn.execute(
|
|
|
|
| 83 |
return f"[From memory] {result[0]}"
|
| 84 |
|
| 85 |
context = self.retrieve_context(query)
|
| 86 |
+
compressed_context = self.compress_context(context)
|
| 87 |
+
|
| 88 |
prompt = (
|
| 89 |
f"You are a helpful coding assistant.\n\n"
|
| 90 |
+
f"Context (from uploaded docs):\n{compressed_context}\n\n"
|
| 91 |
f"User question: {query}\n\n"
|
| 92 |
f"Answer with code or explanation where needed."
|
| 93 |
)
|