Fix the max_tokens, temperature, and model setup.

This commit is contained in:
Oleksandr Kozachuk 2023-08-12 12:20:49 +02:00
parent bc5e6228a6
commit e4d055b900
2 changed files with 23 additions and 14 deletions

View File

@ -21,7 +21,7 @@ def tags_completer(prefix, parsed_args, **kwargs):
return get_tags_unique(config, prefix) return get_tags_unique(config, prefix)
def read_config(path: str): def read_config(path: str) -> ConfigType:
with open(path, 'r') as f: with open(path, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader) config = yaml.load(f, Loader=yaml.FullLoader)
return config return config
@ -81,6 +81,15 @@ def ask_cmd(args: argparse.Namespace, config: ConfigType) -> None:
""" """
Handler for the 'ask' command. Handler for the 'ask' command.
""" """
if type(config['openai']) is not dict:
raise RuntimeError('Configuration openai is not a dict.')
config_openai = config['openai']
if args.max_tokens:
config_openai['max_tokens'] = args.max_tokens
if args.temperature:
config_openai['temperature'] = args.temperature
if args.model:
config_openai['model'] = args.model
chat, question, tags = create_question_with_hist(args, config) chat, question, tags = create_question_with_hist(args, config)
print_chat_hist(chat, False, args.only_source_code) print_chat_hist(chat, False, args.only_source_code)
otags = args.output_tags or [] otags = args.output_tags or []
@ -211,13 +220,14 @@ def main() -> int:
config = read_config(args.config) config = read_config(args.config)
# modify config according to args # modify config according to args
openai_api_key(config['openai']['api_key']) if type(config['openai']) is dict:
if args.max_tokens: config_openai = config['openai']
config['openai']['max_tokens'] = args.max_tokens else:
if args.temperature: RuntimeError("Configuration openai is not a dict.")
config['openai']['temperature'] = args.temperature if type(config_openai['api_key']) is str:
if args.model: openai_api_key(config_openai['api_key'])
config['openai']['model'] = args.model else:
raise RuntimeError("Configuration openai.api_key is not a string.")
command.func(command, config) command.func(command, config)

View File

@ -1,8 +1,7 @@
import shutil import shutil
from pprint import PrettyPrinter from pprint import PrettyPrinter
from typing import List, Dict, Union
ConfigType = Dict[str, Union[str, Dict[str, Union[str, int]]]] ConfigType = dict[str, str | dict[str, str | int | float]]
def terminal_width() -> int: def terminal_width() -> int:
@ -31,15 +30,15 @@ def print_tag_args(tags: list[str], extags: list[str], otags: list[str]) -> None
print() print()
def append_message(chat: List[Dict[str, str]], def append_message(chat: list[dict[str, str]],
role: str, role: str,
content: str content: str
) -> None: ) -> None:
chat.append({'role': role, 'content': content.replace("''", "'")}) chat.append({'role': role, 'content': content.replace("''", "'")})
def message_to_chat(message: Dict[str, str], def message_to_chat(message: dict[str, str],
chat: List[Dict[str, str]], chat: list[dict[str, str]],
with_tags: bool = False, with_tags: bool = False,
with_file: bool = False with_file: bool = False
) -> None: ) -> None:
@ -80,6 +79,6 @@ def print_chat_hist(chat, dump=False, source_code=False) -> None:
print(f"{message['role'].upper()}: {message['content']}") print(f"{message['role'].upper()}: {message['content']}")
def print_tags_frequency(tags: List[str]) -> None: def print_tags_frequency(tags: list[str]) -> None:
for tag in sorted(set(tags)): for tag in sorted(set(tags)):
print(f"- {tag}: {tags.count(tag)}") print(f"- {tag}: {tags.count(tag)}")