This commit is contained in:
Marek Lenczewski
2026-04-18 07:41:09 +02:00
parent 18fc50c75b
commit bf6f5456d6
63 changed files with 288 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ from core.db import SessionLocal
from apps.ai_core.ollama_client import get_llm
from apps.ai_core.tools import describe_for_prompt, get_tool, validate_args
from apps.catalog.models import Category, Product
from apps.orders.models import Order
SYSTEM_PROMPT = """You are an admin assistant for an e-commerce shop.
@@ -44,7 +45,11 @@ def _shop_state_snapshot() -> dict:
{"id": c.id, "slug": c.slug, "name_de": (c.name or {}).get("de", "")}
for c in db.query(Category).order_by(Category.id).all()
]
return {"products": products, "categories": categories}
orders = [
{"id": o.id, "status": o.status, "total": float(o.total)}
for o in db.query(Order).order_by(Order.id.desc()).limit(20).all()
]
return {"products": products, "categories": categories, "orders": orders}
finally:
db.close()
@@ -54,7 +59,7 @@ def build_plan(user_prompt: str) -> list[dict]:
state = _shop_state_snapshot()
user_msg = (
f"TOOL CATALOG (JSON):\n{json.dumps(tools, ensure_ascii=False)}\n\n"
f"SHOP STATE (current products & categories):\n{json.dumps(state, ensure_ascii=False)}\n\n"
f"SHOP STATE (current products, categories, recent orders):\n{json.dumps(state, ensure_ascii=False)}\n\n"
f"USER REQUEST:\n{user_prompt}\n\n"
"Reply with ONLY the JSON object described in the rules."
)

View File

@@ -216,10 +216,49 @@ CATEGORY_CREATE = ToolSpec(
)
# ---- orders.update_status -----------------------------------------
def _handler_order_status(args: dict, db: Session) -> dict:
from apps.orders.models import Order, OrderStatusHistory
order_id = int(args["id"])
o = db.get(Order, order_id)
if not o:
raise ValueError(f"Order {order_id} not found")
new_status = args["status"]
note = _coalesce(args.get("note"), "")
o.status = new_status
db.add(OrderStatusHistory(order_id=order_id, status=new_status, note=note))
db.commit()
event_bus.publish("order.status_changed", {"id": order_id, "status": new_status}, db=db)
return {"id": order_id, "status": new_status}
ORDER_STATUS = ToolSpec(
name="orders.update_status",
description="Change the status of an order (e.g. paid, packed, shipped, delivered, cancelled).",
args_schema={
"type": "object",
"required": ["id", "status"],
"properties": {
"id": {"type": "integer", "description": "Order ID"},
"status": {
"type": "string",
"description": "New status: paid, packed, shipped, delivered, cancelled",
},
"note": {"type": "string", "description": "Optional note"},
},
},
handler=_handler_order_status,
examples=[{"id": 1, "status": "shipped", "note": "DHL versendet"}],
)
# ---- registration -------------------------------------------------
ALL_TOOLS = [SETTINGS_UPDATE, PRODUCT_CREATE, PRODUCT_UPDATE, CATEGORY_CREATE]
ALL_TOOLS = [SETTINGS_UPDATE, PRODUCT_CREATE, PRODUCT_UPDATE, CATEGORY_CREATE, ORDER_STATUS]
def register_all() -> None:

View File

@@ -12,9 +12,9 @@ class Settings(BaseSettings):
MEILI_KEY: str = "shop-dev-master-key"
OLLAMA_URL: str = "http://localhost:11434"
OLLAMA_CHAT_MODEL: str = "llama3.1"
OLLAMA_EMBED_MODEL: str = "nomic-embed-text"
OLLAMA_EMBED_DIM: int = 768
OLLAMA_CHAT_MODEL: str = "qwen2.5:7b"
OLLAMA_EMBED_MODEL: str = "bge-m3"
OLLAMA_EMBED_DIM: int = 1024
SMTP_HOST: str = "localhost"
SMTP_PORT: int = 1025

View File

@@ -0,0 +1,28 @@
"""ai_core: change embedding dim from 768 to 1024 (bge-m3)
Revision ID: 0007_embed1024
Revises: 0006_ai_core
Create Date: 2026-04-17
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "0007_embed1024"
down_revision: str | None = "0006_ai_core"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# Drop and recreate with new dimension — truncating old embeddings is fine,
# reindex will rebuild them.
op.execute("DELETE FROM ai_documents")
op.execute("ALTER TABLE ai_documents ALTER COLUMN embedding TYPE vector(1024)")
def downgrade() -> None:
op.execute("DELETE FROM ai_documents")
op.execute("ALTER TABLE ai_documents ALTER COLUMN embedding TYPE vector(768)")