Spaces:
Sleeping
Sleeping
Commit ·
831dfd3
1
Parent(s): 5ef1c2e
updated aggent
Browse files
agent.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# agent.py — CPU-
|
| 2 |
import os
|
| 3 |
import fitz
|
| 4 |
import faiss
|
|
@@ -7,18 +7,27 @@ import sqlite3
|
|
| 7 |
import numpy as np
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 10 |
|
| 11 |
class CodingAgent:
|
| 12 |
def __init__(self):
|
| 13 |
-
# Load
|
| 14 |
-
model_id = "
|
| 15 |
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 16 |
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
self.llm = pipeline(
|
| 18 |
"text-generation",
|
| 19 |
model=self.model,
|
| 20 |
tokenizer=self.tokenizer,
|
| 21 |
-
max_new_tokens=
|
|
|
|
| 22 |
device=-1
|
| 23 |
)
|
| 24 |
|
|
@@ -62,7 +71,7 @@ class CodingAgent:
|
|
| 62 |
self.id_map.extend(range(len(self.docs)-len(chunks), len(self.docs)))
|
| 63 |
return f"Added {len(chunks)} chunks."
|
| 64 |
|
| 65 |
-
def retrieve_context(self, query, top_k=
|
| 66 |
if self.index.ntotal == 0:
|
| 67 |
return ""
|
| 68 |
query_emb = self.embed_chunks([query])[0]
|
|
@@ -77,9 +86,12 @@ class CodingAgent:
|
|
| 77 |
return f"[From memory] {result[0]}"
|
| 78 |
|
| 79 |
context = self.retrieve_context(query)
|
| 80 |
-
prompt = f"
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
|
|
|
| 83 |
self.conn.execute("INSERT INTO memory (query, response) VALUES (?, ?)", (query, result))
|
| 84 |
self.conn.commit()
|
| 85 |
return result
|
|
|
|
| 1 |
+
# agent.py — CPU-ready, optimized for speed with TinyLlama
|
| 2 |
import os
|
| 3 |
import fitz
|
| 4 |
import faiss
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
+
from torch.quantization import quantize_dynamic
|
| 11 |
|
| 12 |
class CodingAgent:
|
| 13 |
def __init__(self):
|
| 14 |
+
# Load TinyLlama (chat-optimized, CPU-compatible)
|
| 15 |
+
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 16 |
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 17 |
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 18 |
+
|
| 19 |
+
# Apply dynamic quantization to speed up inference on CPU
|
| 20 |
+
self.model = quantize_dynamic(
|
| 21 |
+
self.model, {torch.nn.Linear}, dtype=torch.qint8
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Text-generation pipeline, limit token output
|
| 25 |
self.llm = pipeline(
|
| 26 |
"text-generation",
|
| 27 |
model=self.model,
|
| 28 |
tokenizer=self.tokenizer,
|
| 29 |
+
max_new_tokens=128,
|
| 30 |
+
do_sample=False,
|
| 31 |
device=-1
|
| 32 |
)
|
| 33 |
|
|
|
|
| 71 |
self.id_map.extend(range(len(self.docs)-len(chunks), len(self.docs)))
|
| 72 |
return f"Added {len(chunks)} chunks."
|
| 73 |
|
| 74 |
+
def retrieve_context(self, query, top_k=1): # reduce to 1 chunk for speed
|
| 75 |
if self.index.ntotal == 0:
|
| 76 |
return ""
|
| 77 |
query_emb = self.embed_chunks([query])[0]
|
|
|
|
| 86 |
return f"[From memory] {result[0]}"
|
| 87 |
|
| 88 |
context = self.retrieve_context(query)
|
| 89 |
+
prompt = f"### User:\n{query}\n\n### Context:\n{context}\n\n### Assistant:"
|
| 90 |
+
# Generate with limited tokens
|
| 91 |
+
out = self.llm(prompt)[0]['generated_text']
|
| 92 |
+
result = out.split("### Assistant:")[-1].strip()
|
| 93 |
|
| 94 |
+
# Save to memory
|
| 95 |
self.conn.execute("INSERT INTO memory (query, response) VALUES (?, ?)", (query, result))
|
| 96 |
self.conn.commit()
|
| 97 |
return result
|