File size: 7,468 Bytes
4b97c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
TokioAI Engine -- CLI Entry Point

Usage:
  python -m engine serve              # Start OpenAI-compatible API server
  python -m engine test               # Run test suite
  python -m engine bench              # Benchmark pipeline
  python -m engine chat               # Interactive chat (for testing)
  python -m engine stats              # Show memory/pipeline stats
"""

import sys
import json
import time
import argparse

from .pipeline import Pipeline, EngineConfig


def cmd_serve(args):
    """Start the API server."""
    from .server import create_app
    from .pipeline import EngineConfig

    config = EngineConfig(
        ollama_host=args.ollama_host,
        model_name=args.model,
        max_retries=args.max_retries,
        use_guided_gen=not args.no_guided,
        use_memory=not args.no_memory,
        verbose=args.verbose,
    )

    print(f"""
 _____     _   _       _    ___
|_   _|__ | | _(_) ___ / \\  |_ _|
  | |/ _ \\| |/ / |/ _ / _ \\  | |
  | | (_) |   <| | (_/ ___ \\ | |
  |_|\\___/|_|\\_\\_|\\___/_/   \\_\\___|
   ___           _
  | __| _ _  __ _(_) _ _  ___
  | _| | ' \\/ _` | || ' \\/ -_)
  |___||_||_\\__, |_||_||_\\___|
            |___/            v3.0.0

  Model:    {config.model_name}
  Ollama:   {config.ollama_host}
  Memory:   {'ON' if config.use_memory else 'OFF'}
  Guided:   {'ON' if config.use_guided_gen else 'OFF'}
  Retries:  {config.max_retries}
  
  API: http://{args.host}:{args.port}/v1/chat/completions
  Health: http://{args.host}:{args.port}/health
  Stats: http://{args.host}:{args.port}/stats
""")

    app = create_app(config)
    app.run(host=args.host, port=args.port, debug=False)


def cmd_chat(args):
    """Interactive chat for testing the pipeline."""
    config = EngineConfig(
        ollama_host=args.ollama_host,
        model_name=args.model,
        verbose=args.verbose,
    )
    engine = Pipeline(config)

    print(f"\n[TokioAI Engine] Connected to {config.ollama_host} / {config.model_name}")
    print("[TokioAI Engine] Type 'quit' to exit, 'stats' for stats, 'reset' to clear history\n")

    while True:
        try:
            user_input = input("\033[36myou>\033[0m ")
        except (EOFError, KeyboardInterrupt):
            print("\nbye.")
            break

        if not user_input.strip():
            continue
        if user_input.strip().lower() == "quit":
            break
        if user_input.strip().lower() == "stats":
            print(json.dumps(engine.get_stats(), indent=2))
            continue
        if user_input.strip().lower() == "reset":
            engine.reset()
            print("[reset]")
            continue

        result = engine.process(user_input)

        # Display result
        print(f"\033[33m[{result.intent}|{result.confidence:.1f}|{result.attempts}att|{result.latency_ms}ms]\033[0m")

        if result.output_type == "tool_call":
            for tc in result.tool_calls:
                print(f"\033[32m>>> TOOL: {tc['name']}({json.dumps(tc['arguments'])})\033[0m")
        else:
            print(f"\033[37m{result.text}\033[0m")
        print()


def cmd_test(args):
    """Run the test suite."""
    from .test_engine import run_tests
    run_tests(verbose=args.verbose, ollama_host=args.ollama_host, model=args.model)


def cmd_bench(args):
    """Benchmark the pipeline."""
    config = EngineConfig(
        ollama_host=args.ollama_host,
        model_name=args.model,
        verbose=False,
    )
    engine = Pipeline(config)

    test_prompts = [
        ("check disk space", "tool_call"),
        ("what is docker", "text"),
        ("read /etc/hostname", "tool_call"),
        ("hi", "text"),
        ("find all python files with TODO", "tool_call"),
        ("explain SQL injection", "text"),
        ("is docker running", "tool_call"),
        ("list processes by memory usage", "tool_call"),
        ("what is the TCP 3-way handshake", "text"),
        ("scan ports on localhost", "tool_call"),
    ]

    print(f"\n[BENCH] Running {len(test_prompts)} prompts against {config.model_name}...\n")
    
    results = []
    for prompt, expected in test_prompts:
        start = time.time()
        result = engine.process(prompt)
        elapsed = int((time.time() - start) * 1000)

        correct = (
            (expected == "tool_call" and result.output_type == "tool_call") or
            (expected == "text" and result.output_type == "text")
        )

        status = "\033[32mPASS\033[0m" if correct else "\033[31mFAIL\033[0m"
        detail = ""
        if result.output_type == "tool_call" and result.tool_calls:
            detail = f"-> {result.tool_calls[0]['name']}"
        elif result.output_type == "text":
            detail = f"-> text ({len(result.text)} chars)"

        print(f"  {status} [{elapsed:4d}ms] [{result.intent:9s}] {prompt:40s} {detail}")
        results.append(correct)
        engine.reset()  # Reset between tests

    passed = sum(results)
    total = len(results)
    print(f"\n[BENCH] {passed}/{total} correct ({100*passed//total}%)")
    print(f"[BENCH] Stats: {json.dumps(engine.get_stats(), indent=2)}")


def cmd_stats(args):
    """Show memory and pipeline stats."""
    from .memory import EngineMemory
    mem = EngineMemory()
    print(json.dumps(mem.get_stats(), indent=2))


def main():
    parser = argparse.ArgumentParser(description="TokioAI Engine")
    parser.add_argument("--ollama-host", default=None, help="Ollama API URL")
    parser.add_argument("--model", default="tokioai-coder", help="Model name")
    parser.add_argument("-v", "--verbose", action="store_true")
    
    subparsers = parser.add_subparsers(dest="command")
    
    # serve
    serve_parser = subparsers.add_parser("serve", help="Start API server")
    serve_parser.add_argument("--host", default="0.0.0.0", help="Bind address")
    serve_parser.add_argument("--port", type=int, default=8080, help="Port")
    serve_parser.add_argument("--ollama-host", default=None, help="Ollama API URL")
    serve_parser.add_argument("--model", default="tokioai-coder", help="Model name")
    serve_parser.add_argument("--no-memory", action="store_true", help="Disable memory")
    serve_parser.add_argument("--no-guided", action="store_true", help="Disable guided gen")
    serve_parser.add_argument("--max-retries", type=int, default=2, help="Max retries")
    serve_parser.add_argument("-v", "--verbose", action="store_true")
    
    # chat
    chat_parser = subparsers.add_parser("chat", help="Interactive chat")
    
    # test
    test_parser = subparsers.add_parser("test", help="Run tests")
    
    # bench
    bench_parser = subparsers.add_parser("bench", help="Benchmark")
    
    # stats
    stats_parser = subparsers.add_parser("stats", help="Show stats")

    # Add common args to all subparsers that need them
    for sp in [chat_parser, test_parser, bench_parser]:
        sp.add_argument("--ollama-host", default=None, help="Ollama API URL")
        sp.add_argument("--model", default="tokioai-coder", help="Model name")
        sp.add_argument("-v", "--verbose", action="store_true")

    args = parser.parse_args()

    if args.command == "serve":
        cmd_serve(args)
    elif args.command == "chat":
        cmd_chat(args)
    elif args.command == "test":
        cmd_test(args)
    elif args.command == "bench":
        cmd_bench(args)
    elif args.command == "stats":
        cmd_stats(args)
    else:
        parser.print_help()


if __name__ == "__main__":
    main()