File size: 3,145 Bytes
f6b05db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
RAG ์‹œ์Šคํ…œ ํ…Œ์ŠคํŠธ ์Šคํฌ๋ฆฝํŠธ

API ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰ ์ค‘์ผ ๋•Œ ์‚ฌ์šฉ
"""

import requests
import json
from typing import Dict


def test_query(
    question: str,
    top_k: int = 5,
    enable_metacognition: bool = True,
    api_url: str = "http://localhost:8000"
) -> Dict:
    """
    ์งˆ๋ฌธ ํ…Œ์ŠคํŠธ

    Args:
        question: ์งˆ๋ฌธ
        top_k: ๊ฒ€์ƒ‰ํ•  ๋ฌธ์„œ ๊ฐœ์ˆ˜
        enable_metacognition: ๋ฉ”ํƒ€์ธ์ง€ ํ™œ์„ฑํ™”
        api_url: API URL

    Returns:
        ์‘๋‹ต ๋ฐ์ดํ„ฐ
    """
    print("=" * 80)
    print(f"์งˆ๋ฌธ: {question}")
    print("=" * 80)

    # ์š”์ฒญ
    response = requests.post(
        f"{api_url}/query",
        json={
            "question": question,
            "top_k": top_k,
            "enable_metacognition": enable_metacognition
        }
    )

    if response.status_code != 200:
        print(f"โŒ ์˜ค๋ฅ˜: {response.status_code}")
        print(response.text)
        return {}

    result = response.json()

    # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    print("\n๐Ÿ“ ๋‹ต๋ณ€:")
    print("-" * 80)
    print(result["answer"])
    print("-" * 80)

    print(f"\n๐Ÿ“š ์ฐธ๊ณ  ๋ฌธํ—Œ: {len(result['sources'])}๊ฐœ")
    for i, source in enumerate(result['sources'][:3], 1):
        print(f"\n[{i}] {source['source_filename']}")
        print(f"    ์œ ์‚ฌ๋„: {source['similarity']:.3f}")
        print(f"    ๋‚ด์šฉ: {source['text'][:100]}...")

    if result.get('metacognition'):
        print(f"\n๐Ÿง  ๋ฉ”ํƒ€์ธ์ง€ ์ •๋ณด:")
        print(f"    ๋ฐ˜๋ณต ํšŸ์ˆ˜: {result['metacognition']['iterations']}")
        print(f"    ์‚ฌ๊ณ  ๊ณผ์ • ๋‹จ๊ณ„: {len(result['metacognition']['thinking_history'])}")

    print("\n" + "=" * 80)
    return result


def test_health(api_url: str = "http://localhost:8000"):
    """ํ—ฌ์Šค ์ฒดํฌ"""
    print("๐Ÿฅ ํ—ฌ์Šค ์ฒดํฌ ์ค‘...")
    response = requests.get(f"{api_url}/health")

    if response.status_code == 200:
        data = response.json()
        print("โœ… ์„œ๋ฒ„ ์ •์ƒ")
        print(f"   Vector Store: {data['vector_store']['total_documents']}๊ฐœ ๋ฌธ์„œ")
        print(f"   Embedding: {data['embedding_model']['type']} ({data['embedding_model']['dimension']}์ฐจ์›)")
    else:
        print(f"โŒ ์„œ๋ฒ„ ์˜ค๋ฅ˜: {response.status_code}")


if __name__ == "__main__":
    # ํ—ฌ์Šค ์ฒดํฌ
    test_health()

    print("\n")

    # ์ƒ˜ํ”Œ ์งˆ๋ฌธ๋“ค
    questions = [
        "๊ธˆ์œต์œ„๊ธฐ์˜ ์ฃผ์š” ์›์ธ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?",
        "ํฌํŠธํด๋ฆฌ์˜ค ๋‹ค๊ฐํ™”์˜ ํšจ๊ณผ๋Š”?",
        "์ค‘์•™์€ํ–‰์˜ ๊ธˆ๋ฆฌ ์ •์ฑ…์ด ์‹œ์žฅ์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์€?",
    ]

    for question in questions:
        try:
            test_query(question, top_k=5, enable_metacognition=True)
            print("\n\n")
        except Exception as e:
            print(f"โŒ ์˜ค๋ฅ˜: {str(e)}\n\n")

    # ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์งˆ๋ฌธ
    print("\n์ปค์Šคํ…€ ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š” (Enter๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ข…๋ฃŒ):")
    while True:
        question = input("\n์งˆ๋ฌธ: ").strip()
        if not question:
            break

        try:
            test_query(question, top_k=5, enable_metacognition=True)
        except Exception as e:
            print(f"โŒ ์˜ค๋ฅ˜: {str(e)}")