44"""
55
66import importlib
7+ import os
78import sys
89
9- from ..common import BotConfig
10+ from ..common import BotConfig , reindent
1011from .common import Action , Bot , Goal , Operation , OperationHook , Toolbox
1112
1213__all__ = [
1920]
2021
2122
22- def load_bot (config : BotConfig ) -> Bot :
23- """Load and return a Bot instance using the provided configuration.
23+ def load_bot (config : BotConfig | None ) -> Bot :
24+ """Load and return a Bot instance using the provided configuration"""
25+ if not config :
26+ return _default_bot ()
2427
25- If a pythonpath is specified in the config and not already present in
26- sys.path, it is added. The function expects the config.factory in the
27- format 'module:symbol' or 'symbol'. If only 'symbol' is provided, the
28- current module is used.
29-
30- Args:
31- config: BotConfig object containing bot configuration details.
32-
33- Raises:
34- NotImplementedError: If the specified factory cannot be found.
35- """
3628 if config .pythonpath and config .pythonpath not in sys .path :
3729 sys .path .insert (0 , config .pythonpath )
3830
@@ -44,7 +36,36 @@ def load_bot(config: BotConfig) -> Bot:
4436
4537 factory = getattr (module , symbol , None )
4638 if not factory :
47- raise NotImplementedError (f"Unknown bot factory: { factory } " )
39+ raise NotImplementedError (f"Unknown bot factory: { config . factory } " )
4840
4941 kwargs = config .config or {}
5042 return factory (** kwargs )
43+
44+
45+ def _default_bot () -> Bot :
46+ if not os .environ .get ("OPENAI_API_KEY" ):
47+ raise RuntimeError (
48+ reindent (
49+ """
50+ The default bot implementation requires an OpenAI API key.
51+ Please specify one via the `$OPENAI_API_KEY` environment
52+ variable or enable a different bot in your configuration.
53+ """
54+ )
55+ )
56+
57+ try :
58+ from .openai import threads_bot
59+
60+ except ImportError :
61+ raise RuntimeError (
62+ reindent (
63+ """
64+ The default bot implementation requires the `openai` Python
65+ package. Please install it or specify a different bot in
66+ your configuration.
67+ """
68+ )
69+ )
70+ else :
71+ return threads_bot ()
0 commit comments