ltcai 0.2.2 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/docs/CHANGELOG.md +125 -0
- package/kg_schema.py +64 -15
- package/knowledge_graph.py +299 -2
- package/knowledge_graph_api.py +10 -2
- package/latticeai/api/security_dashboard.py +580 -0
- package/latticeai/core/__init__.py +1 -1
- package/latticeai/core/context_builder.py +191 -0
- package/latticeai/core/document_generator.py +103 -0
- package/latticeai/core/graph_curator.py +417 -0
- package/latticeai/core/model_compat.py +407 -0
- package/latticeai/core/model_resolution.py +227 -0
- package/llm_router.py +147 -0
- package/package.json +1 -1
- package/server.py +324 -22
- package/static/account.html +2 -2
- package/static/admin.html +75 -1
- package/static/chat.html +2 -2
- package/static/css/tokens.css +26 -0
- package/static/graph.html +2 -2
- package/static/lattice-reference.css +372 -414
- package/static/scripts/account.js +10 -2
- package/static/scripts/admin.js +296 -0
- package/static/scripts/chat.js +82 -9
- package/static/scripts/graph.js +6 -2
- package/static/sw.js +1 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Context Builder — Knowledge Graph 기반 문서 생성용 컨텍스트 조합 모듈.
|
|
3
|
+
|
|
4
|
+
retrieve_context_for_generation() 파이프라인:
|
|
5
|
+
Step 1: Query → Hybrid Search (text + graph + recency)
|
|
6
|
+
Step 2: Seed nodes → Multi-hop traversal (Document → Project → Concept)
|
|
7
|
+
Step 3: Top-K 결과를 구조화된 Markdown Context로 변환
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import logging
|
|
11
|
+
import re
|
|
12
|
+
from typing import Any, Dict, List, Optional
|
|
13
|
+
|
|
14
|
+
_CLEAN_RE = re.compile(r"\s+")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _clean(text: str, max_len: int = 700) -> str:
|
|
18
|
+
return _CLEAN_RE.sub(" ", str(text or "")).strip()[:max_len]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def retrieve_context_for_generation(
|
|
22
|
+
kg_store,
|
|
23
|
+
query: str,
|
|
24
|
+
*,
|
|
25
|
+
max_results: int = 10,
|
|
26
|
+
max_hops: int = 2,
|
|
27
|
+
) -> Dict[str, Any]:
|
|
28
|
+
"""Knowledge Graph에서 문서 생성에 필요한 컨텍스트를 검색·조합한다.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
{
|
|
32
|
+
"query": str,
|
|
33
|
+
"context_markdown": str, # LLM 프롬프트에 직접 주입할 Markdown
|
|
34
|
+
"sources": [...], # 참조된 소스 목록
|
|
35
|
+
"stats": {...}, # 검색 통계
|
|
36
|
+
}
|
|
37
|
+
"""
|
|
38
|
+
query = str(query or "").strip()
|
|
39
|
+
if not query or not kg_store:
|
|
40
|
+
return {"query": query, "context_markdown": "", "sources": [], "stats": {}}
|
|
41
|
+
|
|
42
|
+
results = kg_store.search_for_document_generation(query, limit=max_results)
|
|
43
|
+
if not results:
|
|
44
|
+
fallback_ctx = kg_store.context_for_query(query, limit=max_results)
|
|
45
|
+
return {
|
|
46
|
+
"query": query,
|
|
47
|
+
"context_markdown": fallback_ctx,
|
|
48
|
+
"sources": [],
|
|
49
|
+
"stats": {"method": "fallback", "matches": 0},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
seed_ids = [r["id"] for r in results]
|
|
53
|
+
hop_data = kg_store.multi_hop_context(seed_ids, max_hops=max_hops)
|
|
54
|
+
|
|
55
|
+
extra_nodes_by_id = {}
|
|
56
|
+
for node in hop_data.get("nodes", []):
|
|
57
|
+
if node["id"] not in {r["id"] for r in results}:
|
|
58
|
+
extra_nodes_by_id[node["id"]] = node
|
|
59
|
+
|
|
60
|
+
sections = _build_context_sections(results, extra_nodes_by_id, hop_data.get("edges", []))
|
|
61
|
+
context_md = _render_markdown(query, sections)
|
|
62
|
+
sources = _extract_sources(results)
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
"query": query,
|
|
66
|
+
"context_markdown": context_md,
|
|
67
|
+
"sources": sources,
|
|
68
|
+
"stats": {
|
|
69
|
+
"method": "hybrid",
|
|
70
|
+
"primary_matches": len(results),
|
|
71
|
+
"graph_nodes": len(hop_data.get("nodes", [])),
|
|
72
|
+
"graph_edges": len(hop_data.get("edges", [])),
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _build_context_sections(
|
|
78
|
+
primary_results: List[Dict[str, Any]],
|
|
79
|
+
extra_nodes: Dict[str, Dict[str, Any]],
|
|
80
|
+
edges: List[Dict[str, Any]],
|
|
81
|
+
) -> List[Dict[str, Any]]:
|
|
82
|
+
sections = []
|
|
83
|
+
|
|
84
|
+
docs = [r for r in primary_results if r["type"] in (
|
|
85
|
+
"Document", "File", "SlideDeck", "Spreadsheet", "CodeFile", "Image", "ImageText",
|
|
86
|
+
)]
|
|
87
|
+
if docs:
|
|
88
|
+
sections.append({
|
|
89
|
+
"title": "관련 문서/파일",
|
|
90
|
+
"items": docs,
|
|
91
|
+
"icon": "📄",
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
decisions = [r for r in primary_results if r["type"] in ("Decision", "Task")]
|
|
95
|
+
if decisions:
|
|
96
|
+
sections.append({
|
|
97
|
+
"title": "관련 결정사항/작업",
|
|
98
|
+
"items": decisions,
|
|
99
|
+
"icon": "✅",
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
conversations = [r for r in primary_results if r["type"] == "Chat"]
|
|
103
|
+
if conversations:
|
|
104
|
+
sections.append({
|
|
105
|
+
"title": "관련 대화",
|
|
106
|
+
"items": conversations,
|
|
107
|
+
"icon": "💬",
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
concepts = [r for r in primary_results if r["type"] in ("Concept", "Feature")]
|
|
111
|
+
extra_concepts = [n for n in extra_nodes.values() if n["type"] in ("Concept", "Feature")]
|
|
112
|
+
all_concepts = concepts + extra_concepts[:8]
|
|
113
|
+
if all_concepts:
|
|
114
|
+
sections.append({
|
|
115
|
+
"title": "관련 개념/기술",
|
|
116
|
+
"items": all_concepts,
|
|
117
|
+
"icon": "🔗",
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
return sections
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _render_markdown(query: str, sections: List[Dict[str, Any]]) -> str:
|
|
124
|
+
lines = []
|
|
125
|
+
for section in sections:
|
|
126
|
+
if not section["items"]:
|
|
127
|
+
continue
|
|
128
|
+
lines.append(f"### {section['icon']} {section['title']}")
|
|
129
|
+
lines.append("")
|
|
130
|
+
for item in section["items"][:8]:
|
|
131
|
+
title = item.get("title", "")
|
|
132
|
+
summary = _clean(item.get("summary", ""))
|
|
133
|
+
item_type = item.get("type", "")
|
|
134
|
+
score_info = ""
|
|
135
|
+
if "hybrid_score" in item:
|
|
136
|
+
score_info = f" (relevance: {item['hybrid_score']:.2f})"
|
|
137
|
+
|
|
138
|
+
meta = item.get("metadata") or {}
|
|
139
|
+
source = (
|
|
140
|
+
meta.get("relative_path")
|
|
141
|
+
or meta.get("filename")
|
|
142
|
+
or meta.get("conversation_id")
|
|
143
|
+
or meta.get("source")
|
|
144
|
+
or item.get("id", "")
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
lines.append(f"- **[{item_type}] {title}**{score_info}")
|
|
148
|
+
if source and source != item.get("id", ""):
|
|
149
|
+
lines.append(f" - 출처: {source}")
|
|
150
|
+
if summary:
|
|
151
|
+
lines.append(f" - {summary}")
|
|
152
|
+
|
|
153
|
+
related = item.get("related_concepts", [])
|
|
154
|
+
if related:
|
|
155
|
+
tags = ", ".join(c["title"] for c in related[:5])
|
|
156
|
+
lines.append(f" - 관련: {tags}")
|
|
157
|
+
|
|
158
|
+
lines.append("")
|
|
159
|
+
|
|
160
|
+
return "\n".join(lines).strip()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _extract_sources(results: List[Dict[str, Any]]) -> List[Dict[str, str]]:
|
|
164
|
+
sources = []
|
|
165
|
+
seen = set()
|
|
166
|
+
for r in results:
|
|
167
|
+
meta = r.get("metadata") or {}
|
|
168
|
+
source_key = (
|
|
169
|
+
meta.get("relative_path")
|
|
170
|
+
or meta.get("filename")
|
|
171
|
+
or meta.get("conversation_id")
|
|
172
|
+
or r.get("id", "")
|
|
173
|
+
)
|
|
174
|
+
if source_key and source_key not in seen:
|
|
175
|
+
seen.add(source_key)
|
|
176
|
+
sources.append({
|
|
177
|
+
"id": r.get("id", ""),
|
|
178
|
+
"type": r.get("type", ""),
|
|
179
|
+
"title": r.get("title", ""),
|
|
180
|
+
"source": source_key,
|
|
181
|
+
})
|
|
182
|
+
return sources
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def format_sources_footnote(sources: List[Dict[str, str]]) -> str:
|
|
186
|
+
if not sources:
|
|
187
|
+
return ""
|
|
188
|
+
lines = ["\n---\n**참조된 지식 그래프 노드:**"]
|
|
189
|
+
for i, src in enumerate(sources[:10], 1):
|
|
190
|
+
lines.append(f"{i}. [{src['type']}] {src['title']} ({src['source']})")
|
|
191
|
+
return "\n".join(lines)
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Document Generator — 지식 그래프 기반 고품질 문서 자동 생성 모듈.
|
|
3
|
+
|
|
4
|
+
사용자가 "Q3 마케팅 전략 보고서 작성해줘" 같은 일반 채팅을 하면,
|
|
5
|
+
Knowledge Graph에서 관련 과거 문서/개념/관계를 자동으로 찾아
|
|
6
|
+
LLM이 자연스럽고 일관성 있게 새로운 문서를 생성한다.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from typing import Optional
|
|
11
|
+
|
|
12
|
+
_DOCUMENT_INTENT_PATTERNS = [
|
|
13
|
+
re.compile(r"(보고서|계획서|기획서|제안서|문서|리포트|요약서|분석서|전략서|매뉴얼|가이드)", re.IGNORECASE),
|
|
14
|
+
re.compile(r"(작성|만들어|생성|써|줘|write|create|generate|draft|compose|prepare)", re.IGNORECASE),
|
|
15
|
+
re.compile(r"(report|proposal|plan|document|summary|analysis|strategy|guide|manual|brief)", re.IGNORECASE),
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
_STRONG_INTENT_PATTERNS = [
|
|
19
|
+
re.compile(r"(작성해|만들어\s*줘|써\s*줘|생성해|write\s+(?:a|me|the)|create\s+(?:a|me|the)|draft\s+(?:a|me|the))", re.IGNORECASE),
|
|
20
|
+
re.compile(r"(보고서|계획서|기획서|제안서|전략서|매뉴얼).*(작성|만들|생성|써)", re.IGNORECASE),
|
|
21
|
+
re.compile(r"(작성|만들|생성|써).*(보고서|계획서|기획서|제안서|전략서|매뉴얼)", re.IGNORECASE),
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def detect_document_intent(message: str) -> bool:
|
|
26
|
+
"""Detect whether the user's message is requesting document generation."""
|
|
27
|
+
if not message or len(message) < 5:
|
|
28
|
+
return False
|
|
29
|
+
for pattern in _STRONG_INTENT_PATTERNS:
|
|
30
|
+
if pattern.search(message):
|
|
31
|
+
return True
|
|
32
|
+
hit_count = sum(1 for p in _DOCUMENT_INTENT_PATTERNS if p.search(message))
|
|
33
|
+
return hit_count >= 2
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
DOCUMENT_GENERATION_SYSTEM_PROMPT = """당신은 사용자의 개인 AI 지식 어시스턴트 Lattice AI입니다.
|
|
37
|
+
사용자의 기존 지식 기반을 활용하여 고품질 문서를 생성합니다.
|
|
38
|
+
|
|
39
|
+
## 지침
|
|
40
|
+
1. 아래 제공된 지식 그래프 컨텍스트를 최대한 활용하세요.
|
|
41
|
+
2. 이전 문서의 스타일과 톤을 유지하면서 최신적이고 전문적인 문서를 작성하세요.
|
|
42
|
+
3. 출처는 자연스럽게 본문이나 각주에 포함하세요.
|
|
43
|
+
4. 사용자의 언어(한국어/영어)에 맞춰 작성하세요.
|
|
44
|
+
5. 구조화된 포맷(제목, 소제목, 목록 등)을 사용하세요.
|
|
45
|
+
|
|
46
|
+
## 사용자의 지식 기반
|
|
47
|
+
|
|
48
|
+
{graph_context}"""
|
|
49
|
+
|
|
50
|
+
DOCUMENT_GENERATION_FOLLOWUP_PROMPT = """당신은 사용자의 개인 AI 지식 어시스턴트 Lattice AI입니다.
|
|
51
|
+
이전에 생성한 문서를 사용자의 요청에 따라 수정/보완합니다.
|
|
52
|
+
|
|
53
|
+
## 이전 생성 컨텍스트
|
|
54
|
+
|
|
55
|
+
{graph_context}
|
|
56
|
+
|
|
57
|
+
## 이전 문서
|
|
58
|
+
{previous_document}
|
|
59
|
+
|
|
60
|
+
위 문서를 사용자의 요청에 따라 수정하세요. 기존 스타일과 톤을 유지하세요."""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def build_document_system_prompt(graph_context: str) -> str:
|
|
64
|
+
if not graph_context:
|
|
65
|
+
return DOCUMENT_GENERATION_SYSTEM_PROMPT.replace("{graph_context}", "(사용 가능한 지식 기반이 없습니다. 일반 지식을 활용하여 작성합니다.)")
|
|
66
|
+
return DOCUMENT_GENERATION_SYSTEM_PROMPT.replace("{graph_context}", graph_context)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def build_followup_system_prompt(graph_context: str, previous_document: str) -> str:
|
|
70
|
+
prompt = DOCUMENT_GENERATION_FOLLOWUP_PROMPT.replace("{graph_context}", graph_context or "(없음)")
|
|
71
|
+
return prompt.replace("{previous_document}", previous_document or "(없음)")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class DocumentGenerationSession:
|
|
75
|
+
"""Maintains state across iterative document generation requests."""
|
|
76
|
+
|
|
77
|
+
def __init__(self):
|
|
78
|
+
self._last_context: Optional[str] = None
|
|
79
|
+
self._last_document: Optional[str] = None
|
|
80
|
+
self._conversation_id: Optional[str] = None
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def has_previous(self) -> bool:
|
|
84
|
+
return self._last_document is not None
|
|
85
|
+
|
|
86
|
+
def update(self, context: str, document: str, conversation_id: Optional[str] = None) -> None:
|
|
87
|
+
self._last_context = context
|
|
88
|
+
self._last_document = document
|
|
89
|
+
if conversation_id:
|
|
90
|
+
self._conversation_id = conversation_id
|
|
91
|
+
|
|
92
|
+
def get_system_prompt(self, graph_context: str) -> str:
|
|
93
|
+
if self.has_previous:
|
|
94
|
+
return build_followup_system_prompt(
|
|
95
|
+
graph_context or self._last_context or "",
|
|
96
|
+
self._last_document or "",
|
|
97
|
+
)
|
|
98
|
+
return build_document_system_prompt(graph_context)
|
|
99
|
+
|
|
100
|
+
def clear(self) -> None:
|
|
101
|
+
self._last_context = None
|
|
102
|
+
self._last_document = None
|
|
103
|
+
self._conversation_id = None
|