File size: 2,689 Bytes
75e0882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74e743a
75e0882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Wardrobe assistant: answer questions using catalog context.

Injects the full wardrobe catalog into the system prompt and uses
the LLM to answer questions about outfits, garment care, combinations,
and wardrobe organization.

Uses the same Gemma 3 4B model as the vision pipeline (just without
sending images). This simplifies VRAM management.
"""

import logging

from .model_loader import model_manager
from .catalog import get_catalog_summary

logger = logging.getLogger(__name__)

SYSTEM_PROMPT = """You are a personal wardrobe assistant. You help the user organize their clothes, choose outfits, and take care of their garments.

Here is the user's complete wardrobe catalog:

{catalog}

Guidelines:
- When suggesting outfits or referencing specific garments, ALWAYS use the exact bracketed ID from the catalog, e.g. [garment_001], [garment_012]. Never write bare IDs without brackets.
- Consider season, formality, and color coordination when recommending combinations.
- For care instructions, use common knowledge about fabric care (e.g., wool should be hand-washed, denim can be machine-washed cold).
- If the wardrobe is empty, suggest the user upload photos of their clothes first.
- Be concise and practical. No lengthy fashion theory.
- Respond in the same language as the user's question."""


def ask(question: str) -> str:
    """Answer a question about the user's wardrobe."""
    llm = model_manager.get_text_model()
    catalog_text = get_catalog_summary()
    system = SYSTEM_PROMPT.format(catalog=catalog_text)

    logger.info("Assistant question: %s", question[:100])

    response = llm.create_chat_completion(
        messages=[
            {"role": "system", "content": system},
            {"role": "user", "content": question},
        ],
        max_tokens=1024,
        temperature=0.7,
    )

    answer = response["choices"][0]["message"]["content"]
    logger.debug("Assistant response: %s", answer[:200])
    return answer


def ask_streaming(question: str):
    """Stream a response token by token (for Gradio chatbot)."""
    llm = model_manager.get_text_model()
    catalog_text = get_catalog_summary()
    system = SYSTEM_PROMPT.format(catalog=catalog_text)

    logger.info("Assistant question (streaming): %s", question[:100])

    stream = llm.create_chat_completion(
        messages=[
            {"role": "system", "content": system},
            {"role": "user", "content": question},
        ],
        max_tokens=1024,
        temperature=0.7,
        stream=True,
    )

    for chunk in stream:
        delta = chunk["choices"][0].get("delta", {})
        content = delta.get("content", "")
        if content:
            yield content