| """search_knowledge tool - RAG ๊ฒ์ (CRAG self-correction ํฌํจ) |
| |
| ๊ธฐ๋ณธ์ CRAG ํ์ฑํ (๊ฒ์ โ ๊ด๋ จ์ฑ ํ๊ฐ โ ๋ฏธ๋ฌ ์ ์ฟผ๋ฆฌ ์ฌ์์ฑ + ์ฌ๊ฒ์). |
| ํ๊ฒฝ๋ณ์ CRAG_ENABLED=false ๋ก ๋นํ์ฑ ๊ฐ๋ฅ (์คํ ๋น๊ต์ฉ). |
| |
| ์ ์ญ trace list (`LAST_CRAG_TRACE`)์ CRAG ๋ฉํ๊ฐ ๋์ ๋์ด agent ํธ์ถ๋ณ ๊ด์ฐฐ ๊ฐ๋ฅ. |
| """ |
| from agents.rag.crag import crag_enabled, crag_search |
| from agents.rag.store import load_document, search |
|
|
| |
| LAST_CRAG_TRACE: list[dict] = [] |
|
|
|
|
| def reset_crag_trace() -> list[dict]: |
| """์ด์ trace ํ์ ํ ์๋ก ์์ - ์คํยทagent trace ์์ง์ฉ""" |
| global LAST_CRAG_TRACE |
| out = LAST_CRAG_TRACE |
| LAST_CRAG_TRACE = [] |
| return out |
|
|
|
|
| def search_knowledge(query: str, top_k: int = 3) -> dict: |
| """์ฌ๋ด ์ง์ ๋ฌธ์๋ฅผ hybrid ๊ฒ์ + CRAG ์์ฒด ํ๊ฐ |
| |
| CRAG ํ์ฑ ์ ๋ฐํ์ relevance_score, refined ์ฌ๋ถ ํฌํจ. |
| """ |
| if crag_enabled(): |
| result = crag_search(query, top_k=top_k, trace_list=LAST_CRAG_TRACE) |
| return result |
| |
| doc_ids = search(query, top_k=top_k) |
| hits = [] |
| for d in doc_ids: |
| text = load_document(d) |
| hits.append({"doc_id": d, "snippet": text[:400] + ("..." if len(text) > 400 else "")}) |
| return {"hits": hits} |
|
|
|
|
| SCHEMA = { |
| "type": "function", |
| "function": { |
| "name": "search_knowledge", |
| "description": ( |
| "์ฌ๋ด ์ง์ ๋ฌธ์(๊ณผ๊ฑฐ ์ฌ๋ก INC, ์คํจ ๋ชจ๋ FMEA, ํ์ค ์ ์ฐจ SOP, ๊ณต์ ํ๋ฆ FLOW)๋ฅผ " |
| "hybrid ๊ฒ์ํฉ๋๋ค. CRAG self-correction์ด ํ์ฑํ๋์ด ์์ด ๊ฒ์ ๊ฒฐ๊ณผ์ ๊ด๋ จ์ฑ์ด " |
| "๋ฎ์ผ๋ฉด ์๋์ผ๋ก ์ฟผ๋ฆฌ๋ฅผ ์ฌ์์ฑํด ์ฌ๊ฒ์ํฉ๋๋ค. ๋ฐํ ๊ฐ์ relevance_score(0~1)๊ฐ ํฌํจ๋์ด " |
| "์์ธ ๋ถ์ยท๋์ ๊ถ๊ณ ์ ์ ๋ขฐ๋์ ํจ๊ป ํ์ฉํ ์ ์์ต๋๋ค." |
| ), |
| "parameters": { |
| "type": "object", |
| "properties": { |
| "query": { |
| "type": "string", |
| "description": "๊ฒ์ํ ํค์๋/์ง์๋ฌธ (์: 'CMP ์ฌ๋ฌ๋ฆฌ ์ ๋ ์ด์')", |
| }, |
| "top_k": { |
| "type": "integer", |
| "description": "๋ฐํํ ๋ฌธ์ ์ (๊ธฐ๋ณธ 3)", |
| "default": 3, |
| }, |
| }, |
| "required": ["query"], |
| }, |
| }, |
| } |
|
|