38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
"""
|
|
Creates different AI instances, based on the given configuration.
|
|
"""
|
|
|
|
import argparse
|
|
from typing import cast
|
|
from .configuration import Config, OpenAIConfig, default_ai_ID
|
|
from .ai import AI, AIError
|
|
from .ais.openai_cmm import OpenAI
|
|
|
|
|
|
def create_ai(args: argparse.Namespace, config: Config) -> AI:
|
|
"""
|
|
Creates an AI subclass instance from the given arguments
|
|
and configuration file.
|
|
"""
|
|
if args.ai:
|
|
try:
|
|
ai_conf = config.ais[args.ai]
|
|
except KeyError:
|
|
raise AIError(f"AI ID '{args.ai}' does not exist in this configuration")
|
|
elif default_ai_ID in config.ais:
|
|
ai_conf = config.ais[default_ai_ID]
|
|
else:
|
|
raise AIError("No AI name given and no default exists")
|
|
|
|
if ai_conf.name == 'openai':
|
|
ai = OpenAI(cast(OpenAIConfig, ai_conf))
|
|
if args.model:
|
|
ai.config.model = args.model
|
|
if args.max_tokens:
|
|
ai.config.max_tokens = args.max_tokens
|
|
if args.temperature:
|
|
ai.config.temperature = args.temperature
|
|
return ai
|
|
else:
|
|
raise AIError(f"AI '{args.ai}' is not supported")
|