SakshamSna commited on
Commit
f972f9f
·
1 Parent(s): fd18bec

updated agent

Browse files
Files changed (1) hide show
  1. agent.py +23 -30
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
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
8
  from sentence_transformers import SentenceTransformer
9
- from torch.quantization import quantize_dynamic
10
 
11
  class CodingAgent:
12
  def __init__(self):
13
- # Load TinyLlama (chat-optimized, CPU-compatible)
14
- model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
15
- self.tokenizer = AutoTokenizer.from_pretrained(model_id)
16
- self.model = AutoModelForCausalLM.from_pretrained(model_id)
17
 
18
- # Apply dynamic quantization to speed up inference on CPU
19
- self.model = quantize_dynamic(
20
- self.model, {torch.nn.Linear}, dtype=torch.qint8
21
- )
22
 
23
- # Text-generation pipeline: reduced token output and deterministic
24
- self.llm = pipeline(
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 for session memory
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=1):
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"### User:\n{query}\n\n### Context:\n{context}\n\n### Assistant:"
 
 
 
92
  )
93
- # Generate response
94
- output = self.llm(prompt)[0]['generated_text']
95
- response = output.split("### Assistant:")[-1].strip()
96
 
97
- # Save to memory
 
 
98
  self.conn.execute(
99
  "INSERT INTO memory (query, response) VALUES (?, ?)",
100
- (query, response)
101
  )
102
  self.conn.commit()
103
- return response
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")