Spaces:
Paused
Paused
Deploy Agentic RPA System v1
Browse files- Dockerfile +15 -32
- app.py +8 -0
- requirements.txt +14 -28
- src/agents/base.py +10 -2
- src/agents/coder.py +15 -58
- src/agents/manager.py +29 -82
- src/agents/researcher.py +5 -26
- src/agents/vision.py +14 -65
- src/core/agent_middleware.py +46 -0
- src/core/config.py +21 -28
- src/core/context.py +2 -37
- src/core/engine.py +4 -30
- src/core/integrations.py +6 -49
- src/core/memory.py +53 -89
- src/core/saas_api.py +10 -60
- src/server.py +44 -81
Dockerfile
CHANGED
|
@@ -1,44 +1,27 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
-
FROM nvidia/cuda:12.1.0-runtime-ubuntu22.04
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
ENV DEBIAN_FRONTEND=noninteractive
|
| 7 |
-
ENV PYTHONUNBUFFERED=1
|
| 8 |
-
ENV PORT=7860
|
| 9 |
-
|
| 10 |
-
# 1. Install System Dependencies
|
| 11 |
RUN apt-get update && apt-get install -y \
|
| 12 |
-
|
| 13 |
-
|
| 14 |
git \
|
| 15 |
-
poppler-utils ffmpeg \
|
| 16 |
-
libsm6 \
|
| 17 |
-
libxext6 \
|
| 18 |
&& rm -rf /var/lib/apt/lists/*
|
| 19 |
|
| 20 |
-
# 2. Set Working Directory
|
| 21 |
WORKDIR /app
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
ENV HOME=/home/user \
|
| 27 |
-
PATH=/home/user/.local/bin:$PATH
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
COPY
|
| 31 |
-
RUN pip install --no-cache-dir --upgrade pip && \
|
| 32 |
-
pip install --no-cache-dir -r requirements.txt
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
COPY --chown=user static/ static/
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
RUN mkdir -p src/data/my_workflows && \
|
| 41 |
-
chmod -R 777 src/data
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
CMD ["uvicorn", "
|
|
|
|
| 1 |
|
| 2 |
+
FROM python:3.10-slim
|
|
|
|
| 3 |
|
| 4 |
+
# Install System Dependencies (PDF & Media)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
+
poppler-utils \
|
| 7 |
+
ffmpeg \
|
| 8 |
git \
|
|
|
|
|
|
|
|
|
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
|
|
|
| 11 |
WORKDIR /app
|
| 12 |
|
| 13 |
+
# Install Python Deps
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Copy Code
|
| 18 |
+
COPY . .
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Create Directories for Data
|
| 21 |
+
RUN mkdir -p src/data/docs src/data/blueprints my_workflows
|
|
|
|
| 22 |
|
| 23 |
+
# Set Permissions
|
| 24 |
+
RUN chmod -R 777 src/data my_workflows
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Launch Server (HF Spaces use port 7860)
|
| 27 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Ensure src is in path
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 7 |
+
|
| 8 |
+
from src.server import app
|
requirements.txt
CHANGED
|
@@ -1,35 +1,21 @@
|
|
| 1 |
-
# --- Core AI & LLM ---
|
| 2 |
-
torch>=2.4.0
|
| 3 |
-
transformers>=4.46.0
|
| 4 |
-
accelerate>=1.0.0
|
| 5 |
-
bitsandbytes>=0.44.1
|
| 6 |
-
protobuf
|
| 7 |
-
sentencepiece
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
pillow
|
| 14 |
-
|
| 15 |
-
# --- RAG & Vector Database ---
|
| 16 |
-
chromadb
|
| 17 |
-
sentence-transformers
|
| 18 |
-
|
| 19 |
-
# --- Server & Connectivity ---
|
| 20 |
fastapi
|
| 21 |
uvicorn
|
| 22 |
python-multipart
|
| 23 |
-
pyngrok
|
| 24 |
-
nest_asyncio
|
| 25 |
-
|
| 26 |
-
# --- File Parsers ---
|
| 27 |
pypdf
|
| 28 |
-
python-docx
|
| 29 |
pdf2image
|
| 30 |
-
|
| 31 |
-
# --- Robustness & Tools ---
|
| 32 |
-
json_repair
|
| 33 |
duckduckgo-search
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
torch>=2.1.0
|
| 3 |
+
transformers
|
| 4 |
+
accelerate
|
| 5 |
+
bitsandbytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
fastapi
|
| 7 |
uvicorn
|
| 8 |
python-multipart
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
pypdf
|
|
|
|
| 10 |
pdf2image
|
| 11 |
+
python-docx
|
|
|
|
|
|
|
| 12 |
duckduckgo-search
|
| 13 |
+
sqlalchemy
|
| 14 |
+
psycopg2-binary
|
| 15 |
+
qwen_vl_utils
|
| 16 |
+
decord
|
| 17 |
+
timm
|
| 18 |
+
einops
|
| 19 |
+
pillow
|
| 20 |
+
json_repair
|
| 21 |
+
pytz
|
src/agents/base.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from abc import ABC
|
| 2 |
from src.core.engine import ModelEngine
|
| 3 |
|
|
@@ -10,7 +11,14 @@ class BaseAgent(ABC):
|
|
| 10 |
asset = self.engine.load_model(self.role)
|
| 11 |
model, tokenizer = asset['model'], asset['tokenizer']
|
| 12 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 13 |
gen_kwargs = self.engine.config.generation.copy()
|
| 14 |
gen_kwargs.update(kwargs)
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
from abc import ABC
|
| 3 |
from src.core.engine import ModelEngine
|
| 4 |
|
|
|
|
| 11 |
asset = self.engine.load_model(self.role)
|
| 12 |
model, tokenizer = asset['model'], asset['tokenizer']
|
| 13 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 14 |
+
|
| 15 |
gen_kwargs = self.engine.config.generation.copy()
|
| 16 |
gen_kwargs.update(kwargs)
|
| 17 |
+
|
| 18 |
+
outputs = model.generate(
|
| 19 |
+
**inputs,
|
| 20 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 21 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 22 |
+
**gen_kwargs
|
| 23 |
+
)
|
| 24 |
+
return tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
|
src/agents/coder.py
CHANGED
|
@@ -1,70 +1,27 @@
|
|
|
|
|
| 1 |
from src.agents.base import BaseAgent
|
| 2 |
-
from src.core.
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
class CoderAgent(BaseAgent):
|
| 7 |
def __init__(self, engine, memory):
|
| 8 |
super().__init__(engine, "coder")
|
| 9 |
-
|
| 10 |
-
self.registry = {}
|
| 11 |
-
reg_path = "src/data/schemas/make_modules.json"
|
| 12 |
-
if os.path.exists(reg_path):
|
| 13 |
-
with open(reg_path, "r", encoding="utf-8") as f:
|
| 14 |
-
self.registry = json.load(f)
|
| 15 |
-
|
| 16 |
-
def get_relevant_schemas(self, task_description):
|
| 17 |
-
"""
|
| 18 |
-
Simple RAG for Code: Finds module schemas mentioned in the task.
|
| 19 |
-
"""
|
| 20 |
-
relevant_schemas = []
|
| 21 |
-
task_lower = task_description.lower()
|
| 22 |
-
|
| 23 |
-
# Keyword mapping (Expand this list based on your registry)
|
| 24 |
-
keywords = {
|
| 25 |
-
"sheet": ["google-sheets:addRow", "google-sheets:updateRow"],
|
| 26 |
-
"mail": ["google-email:TriggerNewEmail", "google-email:CreateDraft"],
|
| 27 |
-
"webhook": ["gateway:CustomWebHook"],
|
| 28 |
-
"shopee": ["shopee:getOrder"],
|
| 29 |
-
"facebook": ["facebook:createPost"]
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
for key, modules in keywords.items():
|
| 33 |
-
if key in task_lower:
|
| 34 |
-
for mod_name in modules:
|
| 35 |
-
if mod_name in self.registry:
|
| 36 |
-
# Format nicely for the prompt
|
| 37 |
-
schema_snippet = json.dumps(self.registry[mod_name], indent=2)
|
| 38 |
-
relevant_schemas.append(f"TEMPLATE FOR {mod_name}:\n{schema_snippet}")
|
| 39 |
-
|
| 40 |
-
return "\n\n".join(relevant_schemas)
|
| 41 |
|
| 42 |
def write_code(self, task: str, plan: str, feedback: str = ""):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
<|im_start|>user
|
| 52 |
TASK: {task}
|
| 53 |
-
|
| 54 |
-
ARCHITECT PLAN:
|
| 55 |
-
{plan}
|
| 56 |
-
|
| 57 |
-
--- CHEAT SHEET (USE THESE EXACT PARAMETERS) ---
|
| 58 |
-
{schemas}
|
| 59 |
-
------------------------------------------------
|
| 60 |
-
|
| 61 |
-
INSTRUCTIONS:
|
| 62 |
-
1. Generate the valid Make.com JSON blueprint.
|
| 63 |
-
2. COPY the parameter names from the Cheat Sheet exactly.
|
| 64 |
-
3. Map variables using double curly braces e.g. {{{{1.value}}}}.
|
| 65 |
-
|
| 66 |
-
{f"FEEDBACK FROM PREVIOUS ERROR: {feedback}" if feedback else ""}
|
| 67 |
<|im_end|>
|
| 68 |
<|im_start|>assistant
|
| 69 |
'''
|
| 70 |
-
return self.generate(prompt, max_new_tokens=
|
|
|
|
| 1 |
+
|
| 2 |
from src.agents.base import BaseAgent
|
| 3 |
+
from src.core.agent_middleware import AgentMiddleware
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class CoderAgent(BaseAgent):
|
| 6 |
def __init__(self, engine, memory):
|
| 7 |
super().__init__(engine, "coder")
|
| 8 |
+
self.middleware = AgentMiddleware()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def write_code(self, task: str, plan: str, feedback: str = ""):
|
| 11 |
+
tools_def = self.middleware.get_workflow_tools()
|
| 12 |
+
db_schema = self.middleware.get_db_schema()
|
| 13 |
+
prompt = f'''<|im_start|>system
|
| 14 |
+
You are the Lead Automation Engineer. Output VALID JSON Workflow definition.
|
| 15 |
+
CONTEXT - SCHEMA:
|
| 16 |
+
{db_schema}
|
| 17 |
+
CONTEXT - PROTOCOL:
|
| 18 |
+
{tools_def}
|
| 19 |
+
RULES: Output ONLY JSON. No markdown.
|
| 20 |
+
<|im_end|>
|
| 21 |
<|im_start|>user
|
| 22 |
TASK: {task}
|
| 23 |
+
PLAN: {plan}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
<|im_end|>
|
| 25 |
<|im_start|>assistant
|
| 26 |
'''
|
| 27 |
+
return self.generate(prompt, max_new_tokens=1024, temperature=0.1)
|
src/agents/manager.py
CHANGED
|
@@ -1,109 +1,56 @@
|
|
|
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
from datetime import datetime
|
| 4 |
import pytz
|
| 5 |
from src.agents.base import BaseAgent
|
| 6 |
-
from src.core.
|
| 7 |
|
| 8 |
class ManagerAgent(BaseAgent):
|
| 9 |
-
def __init__(self, engine, memory):
|
| 10 |
super().__init__(engine, "manager")
|
| 11 |
self.memory = memory
|
| 12 |
-
self.
|
| 13 |
-
|
| 14 |
-
def set_db_context(self, context_str):
|
| 15 |
-
self.db_context = context_str
|
| 16 |
-
|
| 17 |
-
def get_real_time(self):
|
| 18 |
-
tz = pytz.timezone('Asia/Ho_Chi_Minh')
|
| 19 |
-
return datetime.now(tz).strftime("%H:%M - %A, %d/%m/%Y")
|
| 20 |
-
|
| 21 |
-
def get_dynamic_context(self):
|
| 22 |
-
return f"{Prompts.SYSTEM_CONTEXT}\n\n[DATA]\n{self.db_context}"
|
| 23 |
-
|
| 24 |
-
def _extract_json(self, text):
|
| 25 |
-
try:
|
| 26 |
-
match = re.search(r"```json\n(.*?)\n```", text, re.DOTALL)
|
| 27 |
-
if match: return json.loads(match.group(1))
|
| 28 |
-
match = re.search(r"\{.*?\}", text, re.DOTALL)
|
| 29 |
-
if match: return json.loads(match.group(0))
|
| 30 |
-
except: pass
|
| 31 |
-
return None
|
| 32 |
-
|
| 33 |
-
# --- NEW: THE CRITIC LOGIC ---
|
| 34 |
-
def self_correct(self, user_input, initial_response):
|
| 35 |
-
|
| 36 |
-
# Asks the model to review its own answer for quality and hallucinations.
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
3. Tone (Is it professional Vietnamese?)
|
| 45 |
-
|
| 46 |
-
If the response is Good, output: "PASS"
|
| 47 |
-
If Bad, output: "REWRITE: [Instructions on how to fix]"
|
| 48 |
-
<|im_end|>
|
| 49 |
-
<|im_start|>user
|
| 50 |
-
User Question: "{user_input}"
|
| 51 |
-
Store Context: "{self.db_context}"
|
| 52 |
-
Assistant Draft: "{initial_response}"
|
| 53 |
-
<|im_end|>
|
| 54 |
-
<|im_start|>assistant
|
| 55 |
-
'''
|
| 56 |
-
critique = self.generate(critic_prompt, max_new_tokens=128)
|
| 57 |
-
|
| 58 |
-
if "PASS" in critique:
|
| 59 |
-
return initial_response
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
You are the
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
<|im_end|>
|
| 67 |
<|im_start|>user
|
| 68 |
-
|
| 69 |
<|im_end|>
|
| 70 |
<|im_start|>assistant
|
| 71 |
'''
|
| 72 |
-
return self.generate(
|
| 73 |
-
|
| 74 |
-
def analyze_task(self, task: str, history_str: str = ""):
|
| 75 |
-
# (Keep your existing analysis logic here...)
|
| 76 |
-
# For brevity in this snippet, assuming the logic from Phase 22 serves well.
|
| 77 |
-
# ...
|
| 78 |
-
return {"category": "GENERAL"} # Placeholder for the snippet
|
| 79 |
|
| 80 |
def consult(self, task: str, context_data: str = "", history_str: str = "", store_context: str = ""):
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# 1. Draft
|
| 84 |
prompt = f'''<|im_start|>system
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
DATA: {context_data}
|
| 88 |
-
INSTRUCTION: Answer
|
| 89 |
<|im_end|>
|
| 90 |
<|im_start|>user
|
| 91 |
{task}
|
| 92 |
<|im_end|>
|
| 93 |
<|im_start|>assistant
|
| 94 |
'''
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
# 2. Critic Loop (The Upgrade)
|
| 98 |
-
final_response = self.self_correct(task, draft)
|
| 99 |
-
return final_response
|
| 100 |
-
|
| 101 |
-
# ... (Keep plan/review methods as they were)
|
| 102 |
-
def plan(self, task: str, history_str: str = "", store_context: str = ""):
|
| 103 |
-
return self.generate(f"<|im_start|>system\nArchitect.\n<|im_end|>\n<|im_start|>user\n{task}<|im_end|>\n<|im_start|>assistant\n", max_new_tokens=1500)
|
| 104 |
-
|
| 105 |
-
def review(self, task: str, code: str):
|
| 106 |
-
return {"status": "PASS"}
|
| 107 |
-
|
| 108 |
def write_marketing(self, task: str):
|
| 109 |
-
|
|
|
|
| 1 |
+
|
| 2 |
import json
|
| 3 |
import re
|
| 4 |
from datetime import datetime
|
| 5 |
import pytz
|
| 6 |
from src.agents.base import BaseAgent
|
| 7 |
+
from src.core.agent_middleware import AgentMiddleware
|
| 8 |
|
| 9 |
class ManagerAgent(BaseAgent):
|
| 10 |
+
def __init__(self, engine, memory, kb=None):
|
| 11 |
super().__init__(engine, "manager")
|
| 12 |
self.memory = memory
|
| 13 |
+
self.middleware = AgentMiddleware()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def analyze_task(self, task: str, history_str: str = ""):
|
| 16 |
+
task_lower = task.lower().strip()
|
| 17 |
+
if any(x in task_lower for x in ["viết bài", "quảng cáo", "content"]): return {"category": "MARKETING"}
|
| 18 |
+
if any(x in task_lower for x in ["tạo", "build", "automation", "quy trình", "workflow"]): return {"category": "TECHNICAL"}
|
| 19 |
+
if any(x in task_lower for x in ["doanh thu", "tồn kho", "bán được", "sales"]): return {"category": "DATA_INTERNAL"}
|
| 20 |
+
return {"category": "GENERAL"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def plan(self, task: str, history_str: str = "", store_context: str = ""):
|
| 23 |
+
schema = self.middleware.get_db_schema()
|
| 24 |
+
prompt = f'''<|im_start|>system
|
| 25 |
+
You are the Architect. Design a logical flow for this automation request.
|
| 26 |
+
Available Data Schema:
|
| 27 |
+
{schema}
|
| 28 |
+
OUTPUT: A step-by-step logical plan.
|
| 29 |
<|im_end|>
|
| 30 |
<|im_start|>user
|
| 31 |
+
REQUEST: {task}
|
| 32 |
<|im_end|>
|
| 33 |
<|im_start|>assistant
|
| 34 |
'''
|
| 35 |
+
return self.generate(prompt, max_new_tokens=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def consult(self, task: str, context_data: str = "", history_str: str = "", store_context: str = ""):
|
| 38 |
+
schema = self.middleware.get_db_schema()
|
|
|
|
|
|
|
| 39 |
prompt = f'''<|im_start|>system
|
| 40 |
+
You are Project A, a Retail Assistant.
|
| 41 |
+
DATABASE SCHEMA:
|
| 42 |
+
{schema}
|
| 43 |
+
HISTORY:
|
| 44 |
+
{history_str}
|
| 45 |
DATA: {context_data}
|
| 46 |
+
INSTRUCTION: Answer helpfully in Vietnamese.
|
| 47 |
<|im_end|>
|
| 48 |
<|im_start|>user
|
| 49 |
{task}
|
| 50 |
<|im_end|>
|
| 51 |
<|im_start|>assistant
|
| 52 |
'''
|
| 53 |
+
return self.generate(prompt, max_new_tokens=1024)
|
| 54 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def write_marketing(self, task: str):
|
| 56 |
+
return self.generate(f"<|im_start|>system\nCopywriter.\n<|im_end|>\n<|im_start|>user\n{task}<|im_end|>\n<|im_start|>assistant\n", max_new_tokens=1024)
|
src/agents/researcher.py
CHANGED
|
@@ -1,32 +1,11 @@
|
|
|
|
|
| 1 |
from src.agents.base import BaseAgent
|
| 2 |
-
from duckduckgo_search import DDGS
|
| 3 |
|
| 4 |
class ResearcherAgent(BaseAgent):
|
| 5 |
def __init__(self, engine):
|
| 6 |
super().__init__(engine, "researcher")
|
| 7 |
-
|
| 8 |
-
def search(self, query: str):
|
| 9 |
try:
|
| 10 |
-
with DDGS() as ddgs:
|
| 11 |
-
|
| 12 |
-
results = list(ddgs.text(query, max_results=4))
|
| 13 |
-
if not results: return "Search returned no results."
|
| 14 |
-
return str(results)
|
| 15 |
-
except Exception as e:
|
| 16 |
-
return f"Search failed: {e}"
|
| 17 |
-
|
| 18 |
-
def process(self, query: str):
|
| 19 |
-
raw_data = self.search(query)
|
| 20 |
-
|
| 21 |
-
# Qwen prompt format
|
| 22 |
-
prompt = f'''<|im_start|>system
|
| 23 |
-
You are a Research Assistant. Summarize the provided search data concisely in Vietnamese.
|
| 24 |
-
Focus on facts relevant to Retail/Business.
|
| 25 |
-
<|im_end|>
|
| 26 |
-
<|im_start|>user
|
| 27 |
-
QUERY: {query}
|
| 28 |
-
RAW DATA: {raw_data}
|
| 29 |
-
<|im_end|>
|
| 30 |
-
<|im_start|>assistant
|
| 31 |
-
'''
|
| 32 |
-
return self.generate(prompt, max_new_tokens=512)
|
|
|
|
| 1 |
+
|
| 2 |
from src.agents.base import BaseAgent
|
| 3 |
+
from duckduckgo_search import DDGS
|
| 4 |
|
| 5 |
class ResearcherAgent(BaseAgent):
|
| 6 |
def __init__(self, engine):
|
| 7 |
super().__init__(engine, "researcher")
|
| 8 |
+
def search(self, query):
|
|
|
|
| 9 |
try:
|
| 10 |
+
with DDGS() as ddgs: return str(list(ddgs.text(query, max_results=4)))
|
| 11 |
+
except: return "Search failed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/agents/vision.py
CHANGED
|
@@ -1,84 +1,33 @@
|
|
|
|
|
| 1 |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
| 2 |
from qwen_vl_utils import process_vision_info
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
-
import logging
|
| 6 |
|
| 7 |
class VisionAgent:
|
| 8 |
def __init__(self):
|
| 9 |
-
print("👁️ [Vision] Initializing Qwen2-VL-2B
|
| 10 |
self.model_id = "Qwen/Qwen2-VL-2B-Instruct"
|
| 11 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
-
|
| 13 |
try:
|
| 14 |
-
# Load with bfloat16 for efficiency on L4/A100, or float16 for T4
|
| 15 |
dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
|
| 16 |
-
|
| 17 |
-
# We use device_map="auto" to fit it in remaining VRAM
|
| 18 |
self.model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 19 |
-
self.model_id,
|
| 20 |
-
torch_dtype=dtype,
|
| 21 |
-
device_map="auto"
|
| 22 |
)
|
| 23 |
-
|
| 24 |
self.processor = AutoProcessor.from_pretrained(self.model_id)
|
| 25 |
-
|
| 26 |
-
except Exception as e:
|
| 27 |
-
print(f"❌ Vision Load Failed: {e}")
|
| 28 |
-
self.model = None
|
| 29 |
-
|
| 30 |
-
def analyze_image(self, image_path, task_hint="OCR"):
|
| 31 |
-
"""
|
| 32 |
-
Analyzes an image using Qwen2-VL.
|
| 33 |
-
"""
|
| 34 |
-
if not self.model:
|
| 35 |
-
return "Vision model not loaded."
|
| 36 |
-
|
| 37 |
-
if not os.path.exists(image_path):
|
| 38 |
-
return f"Error: Image file not found at {image_path}"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
prompt_text = "Analyze this chart. What are the key trends and numbers?"
|
| 46 |
-
else:
|
| 47 |
-
prompt_text = "Read all text in this image (OCR) and describe the layout."
|
| 48 |
-
|
| 49 |
-
# Prepare Inputs
|
| 50 |
-
messages = [
|
| 51 |
-
{
|
| 52 |
-
"role": "user",
|
| 53 |
-
"content": [
|
| 54 |
-
{"type": "image", "image": image_path},
|
| 55 |
-
{"type": "text", "text": prompt_text},
|
| 56 |
-
],
|
| 57 |
-
}
|
| 58 |
-
]
|
| 59 |
|
| 60 |
-
|
| 61 |
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 62 |
image_inputs, video_inputs = process_vision_info(messages)
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
videos=video_inputs,
|
| 68 |
-
padding=True,
|
| 69 |
-
return_tensors="pt",
|
| 70 |
-
)
|
| 71 |
-
inputs = inputs.to(self.device)
|
| 72 |
-
|
| 73 |
-
# Generate
|
| 74 |
-
generated_ids = self.model.generate(**inputs, max_new_tokens=1024)
|
| 75 |
-
|
| 76 |
-
# Decode
|
| 77 |
-
generated_ids_trimmed = [
|
| 78 |
-
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 79 |
-
]
|
| 80 |
-
output_text = self.processor.batch_decode(
|
| 81 |
-
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
return f"[IMAGE ANALYSIS]\n{output_text[0]}"
|
|
|
|
| 1 |
+
|
| 2 |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
| 3 |
from qwen_vl_utils import process_vision_info
|
| 4 |
import torch
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
class VisionAgent:
|
| 8 |
def __init__(self):
|
| 9 |
+
print("👁️ [Vision] Initializing Qwen2-VL-2B...")
|
| 10 |
self.model_id = "Qwen/Qwen2-VL-2B-Instruct"
|
| 11 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 12 |
try:
|
|
|
|
| 13 |
dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
|
|
|
|
|
|
|
| 14 |
self.model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 15 |
+
self.model_id, torch_dtype=dtype, device_map="auto"
|
|
|
|
|
|
|
| 16 |
)
|
|
|
|
| 17 |
self.processor = AutoProcessor.from_pretrained(self.model_id)
|
| 18 |
+
except: self.model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def analyze_media(self, file_path, task_hint="describe"):
|
| 21 |
+
if not self.model: return "Vision model not loaded."
|
| 22 |
+
media_content = {"type": "image", "image": file_path}
|
| 23 |
+
prompt_text = "Describe this image in detail."
|
| 24 |
+
if "ocr" in task_hint.lower(): prompt_text = "Read all text visible."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
messages = [{"role": "user", "content": [media_content, {"type": "text", "text": prompt_text}]}]
|
| 27 |
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 28 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 29 |
+
inputs = self.processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt").to(self.device)
|
| 30 |
|
| 31 |
+
gen_ids = self.model.generate(**inputs, max_new_tokens=1024)
|
| 32 |
+
gen_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, gen_ids)]
|
| 33 |
+
return self.processor.batch_decode(gen_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/core/agent_middleware.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from sqlalchemy import create_engine, text
|
| 3 |
+
from src.core.config import Config
|
| 4 |
+
|
| 5 |
+
class AgentMiddleware:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.config = Config()
|
| 8 |
+
try:
|
| 9 |
+
self.engine = create_engine(self.config.DB_URL)
|
| 10 |
+
except:
|
| 11 |
+
self.engine = None
|
| 12 |
+
|
| 13 |
+
def get_db_schema(self):
|
| 14 |
+
if not self.engine: return "Database not connected."
|
| 15 |
+
schema_text = []
|
| 16 |
+
try:
|
| 17 |
+
with self.engine.connect() as conn:
|
| 18 |
+
if 'postgres' in self.config.DB_URL:
|
| 19 |
+
sql = text("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
|
| 20 |
+
else:
|
| 21 |
+
sql = text("SELECT name FROM sqlite_master WHERE type='table'")
|
| 22 |
+
|
| 23 |
+
tables = conn.execute(sql).fetchall()
|
| 24 |
+
for t in tables:
|
| 25 |
+
table_name = t[0]
|
| 26 |
+
if table_name in ['sqlite_sequence', 'alembic_version']: continue
|
| 27 |
+
try:
|
| 28 |
+
cols = conn.execute(text(f"SELECT * FROM {table_name} LIMIT 0")).keys()
|
| 29 |
+
schema_text.append(f"- TABLE {table_name.upper()}: {list(cols)}")
|
| 30 |
+
except: pass
|
| 31 |
+
return "\n".join(schema_text)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error fetching schema: {e}"
|
| 34 |
+
|
| 35 |
+
def get_workflow_tools(self):
|
| 36 |
+
return """
|
| 37 |
+
[AVAILABLE WORKFLOW NODES]
|
| 38 |
+
1. 'google_sheet_read' { "sheetId": "...", "range": "A1:Z" }
|
| 39 |
+
2. 'google_sheet_write' { "sheetId": "...", "data": "{{parent.output}}", "mode": "append" }
|
| 40 |
+
3. 'gmail_send' { "to": "...", "subject": "...", "body": "..." }
|
| 41 |
+
4. 'filter' { "condition": "contains", "field": "status", "value": "active" }
|
| 42 |
+
5. 'database_query' { "query": "SELECT * FROM sales WHERE amount > 1000" }
|
| 43 |
+
|
| 44 |
+
[OUTPUT FORMAT]
|
| 45 |
+
JSON with 'nodes' and 'edges'.
|
| 46 |
+
"""
|
src/core/config.py
CHANGED
|
@@ -1,46 +1,39 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import os
|
| 3 |
|
| 4 |
class Config:
|
| 5 |
def __init__(self):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
self.DOCS_DIR = os.path.join(self.DATA_DIR, 'docs')
|
| 20 |
-
|
| 21 |
-
# Ensure directories exist
|
| 22 |
os.makedirs(self.DATA_DIR, exist_ok=True)
|
| 23 |
-
os.makedirs(self.DOCS_DIR, exist_ok=True)
|
| 24 |
|
| 25 |
-
self.SYSTEM_CONTEXT = "You are Project A, a Retail
|
| 26 |
|
| 27 |
-
# Model
|
| 28 |
self.model_id = "sonthaiha/project-a-14b"
|
| 29 |
-
|
| 30 |
-
self.models = {
|
| 31 |
-
"manager": self.model_id,
|
| 32 |
-
"coder": self.model_id,
|
| 33 |
-
"researcher": self.model_id
|
| 34 |
-
}
|
| 35 |
|
| 36 |
self.quantization = {
|
| 37 |
"load_in_4bit": True,
|
| 38 |
"bnb_4bit_compute_dtype": torch.float16,
|
| 39 |
-
"bnb_4bit_quant_type": "nf4"
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
self.generation = {
|
| 43 |
-
"max_new_tokens":
|
| 44 |
-
"temperature": 0.
|
| 45 |
"do_sample": True
|
| 46 |
-
}
|
|
|
|
| 1 |
+
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
|
| 5 |
class Config:
|
| 6 |
def __init__(self):
|
| 7 |
+
self.PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
self.DATA_DIR = os.path.join(self.PROJECT_ROOT, 'src', 'data')
|
| 9 |
+
|
| 10 |
+
# Cloud DB Connection
|
| 11 |
+
raw_url = os.getenv("DATABASE_URL", "")
|
| 12 |
+
if raw_url.startswith("postgres://"):
|
| 13 |
+
self.DB_URL = raw_url.replace("postgres://", "postgresql+psycopg2://")
|
| 14 |
+
elif raw_url.startswith("postgresql://"):
|
| 15 |
+
self.DB_URL = raw_url.replace("postgresql://", "postgresql+psycopg2://")
|
| 16 |
+
else:
|
| 17 |
+
self.DB_URL = "sqlite:///:memory:" # Fallback if secret not set
|
| 18 |
+
|
|
|
|
| 19 |
self.DOCS_DIR = os.path.join(self.DATA_DIR, 'docs')
|
|
|
|
|
|
|
| 20 |
os.makedirs(self.DATA_DIR, exist_ok=True)
|
|
|
|
| 21 |
|
| 22 |
+
self.SYSTEM_CONTEXT = "You are Project A, a Retail Automation Architect."
|
| 23 |
|
| 24 |
+
# Model Config
|
| 25 |
self.model_id = "sonthaiha/project-a-14b"
|
| 26 |
+
self.models = { "manager": self.model_id, "coder": self.model_id, "researcher": self.model_id }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
self.quantization = {
|
| 29 |
"load_in_4bit": True,
|
| 30 |
"bnb_4bit_compute_dtype": torch.float16,
|
| 31 |
+
"bnb_4bit_quant_type": "nf4",
|
| 32 |
+
"bnb_4bit_use_double_quant": True,
|
| 33 |
}
|
| 34 |
|
| 35 |
self.generation = {
|
| 36 |
+
"max_new_tokens": 2048,
|
| 37 |
+
"temperature": 0.1,
|
| 38 |
"do_sample": True
|
| 39 |
+
}
|
src/core/context.py
CHANGED
|
@@ -1,38 +1,3 @@
|
|
| 1 |
-
class ContextResolver:
|
| 2 |
-
def __init__(self, memory):
|
| 3 |
-
self.memory = memory
|
| 4 |
-
self.active_store = None
|
| 5 |
-
|
| 6 |
-
def resolve_login(self, user_id):
|
| 7 |
-
"""
|
| 8 |
-
Called when user logs in.
|
| 9 |
-
Returns:
|
| 10 |
-
- ('READY', context_string): If 1 store found.
|
| 11 |
-
- ('AMBIGUOUS', store_list): If multiple stores found.
|
| 12 |
-
- ('EMPTY', None): If no stores found.
|
| 13 |
-
"""
|
| 14 |
-
stores = self.memory.get_user_stores(user_id)
|
| 15 |
-
|
| 16 |
-
if not stores:
|
| 17 |
-
return "EMPTY", None
|
| 18 |
-
|
| 19 |
-
if len(stores) == 1:
|
| 20 |
-
self.active_store = stores[0]
|
| 21 |
-
context = self._build_context_string(stores[0])
|
| 22 |
-
return "READY", context
|
| 23 |
-
|
| 24 |
-
# If multiple stores, we need the user to pick one
|
| 25 |
-
return "AMBIGUOUS", stores
|
| 26 |
-
|
| 27 |
-
def set_active_store(self, store):
|
| 28 |
-
self.active_store = store
|
| 29 |
-
return self._build_context_string(store)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
ACTIVE STORE CONTEXT (FROM DATABASE):
|
| 34 |
-
- Store Name: {store['name']}
|
| 35 |
-
- Industry: {store['industry']}
|
| 36 |
-
- Location: {store['location']}
|
| 37 |
-
- ID: {store['id']}
|
| 38 |
-
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
class ContextResolver:
|
| 3 |
+
def __init__(self, memory): self.memory = memory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/core/engine.py
CHANGED
|
@@ -1,68 +1,42 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gc
|
| 3 |
-
import logging
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 5 |
from src.core.config import Config
|
| 6 |
|
| 7 |
-
logger = logging.getLogger("System")
|
| 8 |
-
|
| 9 |
class ModelEngine:
|
| 10 |
def __init__(self):
|
| 11 |
self.config = Config()
|
| 12 |
self.loaded_models = {}
|
| 13 |
-
# Clear VRAM before loading to prevent fragmentation
|
| 14 |
if torch.cuda.is_available():
|
| 15 |
torch.cuda.empty_cache()
|
| 16 |
gc.collect()
|
| 17 |
-
|
| 18 |
self._load_all_models()
|
| 19 |
|
| 20 |
def _load_all_models(self):
|
| 21 |
print("⚡ [Engine] Initializing Unified Architecture...")
|
| 22 |
-
|
| 23 |
-
# 1. GROUP ROLES BY MODEL NAME
|
| 24 |
-
# This ensures we only load 'Qwen-14B' ONCE, even if used by 3 agents.
|
| 25 |
unique_models = {}
|
| 26 |
for role, model_name in self.config.models.items():
|
| 27 |
-
if model_name not in unique_models:
|
| 28 |
-
unique_models[model_name] = []
|
| 29 |
unique_models[model_name].append(role)
|
| 30 |
|
| 31 |
-
# 2. LOAD EACH UNIQUE MODEL ONCE
|
| 32 |
for model_name, roles in unique_models.items():
|
| 33 |
-
role_list = ", ".join(roles).upper()
|
| 34 |
print(f" -> Loading Shared Model: {model_name}")
|
| 35 |
-
print(f" (Assigned to: {role_list})...")
|
| 36 |
-
|
| 37 |
try:
|
| 38 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 39 |
tokenizer.padding_side = "left"
|
| 40 |
if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token
|
| 41 |
|
| 42 |
-
# 4-bit Quantization is MANDATORY for 14B on L4 GPU
|
| 43 |
model = AutoModelForCausalLM.from_pretrained(
|
| 44 |
model_name,
|
| 45 |
quantization_config=BitsAndBytesConfig(**self.config.quantization),
|
| 46 |
device_map="auto",
|
| 47 |
trust_remote_code=True
|
| 48 |
)
|
| 49 |
-
|
| 50 |
-
# Shared Asset
|
| 51 |
asset = {"model": model, "tokenizer": tokenizer}
|
| 52 |
-
|
| 53 |
-
# Assign to all roles
|
| 54 |
-
for role in roles:
|
| 55 |
-
self.loaded_models[role] = asset
|
| 56 |
-
|
| 57 |
except Exception as e:
|
| 58 |
print(f"❌ Failed to load {model_name}: {e}")
|
| 59 |
-
raise e
|
| 60 |
-
|
| 61 |
-
if torch.cuda.is_available():
|
| 62 |
-
free, total = torch.cuda.mem_get_info()
|
| 63 |
-
print(f"✅ VRAM Status: {(total-free)/1e9:.2f}GB / {total/1e9:.2f}GB Used.")
|
| 64 |
|
| 65 |
def load_model(self, role: str):
|
| 66 |
-
|
| 67 |
-
raise ValueError(f"Role {role} not loaded! Available: {list(self.loaded_models.keys())}")
|
| 68 |
-
return self.loaded_models[role]
|
|
|
|
| 1 |
+
|
| 2 |
import torch
|
| 3 |
import gc
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 5 |
from src.core.config import Config
|
| 6 |
|
|
|
|
|
|
|
| 7 |
class ModelEngine:
|
| 8 |
def __init__(self):
|
| 9 |
self.config = Config()
|
| 10 |
self.loaded_models = {}
|
|
|
|
| 11 |
if torch.cuda.is_available():
|
| 12 |
torch.cuda.empty_cache()
|
| 13 |
gc.collect()
|
|
|
|
| 14 |
self._load_all_models()
|
| 15 |
|
| 16 |
def _load_all_models(self):
|
| 17 |
print("⚡ [Engine] Initializing Unified Architecture...")
|
|
|
|
|
|
|
|
|
|
| 18 |
unique_models = {}
|
| 19 |
for role, model_name in self.config.models.items():
|
| 20 |
+
if model_name not in unique_models: unique_models[model_name] = []
|
|
|
|
| 21 |
unique_models[model_name].append(role)
|
| 22 |
|
|
|
|
| 23 |
for model_name, roles in unique_models.items():
|
|
|
|
| 24 |
print(f" -> Loading Shared Model: {model_name}")
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 27 |
tokenizer.padding_side = "left"
|
| 28 |
if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token
|
| 29 |
|
|
|
|
| 30 |
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
model_name,
|
| 32 |
quantization_config=BitsAndBytesConfig(**self.config.quantization),
|
| 33 |
device_map="auto",
|
| 34 |
trust_remote_code=True
|
| 35 |
)
|
|
|
|
|
|
|
| 36 |
asset = {"model": model, "tokenizer": tokenizer}
|
| 37 |
+
for role in roles: self.loaded_models[role] = asset
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
print(f"❌ Failed to load {model_name}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def load_model(self, role: str):
|
| 42 |
+
return self.loaded_models[role]
|
|
|
|
|
|
src/core/integrations.py
CHANGED
|
@@ -1,60 +1,17 @@
|
|
|
|
|
| 1 |
import json
|
| 2 |
-
import time
|
| 3 |
-
import os
|
| 4 |
-
import re
|
| 5 |
from json_repair import repair_json
|
| 6 |
|
| 7 |
class IntegrationManager:
|
| 8 |
def __init__(self, memory_manager):
|
| 9 |
self.memory = memory_manager
|
| 10 |
-
|
| 11 |
-
# FIX: Determine absolute path to writable directory
|
| 12 |
-
# src/core/integrations.py -> src/core/ -> src/ -> src/data/my_workflows
|
| 13 |
-
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 14 |
-
self.save_dir = os.path.join(base_path, "data", "my_workflows")
|
| 15 |
-
|
| 16 |
-
# Ensure it exists (Should be writable now)
|
| 17 |
-
os.makedirs(self.save_dir, exist_ok=True)
|
| 18 |
-
|
| 19 |
-
def _sanitize_filename(self, name):
|
| 20 |
-
return re.sub(r'[^a-zA-Z0-9_-]', '_', name)
|
| 21 |
|
| 22 |
def deploy_internal(self, store_id, blueprint_json, name="New Automation"):
|
| 23 |
-
print(f" [Internal] Saving workflow '{name}'...")
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
if isinstance(blueprint_json, str):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
print(" [Warning] Malformed JSON. Repairing...")
|
| 31 |
-
payload = repair_json(blueprint_json, return_objects=True)
|
| 32 |
-
else:
|
| 33 |
-
payload = blueprint_json
|
| 34 |
-
|
| 35 |
-
if not payload: raise ValueError("Empty JSON")
|
| 36 |
-
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return {"status": "error", "message": f"Invalid JSON format: {e}"}
|
| 39 |
|
| 40 |
-
# 1. SAVE TO DB
|
| 41 |
wf_id = self.memory.save_workflow(store_id, name, payload)
|
| 42 |
-
|
| 43 |
-
# 2. SAVE TO FILE
|
| 44 |
-
safe_name = self._sanitize_filename(name)
|
| 45 |
-
filename = os.path.join(self.save_dir, f"WF_{wf_id}_{safe_name}.json")
|
| 46 |
-
|
| 47 |
-
with open(filename, "w", encoding="utf-8") as f:
|
| 48 |
-
json.dump(payload, f, indent=4, ensure_ascii=False)
|
| 49 |
-
|
| 50 |
-
# Return RELATIVE path for UI display if needed, or absolute
|
| 51 |
-
return {
|
| 52 |
-
"status": "success",
|
| 53 |
-
"workflow_id": wf_id,
|
| 54 |
-
"file_path": filename,
|
| 55 |
-
"message": "Workflow saved (Auto-Repaired)."
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
def post_to_social(self, platform, content):
|
| 59 |
-
time.sleep(1)
|
| 60 |
-
return {"status": "published", "link": "http://fb.com/post/123"}
|
|
|
|
| 1 |
+
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
| 3 |
from json_repair import repair_json
|
| 4 |
|
| 5 |
class IntegrationManager:
|
| 6 |
def __init__(self, memory_manager):
|
| 7 |
self.memory = memory_manager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def deploy_internal(self, store_id, blueprint_json, name="New Automation"):
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
if isinstance(blueprint_json, str): payload = repair_json(blueprint_json, return_objects=True)
|
| 12 |
+
else: payload = blueprint_json
|
| 13 |
+
if 'nodes' not in payload: payload = {"nodes": payload, "edges": []}
|
| 14 |
+
except: return {"status": "error"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
wf_id = self.memory.save_workflow(store_id, name, payload)
|
| 17 |
+
return {"status": "success", "workflow_id": wf_id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/core/memory.py
CHANGED
|
@@ -1,102 +1,66 @@
|
|
| 1 |
-
|
| 2 |
import json
|
| 3 |
-
import
|
| 4 |
-
from datetime import datetime
|
| 5 |
from src.core.config import Config
|
|
|
|
| 6 |
|
| 7 |
class MemoryManager:
|
| 8 |
def __init__(self):
|
| 9 |
self.config = Config()
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def _init_db(self):
|
| 16 |
-
cursor = self.conn.cursor()
|
| 17 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS history
|
| 18 |
-
(id INTEGER PRIMARY KEY, role TEXT, content TEXT, timestamp TEXT)''')
|
| 19 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS users
|
| 20 |
-
(id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
|
| 21 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS stores
|
| 22 |
-
(id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT,
|
| 23 |
-
industry TEXT, location TEXT, platform_version TEXT)''')
|
| 24 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS sales
|
| 25 |
-
(id INTEGER PRIMARY KEY, store_id INTEGER, date TEXT, amount REAL, category TEXT)''')
|
| 26 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS profile
|
| 27 |
-
(key TEXT PRIMARY KEY, value TEXT)''')
|
| 28 |
-
|
| 29 |
-
# --- NEW: INTERNAL WORKFLOW STORAGE ---
|
| 30 |
-
# This simulates your Platform's Backend Database
|
| 31 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS workflows
|
| 32 |
-
(id INTEGER PRIMARY KEY,
|
| 33 |
-
store_id INTEGER,
|
| 34 |
-
name TEXT,
|
| 35 |
-
status TEXT,
|
| 36 |
-
json_structure TEXT,
|
| 37 |
-
created_at TEXT)''')
|
| 38 |
-
self.conn.commit()
|
| 39 |
-
|
| 40 |
-
def _seed_saas_data(self):
|
| 41 |
-
cursor = self.conn.cursor()
|
| 42 |
-
cursor.execute("SELECT count(*) FROM users")
|
| 43 |
-
if cursor.fetchone()[0] == 0:
|
| 44 |
-
cursor.execute("INSERT INTO users (id, name, email) VALUES (1, 'Nguyen Van A', 'user@example.com')")
|
| 45 |
-
cursor.execute('''INSERT INTO stores (user_id, name, industry, location, platform_version)
|
| 46 |
-
VALUES (1, 'BabyWorld Cầu Giấy', 'Mom & Baby', 'Hanoi - Cau Giay', 'Pro_v2')''')
|
| 47 |
-
cursor.execute('''INSERT INTO stores (user_id, name, industry, location, platform_version)
|
| 48 |
-
VALUES (1, 'Cafe Sáng', 'F&B', 'Da Nang', 'Lite_v1')''')
|
| 49 |
-
# Seed Sales
|
| 50 |
-
today = datetime.now().strftime("%Y-%m-%d")
|
| 51 |
-
cursor.execute("INSERT INTO sales (store_id, date, amount, category) VALUES (1, ?, 2500000, 'Diapers')", (today,))
|
| 52 |
-
self.conn.commit()
|
| 53 |
|
| 54 |
-
def
|
| 55 |
-
"""Saves the AI-generated design to your platform's DB."""
|
| 56 |
-
cursor = self.conn.cursor()
|
| 57 |
-
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
-
cursor.execute("INSERT INTO workflows (store_id, name, status, json_structure, created_at) VALUES (?, ?, ?, ?, ?)",
|
| 59 |
-
(store_id, name, 'draft', json.dumps(json_data), now))
|
| 60 |
-
self.conn.commit()
|
| 61 |
-
return cursor.lastrowid
|
| 62 |
|
| 63 |
-
def
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
res = cursor.fetchone()[0]
|
| 74 |
-
return f"{res:,.0f} VND" if res else "0 VND"
|
| 75 |
-
return "No Data"
|
| 76 |
|
| 77 |
-
def
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
def
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
def
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
def
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
for role, content in history:
|
| 100 |
-
role_name = "User" if role == "user" else "Assistant"
|
| 101 |
-
formatted.append(f"{role_name}: {content}")
|
| 102 |
-
return "\n".join(formatted)
|
|
|
|
| 1 |
+
|
| 2 |
import json
|
| 3 |
+
from sqlalchemy import create_engine, text
|
|
|
|
| 4 |
from src.core.config import Config
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
class MemoryManager:
|
| 8 |
def __init__(self):
|
| 9 |
self.config = Config()
|
| 10 |
+
try:
|
| 11 |
+
self.engine = create_engine(self.config.DB_URL)
|
| 12 |
+
except:
|
| 13 |
+
self.engine = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def get_conn(self): return self.engine.connect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def get_user_workspaces(self, user_id):
|
| 18 |
+
if not self.engine: return [{"id": 1, "name": "Offline Store"}]
|
| 19 |
+
try:
|
| 20 |
+
with self.get_conn() as conn:
|
| 21 |
+
rows = conn.execute(text("SELECT id, name, type FROM workspaces WHERE user_id = :uid"), {"uid": str(user_id)}).fetchall()
|
| 22 |
+
if not rows: return [{"id": 1, "name": "Default Store"}]
|
| 23 |
+
return [{"id": r[0], "name": r[1]} for r in rows]
|
| 24 |
+
except: return [{"id": 1, "name": "Default Store"}]
|
| 25 |
|
| 26 |
+
def _get_or_create_session(self, conn, user_id, workspace_id):
|
| 27 |
+
row = conn.execute(text("SELECT id FROM chat_sessions WHERE user_id = :uid ORDER BY last_active DESC LIMIT 1"), {"uid": str(user_id)}).fetchone()
|
| 28 |
+
if row: return row[0]
|
| 29 |
+
res = conn.execute(text("INSERT INTO chat_sessions (user_id, workspace_id, title) VALUES (:uid, :wid, 'New Chat') RETURNING id"), {"uid": str(user_id), "wid": str(workspace_id)}).fetchone()
|
| 30 |
+
return res[0]
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def save_attachment(self, user_id, workspace_id, filename, filetype, analysis):
|
| 33 |
+
try:
|
| 34 |
+
with self.get_conn() as conn:
|
| 35 |
+
sid = self._get_or_create_session(conn, user_id, workspace_id)
|
| 36 |
+
conn.execute(text("INSERT INTO chat_attachments (session_id, file_name, file_type, analysis_summary) VALUES (:sid, :f, :t, :a)"), {"sid": sid, "f": filename, "t": filetype, "a": analysis})
|
| 37 |
+
conn.commit()
|
| 38 |
+
except: pass
|
| 39 |
|
| 40 |
+
def add_message(self, user_id, workspace_id, role, content):
|
| 41 |
+
try:
|
| 42 |
+
with self.get_conn() as conn:
|
| 43 |
+
sid = self._get_or_create_session(conn, user_id, workspace_id)
|
| 44 |
+
conn.execute(text("INSERT INTO chat_messages (session_id, role, content) VALUES (:sid, :role, :content)"), {"sid": sid, "role": role, "content": str(content)})
|
| 45 |
+
conn.commit()
|
| 46 |
+
except: pass
|
| 47 |
|
| 48 |
+
def get_context_string(self, user_id, limit=6):
|
| 49 |
+
try:
|
| 50 |
+
with self.get_conn() as conn:
|
| 51 |
+
rows = conn.execute(text("SELECT m.role, m.content FROM chat_messages m JOIN chat_sessions s ON m.session_id = s.id WHERE s.user_id = :uid ORDER BY m.created_at DESC LIMIT :lim"), {"uid": str(user_id), "lim": limit}).fetchall()
|
| 52 |
+
history = "\n".join([f"{r[0]}: {r[1]}" for r in reversed(rows)])
|
| 53 |
+
|
| 54 |
+
att_rows = conn.execute(text("SELECT a.file_name, a.analysis_summary FROM chat_attachments a JOIN chat_sessions s ON a.session_id = s.id WHERE s.user_id = :uid ORDER BY s.last_active DESC LIMIT 3"), {"uid": str(user_id)}).fetchall()
|
| 55 |
+
vision = ""
|
| 56 |
+
if att_rows:
|
| 57 |
+
vision = "\n[VISUAL CONTEXT]:\n" + "\n".join([f"- {r[0]}: {r[1]}" for r in att_rows])
|
| 58 |
+
return vision + history
|
| 59 |
+
except: return ""
|
| 60 |
|
| 61 |
+
def save_workflow(self, workspace_id, name, json_data):
|
| 62 |
+
with self.get_conn() as conn:
|
| 63 |
+
conn.execute(text("INSERT INTO scenarios (workspace_id, name, description, steps, status, created_at) VALUES (:wid, :name, 'AI Generated', :steps, 'active', :time)"),
|
| 64 |
+
{"wid": workspace_id, "name": name, "steps": json.dumps(json_data), "time": datetime.now().isoformat()})
|
| 65 |
+
conn.commit()
|
| 66 |
+
return 1
|
|
|
|
|
|
|
|
|
|
|
|
src/core/saas_api.py
CHANGED
|
@@ -1,65 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import os
|
| 4 |
from src.core.config import Config
|
| 5 |
|
| 6 |
class SaasAPI:
|
| 7 |
def __init__(self):
|
| 8 |
self.config = Config()
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def _get_mock_conn(self):
|
| 19 |
-
return sqlite3.connect(self.config.DB_PATH, check_same_thread=False)
|
| 20 |
-
|
| 21 |
-
def get_sales_report(self, store_id, period="today"):
|
| 22 |
-
"""Fetches sales data."""
|
| 23 |
-
|
| 24 |
-
if self.USE_MOCK:
|
| 25 |
-
# --- MOCK LOGIC (Local DB) ---
|
| 26 |
-
conn = self._get_mock_conn()
|
| 27 |
-
cursor = conn.cursor()
|
| 28 |
-
date_str = "date('now', 'localtime')"
|
| 29 |
-
cursor.execute(f"SELECT SUM(amount), COUNT(*) FROM sales WHERE store_id = ? AND date = {date_str}", (store_id,))
|
| 30 |
-
res = cursor.fetchone()
|
| 31 |
-
conn.close()
|
| 32 |
-
if res and res[0]:
|
| 33 |
-
return {"revenue": res[0], "orders": res[1], "period": period}
|
| 34 |
-
return {"revenue": 0, "orders": 0, "period": period}
|
| 35 |
-
|
| 36 |
-
else:
|
| 37 |
-
# --- REAL PRODUCTION LOGIC (HTTP) ---
|
| 38 |
-
try:
|
| 39 |
-
headers = {"Authorization": f"Bearer {self.API_KEY}"}
|
| 40 |
-
url = f"{self.API_BASE_URL}/stores/{store_id}/reports/sales"
|
| 41 |
-
response = requests.get(url, params={"period": period}, headers=headers, timeout=5)
|
| 42 |
-
|
| 43 |
-
if response.status_code == 200:
|
| 44 |
-
return response.json() # Expects { "revenue": 100, "orders": 5 }
|
| 45 |
-
else:
|
| 46 |
-
return {"error": f"API Error: {response.status_code}"}
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return {"error": f"Connection Failed: {e}"}
|
| 49 |
-
|
| 50 |
-
def check_inventory(self, product_name):
|
| 51 |
-
if self.USE_MOCK:
|
| 52 |
-
# Mock Data
|
| 53 |
-
if "bỉm" in product_name.lower(): return {"stock": 45, "name": "Bỉm Bobby"}
|
| 54 |
-
return {"error": "Not found"}
|
| 55 |
-
else:
|
| 56 |
-
# Real API Call
|
| 57 |
-
# requests.get(...)
|
| 58 |
-
pass
|
| 59 |
-
|
| 60 |
-
def get_customer_info(self, query):
|
| 61 |
-
if self.USE_MOCK:
|
| 62 |
-
return {"name": "Test Customer", "rank": "VIP"}
|
| 63 |
-
else:
|
| 64 |
-
# Real API Call
|
| 65 |
-
pass
|
|
|
|
| 1 |
+
|
| 2 |
+
from sqlalchemy import create_engine, text
|
|
|
|
| 3 |
from src.core.config import Config
|
| 4 |
|
| 5 |
class SaasAPI:
|
| 6 |
def __init__(self):
|
| 7 |
self.config = Config()
|
| 8 |
+
try: self.engine = create_engine(self.config.DB_URL)
|
| 9 |
+
except: self.engine = None
|
| 10 |
+
|
| 11 |
+
def get_sales_report(self, workspace_id=1, period="today"):
|
| 12 |
+
if not self.engine: return {"revenue": "0", "orders": 0}
|
| 13 |
+
with self.engine.connect() as conn:
|
| 14 |
+
res = conn.execute(text("SELECT SUM(amount), COUNT(*) FROM sales WHERE workspace_id = :wid"), {"wid": workspace_id}).fetchone()
|
| 15 |
+
return {"revenue": f"{res[0] or 0:,.0f} VND", "orders": res[1] or 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/server.py
CHANGED
|
@@ -1,61 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import FileResponse
|
| 3 |
import sys
|
| 4 |
import os
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import re
|
| 7 |
-
from fastapi import FastAPI
|
| 8 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from pydantic import BaseModel
|
| 10 |
-
|
| 11 |
-
# Setup Path
|
| 12 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
-
project_root = os.path.dirname(current_dir)
|
| 14 |
-
if project_root not in sys.path: sys.path.insert(0, project_root)
|
| 15 |
-
|
| 16 |
-
# Imports
|
| 17 |
from src.core.engine import ModelEngine
|
| 18 |
from src.core.memory import MemoryManager
|
| 19 |
-
from src.core.context import ContextResolver
|
| 20 |
from src.core.saas_api import SaasAPI
|
| 21 |
from src.core.integrations import IntegrationManager
|
| 22 |
from src.agents.manager import ManagerAgent
|
| 23 |
from src.agents.coder import CoderAgent
|
| 24 |
-
from src.agents.researcher import ResearcherAgent
|
| 25 |
from src.agents.vision import VisionAgent
|
| 26 |
|
| 27 |
-
# Engine Lazy Load
|
| 28 |
try:
|
| 29 |
-
if 'engine' not in globals():
|
| 30 |
-
|
| 31 |
-
except:
|
| 32 |
-
engine = None
|
| 33 |
|
| 34 |
memory = MemoryManager()
|
| 35 |
saas = SaasAPI()
|
| 36 |
integrations = IntegrationManager(memory)
|
| 37 |
-
|
| 38 |
manager = ManagerAgent(engine, memory)
|
| 39 |
coder = CoderAgent(engine, memory)
|
| 40 |
-
researcher = ResearcherAgent(engine)
|
| 41 |
vision = VisionAgent()
|
| 42 |
|
| 43 |
-
app = FastAPI(
|
| 44 |
-
|
| 45 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 46 |
-
|
| 47 |
-
@app.get("/")
|
| 48 |
-
async def read_index():
|
| 49 |
-
return FileResponse('static/index.html')
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
app.add_middleware(
|
| 53 |
-
CORSMiddleware,
|
| 54 |
-
allow_origins=["*"],
|
| 55 |
-
allow_credentials=True,
|
| 56 |
-
allow_methods=["*"],
|
| 57 |
-
allow_headers=["*"],
|
| 58 |
-
)
|
| 59 |
|
| 60 |
class ChatRequest(BaseModel):
|
| 61 |
user_id: int
|
|
@@ -66,56 +39,46 @@ def clean_output(text):
|
|
| 66 |
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
|
| 67 |
return text.replace("</think>", "").replace("<think>", "").strip()
|
| 68 |
|
| 69 |
-
@app.post("/
|
| 70 |
-
async def
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
if
|
| 79 |
-
store_context = f"Store: {active_store['name']}, Industry: {active_store['industry']}"
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
memory.
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
meta = {}
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
code = coder.write_code(req.message, plan)
|
| 98 |
-
|
| 99 |
match = re.search(r"```json\n(.*?)\n```", code, re.DOTALL)
|
| 100 |
if match:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
elif category == "DATA_INTERNAL":
|
| 107 |
-
action = "data"
|
| 108 |
-
val = saas.get_sales_report(req.store_id, "today")
|
| 109 |
-
context_data = f"SALES DATA: {val}"
|
| 110 |
-
# PASS CONTEXT HERE
|
| 111 |
-
response_text = manager.consult(req.message, context_data, history_str, store_context)
|
| 112 |
-
|
| 113 |
else:
|
| 114 |
-
|
| 115 |
-
# PASS CONTEXT HERE
|
| 116 |
-
response_text = manager.consult(req.message, "", history_str, store_context)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
memory.add_message("assistant",
|
| 120 |
-
|
| 121 |
-
return {"response": clean_res, "action": action, "meta": meta}
|
|
|
|
| 1 |
+
|
|
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
import uuid
|
| 8 |
+
import shutil
|
| 9 |
import re
|
| 10 |
+
from fastapi import FastAPI, UploadFile, File
|
|
|
|
| 11 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from src.core.engine import ModelEngine
|
| 13 |
from src.core.memory import MemoryManager
|
|
|
|
| 14 |
from src.core.saas_api import SaasAPI
|
| 15 |
from src.core.integrations import IntegrationManager
|
| 16 |
from src.agents.manager import ManagerAgent
|
| 17 |
from src.agents.coder import CoderAgent
|
|
|
|
| 18 |
from src.agents.vision import VisionAgent
|
| 19 |
|
|
|
|
| 20 |
try:
|
| 21 |
+
if 'engine' not in globals(): engine = ModelEngine()
|
| 22 |
+
except: engine = None
|
|
|
|
|
|
|
| 23 |
|
| 24 |
memory = MemoryManager()
|
| 25 |
saas = SaasAPI()
|
| 26 |
integrations = IntegrationManager(memory)
|
|
|
|
| 27 |
manager = ManagerAgent(engine, memory)
|
| 28 |
coder = CoderAgent(engine, memory)
|
|
|
|
| 29 |
vision = VisionAgent()
|
| 30 |
|
| 31 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
class ChatRequest(BaseModel):
|
| 34 |
user_id: int
|
|
|
|
| 39 |
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
|
| 40 |
return text.replace("</think>", "").replace("<think>", "").strip()
|
| 41 |
|
| 42 |
+
@app.post("/upload")
|
| 43 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 44 |
+
file_ext = file.filename.split(".")[-1].lower()
|
| 45 |
+
filename = f"{uuid.uuid4()}.{file_ext}"
|
| 46 |
+
save_path = f"src/data/{filename}"
|
| 47 |
+
os.makedirs("src/data", exist_ok=True)
|
| 48 |
+
with open(save_path, "wb") as buffer: shutil.copyfileobj(file.file, buffer)
|
| 49 |
|
| 50 |
+
analysis = f"File {file.filename}"
|
| 51 |
+
if file_ext in ['jpg', 'png']: analysis = vision.analyze_media(save_path)
|
|
|
|
| 52 |
|
| 53 |
+
# Save to Memory (Hardcoded User 1 for now, should come from Form)
|
| 54 |
+
memory.save_attachment(1, 1, file.filename, file_ext, analysis)
|
| 55 |
+
return {"status": "success", "vision_analysis": analysis}
|
| 56 |
+
|
| 57 |
+
@app.post("/chat")
|
| 58 |
+
async def chat_endpoint(req: ChatRequest):
|
| 59 |
+
memory.add_message(req.user_id, req.store_id, "user", req.message)
|
| 60 |
+
history = memory.get_context_string(req.user_id)
|
| 61 |
|
| 62 |
+
decision = manager.analyze_task(req.message, history)
|
| 63 |
+
cat = decision.get("category", "GENERAL")
|
|
|
|
| 64 |
|
| 65 |
+
# Vision Override
|
| 66 |
+
if "ảnh" in req.message.lower(): cat = "GENERAL"
|
| 67 |
+
|
| 68 |
+
resp = ""
|
| 69 |
+
if cat == "TECHNICAL":
|
| 70 |
+
plan = manager.plan(req.message, history)
|
| 71 |
code = coder.write_code(req.message, plan)
|
|
|
|
| 72 |
match = re.search(r"```json\n(.*?)\n```", code, re.DOTALL)
|
| 73 |
if match:
|
| 74 |
+
integrations.deploy_internal(req.store_id, match.group(1))
|
| 75 |
+
resp = f"Đã tạo quy trình:\n{code}"
|
| 76 |
+
elif cat == "DATA_INTERNAL":
|
| 77 |
+
data = saas.get_sales_report(req.store_id)
|
| 78 |
+
resp = manager.consult(req.message, str(data), history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
+
resp = manager.consult(req.message, "", history)
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
final = clean_output(resp)
|
| 83 |
+
memory.add_message(req.user_id, req.store_id, "assistant", final)
|
| 84 |
+
return {"response": final}
|
|
|