Spaces:
Sleeping
Sleeping
Commit ·
4d5bbcc
1
Parent(s): 1425e02
updated aggent
Browse files
agent.py
CHANGED
|
@@ -3,17 +3,23 @@ import fitz
|
|
| 3 |
import faiss
|
| 4 |
import torch
|
| 5 |
import sqlite3
|
|
|
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
|
| 9 |
class CodingAgent:
|
| 10 |
def __init__(self):
|
| 11 |
-
#
|
| 12 |
-
model_id = "
|
| 13 |
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 14 |
-
self.model = AutoModelForCausalLM.from_pretrained(model_id
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Embedding model + FAISS index
|
| 19 |
self.embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
|
@@ -28,7 +34,6 @@ class CodingAgent:
|
|
| 28 |
def embed_chunks(self, texts):
|
| 29 |
return self.embedder.encode(texts, convert_to_numpy=True)
|
| 30 |
|
| 31 |
-
|
| 32 |
def ingest_file(self, filepath):
|
| 33 |
chunks = []
|
| 34 |
if filepath.endswith(".pdf"):
|
|
@@ -51,7 +56,7 @@ class CodingAgent:
|
|
| 51 |
return "Unsupported file format."
|
| 52 |
|
| 53 |
embeddings = self.embed_chunks(chunks)
|
| 54 |
-
self.index.add(embeddings)
|
| 55 |
self.docs.extend(chunks)
|
| 56 |
self.id_map.extend(range(len(self.docs)-len(chunks), len(self.docs)))
|
| 57 |
return f"Added {len(chunks)} chunks."
|
|
@@ -60,7 +65,7 @@ class CodingAgent:
|
|
| 60 |
if self.index.ntotal == 0:
|
| 61 |
return ""
|
| 62 |
query_emb = self.embed_chunks([query])[0]
|
| 63 |
-
D, I = self.index.search([query_emb], top_k)
|
| 64 |
return "\n\n".join([self.docs[i] for i in I[0]])
|
| 65 |
|
| 66 |
def answer(self, query):
|
|
@@ -71,7 +76,7 @@ class CodingAgent:
|
|
| 71 |
return f"[From memory] {result[0]}"
|
| 72 |
|
| 73 |
context = self.retrieve_context(query)
|
| 74 |
-
prompt = f"You are a coding assistant.
|
| 75 |
result = self.llm(prompt)[0]['generated_text'].split("Answer:")[-1].strip()
|
| 76 |
|
| 77 |
self.conn.execute("INSERT INTO memory (query, response) VALUES (?, ?)", (query, result))
|
|
|
|
| 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 |
|
| 10 |
class CodingAgent:
|
| 11 |
def __init__(self):
|
| 12 |
+
# ✅ CPU-compatible LLM
|
| 13 |
+
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 14 |
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 15 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 16 |
+
self.llm = pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model=self.model,
|
| 19 |
+
tokenizer=self.tokenizer,
|
| 20 |
+
max_new_tokens=512,
|
| 21 |
+
device=-1 # forces CPU
|
| 22 |
+
)
|
| 23 |
|
| 24 |
# Embedding model + FAISS index
|
| 25 |
self.embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
| 34 |
def embed_chunks(self, texts):
|
| 35 |
return self.embedder.encode(texts, convert_to_numpy=True)
|
| 36 |
|
|
|
|
| 37 |
def ingest_file(self, filepath):
|
| 38 |
chunks = []
|
| 39 |
if filepath.endswith(".pdf"):
|
|
|
|
| 56 |
return "Unsupported file format."
|
| 57 |
|
| 58 |
embeddings = self.embed_chunks(chunks)
|
| 59 |
+
self.index.add(np.array(embeddings))
|
| 60 |
self.docs.extend(chunks)
|
| 61 |
self.id_map.extend(range(len(self.docs)-len(chunks), len(self.docs)))
|
| 62 |
return f"Added {len(chunks)} chunks."
|
|
|
|
| 65 |
if self.index.ntotal == 0:
|
| 66 |
return ""
|
| 67 |
query_emb = self.embed_chunks([query])[0]
|
| 68 |
+
D, I = self.index.search(np.array([query_emb]), top_k)
|
| 69 |
return "\n\n".join([self.docs[i] for i in I[0]])
|
| 70 |
|
| 71 |
def answer(self, query):
|
|
|
|
| 76 |
return f"[From memory] {result[0]}"
|
| 77 |
|
| 78 |
context = self.retrieve_context(query)
|
| 79 |
+
prompt = f"You are a helpful coding assistant.\n\nContext:\n{context}\n\nQuestion: {query}\nAnswer:"
|
| 80 |
result = self.llm(prompt)[0]['generated_text'].split("Answer:")[-1].strip()
|
| 81 |
|
| 82 |
self.conn.execute("INSERT INTO memory (query, response) VALUES (?, ?)", (query, result))
|