File size: 1,506 Bytes
ee1878f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Command implementation for the chat interface."""
import chromadb
from knowlang.chat_bot.chat_interface import create_chatbot
from knowlang.configs.config import AppConfig
from knowlang.cli.types import ChatCommandArgs
from knowlang.utils.fancy_log import FancyLogger

LOG = FancyLogger(__name__)

def create_config(args: ChatCommandArgs) -> AppConfig:
    """Create configuration from file or defaults."""
    if args.config:
        with open(args.config, 'r') as file:
            config_data = file.read()
            return AppConfig.model_validate_json(config_data)
    return AppConfig()

async def chat_command(args: ChatCommandArgs) -> None:
    """Execute the chat command.
    
    Args:
        args: Typed command line arguments
    """
    config = create_config(args)
    
    # Verify database exists
    try:
        db_client = chromadb.PersistentClient(path=str(config.db.persist_directory))
        db_client.get_collection(name=config.db.collection_name)
    except Exception as e:
        LOG.error(
            "Database not found. Please run 'knowlang parse' first to index your codebase."
            f"\nError: {str(e)}"
        )
        return
    
    # Create and launch chatbot
    demo = create_chatbot(config)
    
    launch_kwargs = {
        "server_port": args.server_port,
        "server_name": args.server_name or "127.0.0.1",
        "share": args.share,
    }
    if args.port:
        launch_kwargs["port"] = args.port
        
    demo.launch(**launch_kwargs)