Compare commits
No commits in common. "dc13213c4dd118848d395a009ad98bd2277a0a0a" and "a5075b14a0ad617b131565a906e8100cbd9fc76f" have entirely different histories.
dc13213c4d
...
a5075b14a0
@ -1,17 +1,11 @@
|
|||||||
import openai
|
import openai
|
||||||
|
|
||||||
from .utils import ChatType
|
|
||||||
from .configuration import Config
|
|
||||||
|
|
||||||
|
|
||||||
def openai_api_key(api_key: str) -> None:
|
def openai_api_key(api_key: str) -> None:
|
||||||
openai.api_key = api_key
|
openai.api_key = api_key
|
||||||
|
|
||||||
|
|
||||||
def print_models() -> None:
|
def print_models() -> None:
|
||||||
"""
|
|
||||||
Print all models supported by the current AI.
|
|
||||||
"""
|
|
||||||
not_ready = []
|
not_ready = []
|
||||||
for engine in sorted(openai.Engine.list()['data'], key=lambda x: x['id']):
|
for engine in sorted(openai.Engine.list()['data'], key=lambda x: x['id']):
|
||||||
if engine['ready']:
|
if engine['ready']:
|
||||||
@ -22,16 +16,10 @@ def print_models() -> None:
|
|||||||
print('\nNot ready: ' + ', '.join(not_ready))
|
print('\nNot ready: ' + ', '.join(not_ready))
|
||||||
|
|
||||||
|
|
||||||
def ai(chat: ChatType,
|
def ai(chat: list[dict[str, str]],
|
||||||
config: Config,
|
config: dict,
|
||||||
number: int
|
number: int
|
||||||
) -> tuple[list[str], dict[str, int]]:
|
) -> tuple[list[str], dict[str, int]]:
|
||||||
"""
|
|
||||||
Make AI request with the given chat history and configuration.
|
|
||||||
Return AI response and tokens used.
|
|
||||||
"""
|
|
||||||
if not isinstance(config['openai'], dict):
|
|
||||||
raise RuntimeError('Configuration openai is not a dict.')
|
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
model=config['openai']['model'],
|
model=config['openai']['model'],
|
||||||
messages=chat,
|
messages=chat,
|
||||||
|
|||||||
@ -1,23 +0,0 @@
|
|||||||
from typing import TypedDict
|
|
||||||
|
|
||||||
|
|
||||||
class OpenAIConfig(TypedDict):
|
|
||||||
"""
|
|
||||||
The OpenAI section of the configuration file.
|
|
||||||
"""
|
|
||||||
api_key: str
|
|
||||||
model: str
|
|
||||||
temperature: float
|
|
||||||
max_tokens: int
|
|
||||||
top_p: float
|
|
||||||
frequency_penalty: float
|
|
||||||
presence_penalty: float
|
|
||||||
|
|
||||||
|
|
||||||
class Config(TypedDict):
|
|
||||||
"""
|
|
||||||
The configuration file structure.
|
|
||||||
"""
|
|
||||||
system: str
|
|
||||||
db: str
|
|
||||||
openai: OpenAIConfig
|
|
||||||
@ -7,25 +7,23 @@ import sys
|
|||||||
import argcomplete
|
import argcomplete
|
||||||
import argparse
|
import argparse
|
||||||
import pathlib
|
import pathlib
|
||||||
from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ChatType
|
from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ConfigType
|
||||||
from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, read_config, write_config, dump_data
|
from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, read_config, write_config, dump_data
|
||||||
from .api_client import ai, openai_api_key, print_models
|
from .api_client import ai, openai_api_key, print_models
|
||||||
from .configuration import Config
|
|
||||||
from itertools import zip_longest
|
from itertools import zip_longest
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
default_config = '.config.yaml'
|
default_config = '.config.yaml'
|
||||||
|
|
||||||
|
|
||||||
def tags_completer(prefix: str, parsed_args: Any, **kwargs: Any) -> list[str]:
|
def tags_completer(prefix, parsed_args, **kwargs):
|
||||||
with open(parsed_args.config, 'r') as f:
|
with open(parsed_args.config, 'r') as f:
|
||||||
config = yaml.load(f, Loader=yaml.FullLoader)
|
config = yaml.load(f, Loader=yaml.FullLoader)
|
||||||
return get_tags_unique(config, prefix)
|
return get_tags_unique(config, prefix)
|
||||||
|
|
||||||
|
|
||||||
def create_question_with_hist(args: argparse.Namespace,
|
def create_question_with_hist(args: argparse.Namespace,
|
||||||
config: Config,
|
config: ConfigType,
|
||||||
) -> tuple[ChatType, str, list[str]]:
|
) -> tuple[list[dict[str, str]], str, list[str]]:
|
||||||
"""
|
"""
|
||||||
Creates the "AI request", including the question and chat history as determined
|
Creates the "AI request", including the question and chat history as determined
|
||||||
by the specified tags.
|
by the specified tags.
|
||||||
@ -57,7 +55,7 @@ def create_question_with_hist(args: argparse.Namespace,
|
|||||||
return chat, full_question, tags
|
return chat, full_question, tags
|
||||||
|
|
||||||
|
|
||||||
def tag_cmd(args: argparse.Namespace, config: Config) -> None:
|
def tag_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||||
"""
|
"""
|
||||||
Handler for the 'tag' command.
|
Handler for the 'tag' command.
|
||||||
"""
|
"""
|
||||||
@ -65,10 +63,13 @@ def tag_cmd(args: argparse.Namespace, config: Config) -> None:
|
|||||||
print_tags_frequency(get_tags(config, None))
|
print_tags_frequency(get_tags(config, None))
|
||||||
|
|
||||||
|
|
||||||
def config_cmd(args: argparse.Namespace, config: Config) -> None:
|
def config_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||||
"""
|
"""
|
||||||
Handler for the 'config' command.
|
Handler for the 'config' command.
|
||||||
"""
|
"""
|
||||||
|
if type(config['openai']) is not dict:
|
||||||
|
raise RuntimeError('Configuration openai is not a dict.')
|
||||||
|
|
||||||
if args.list_models:
|
if args.list_models:
|
||||||
print_models()
|
print_models()
|
||||||
elif args.print_model:
|
elif args.print_model:
|
||||||
@ -78,16 +79,19 @@ def config_cmd(args: argparse.Namespace, config: Config) -> None:
|
|||||||
write_config(args.config, config)
|
write_config(args.config, config)
|
||||||
|
|
||||||
|
|
||||||
def ask_cmd(args: argparse.Namespace, config: Config) -> None:
|
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:
|
if args.max_tokens:
|
||||||
config['openai']['max_tokens'] = args.max_tokens
|
config_openai['max_tokens'] = args.max_tokens
|
||||||
if args.temperature:
|
if args.temperature:
|
||||||
config['openai']['temperature'] = args.temperature
|
config_openai['temperature'] = args.temperature
|
||||||
if args.model:
|
if args.model:
|
||||||
config['openai']['model'] = 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 []
|
||||||
@ -97,7 +101,7 @@ def ask_cmd(args: argparse.Namespace, config: Config) -> None:
|
|||||||
print(f"Usage: {usage}")
|
print(f"Usage: {usage}")
|
||||||
|
|
||||||
|
|
||||||
def hist_cmd(args: argparse.Namespace, config: Config) -> None:
|
def hist_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||||
"""
|
"""
|
||||||
Handler for the 'hist' command.
|
Handler for the 'hist' command.
|
||||||
"""
|
"""
|
||||||
@ -111,7 +115,7 @@ def hist_cmd(args: argparse.Namespace, config: Config) -> None:
|
|||||||
print_chat_hist(chat, args.dump, args.only_source_code)
|
print_chat_hist(chat, args.dump, args.only_source_code)
|
||||||
|
|
||||||
|
|
||||||
def print_cmd(args: argparse.Namespace, config: Config) -> None:
|
def print_cmd(args: argparse.Namespace, config: ConfigType) -> None:
|
||||||
"""
|
"""
|
||||||
Handler for the 'print' command.
|
Handler for the 'print' command.
|
||||||
"""
|
"""
|
||||||
@ -227,7 +231,10 @@ def main() -> int:
|
|||||||
command = parser.parse_args()
|
command = parser.parse_args()
|
||||||
config = read_config(args.config)
|
config = read_config(args.config)
|
||||||
|
|
||||||
openai_api_key(config['openai']['api_key'])
|
if type(config['openai']) is dict and type(config['openai']['api_key']) is str:
|
||||||
|
openai_api_key(config['openai']['api_key'])
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Configuration openai.api_key is wrong.")
|
||||||
|
|
||||||
command.func(command, config)
|
command.func(command, config)
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
import yaml
|
import yaml
|
||||||
import io
|
import io
|
||||||
import pathlib
|
import pathlib
|
||||||
from .utils import terminal_width, append_message, message_to_chat, ChatType
|
from .utils import terminal_width, append_message, message_to_chat, ConfigType
|
||||||
from .configuration import Config
|
from typing import List, Dict, Any, Optional
|
||||||
from typing import Any, Optional
|
|
||||||
|
|
||||||
|
|
||||||
def read_file(fname: pathlib.Path, tags_only: bool = False) -> dict[str, Any]:
|
def read_file(fname: pathlib.Path, tags_only: bool = False) -> Dict[str, Any]:
|
||||||
with open(fname, "r") as fd:
|
with open(fname, "r") as fd:
|
||||||
tagline = fd.readline().strip().split(':', maxsplit=1)[1].strip()
|
tagline = fd.readline().strip().split(':', maxsplit=1)[1].strip()
|
||||||
# also support tags separated by ',' (old format)
|
# also support tags separated by ',' (old format)
|
||||||
@ -23,18 +22,18 @@ def read_file(fname: pathlib.Path, tags_only: bool = False) -> dict[str, Any]:
|
|||||||
"file": fname.name}
|
"file": fname.name}
|
||||||
|
|
||||||
|
|
||||||
def read_config(path: str) -> Config:
|
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
|
||||||
|
|
||||||
|
|
||||||
def write_config(path: str, config: Config) -> None:
|
def write_config(path: str, config: ConfigType) -> None:
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w') as f:
|
||||||
yaml.dump(config, f)
|
yaml.dump(config, f)
|
||||||
|
|
||||||
|
|
||||||
def dump_data(data: dict[str, Any]) -> str:
|
def dump_data(data: Dict[str, Any]) -> str:
|
||||||
with io.StringIO() as fd:
|
with io.StringIO() as fd:
|
||||||
fd.write(f'TAGS: {" ".join(data["tags"])}\n')
|
fd.write(f'TAGS: {" ".join(data["tags"])}\n')
|
||||||
fd.write(f'=== QUESTION ===\n{data["question"]}\n')
|
fd.write(f'=== QUESTION ===\n{data["question"]}\n')
|
||||||
@ -42,7 +41,7 @@ def dump_data(data: dict[str, Any]) -> str:
|
|||||||
return fd.getvalue()
|
return fd.getvalue()
|
||||||
|
|
||||||
|
|
||||||
def write_file(fname: str, data: dict[str, Any]) -> None:
|
def write_file(fname: str, data: Dict[str, Any]) -> None:
|
||||||
with open(fname, "w") as fd:
|
with open(fname, "w") as fd:
|
||||||
fd.write(f'TAGS: {" ".join(data["tags"])}\n')
|
fd.write(f'TAGS: {" ".join(data["tags"])}\n')
|
||||||
fd.write(f'=== QUESTION ===\n{data["question"]}\n')
|
fd.write(f'=== QUESTION ===\n{data["question"]}\n')
|
||||||
@ -53,7 +52,7 @@ def save_answers(question: str,
|
|||||||
answers: list[str],
|
answers: list[str],
|
||||||
tags: list[str],
|
tags: list[str],
|
||||||
otags: Optional[list[str]],
|
otags: Optional[list[str]],
|
||||||
config: Config
|
config: ConfigType
|
||||||
) -> None:
|
) -> None:
|
||||||
wtags = otags or tags
|
wtags = otags or tags
|
||||||
num, inum = 0, 0
|
num, inum = 0, 0
|
||||||
@ -76,14 +75,14 @@ def save_answers(question: str,
|
|||||||
|
|
||||||
|
|
||||||
def create_chat_hist(question: Optional[str],
|
def create_chat_hist(question: Optional[str],
|
||||||
tags: Optional[list[str]],
|
tags: Optional[List[str]],
|
||||||
extags: Optional[list[str]],
|
extags: Optional[List[str]],
|
||||||
config: Config,
|
config: ConfigType,
|
||||||
match_all_tags: bool = False,
|
match_all_tags: bool = False,
|
||||||
with_tags: bool = False,
|
with_tags: bool = False,
|
||||||
with_file: bool = False
|
with_file: bool = False
|
||||||
) -> ChatType:
|
) -> List[Dict[str, str]]:
|
||||||
chat: ChatType = []
|
chat: List[Dict[str, str]] = []
|
||||||
append_message(chat, 'system', str(config['system']).strip())
|
append_message(chat, 'system', str(config['system']).strip())
|
||||||
for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
|
for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
|
||||||
if file.suffix == '.yaml':
|
if file.suffix == '.yaml':
|
||||||
@ -109,7 +108,7 @@ def create_chat_hist(question: Optional[str],
|
|||||||
return chat
|
return chat
|
||||||
|
|
||||||
|
|
||||||
def get_tags(config: Config, prefix: Optional[str]) -> list[str]:
|
def get_tags(config: ConfigType, prefix: Optional[str]) -> List[str]:
|
||||||
result = []
|
result = []
|
||||||
for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
|
for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
|
||||||
if file.suffix == '.yaml':
|
if file.suffix == '.yaml':
|
||||||
@ -128,5 +127,5 @@ def get_tags(config: Config, prefix: Optional[str]) -> list[str]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_tags_unique(config: Config, prefix: Optional[str]) -> list[str]:
|
def get_tags_unique(config: ConfigType, prefix: Optional[str]) -> List[str]:
|
||||||
return list(set(get_tags(config, prefix)))
|
return list(set(get_tags(config, prefix)))
|
||||||
|
|||||||
@ -1,15 +1,14 @@
|
|||||||
import shutil
|
import shutil
|
||||||
from pprint import PrettyPrinter
|
from pprint import PrettyPrinter
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
ChatType = list[dict[str, str]]
|
ConfigType = dict[str, str | dict[str, str | int | float]]
|
||||||
|
|
||||||
|
|
||||||
def terminal_width() -> int:
|
def terminal_width() -> int:
|
||||||
return shutil.get_terminal_size().columns
|
return shutil.get_terminal_size().columns
|
||||||
|
|
||||||
|
|
||||||
def pp(*args: Any, **kwargs: Any) -> None:
|
def pp(*args, **kwargs) -> None:
|
||||||
return PrettyPrinter(width=terminal_width()).pprint(*args, **kwargs)
|
return PrettyPrinter(width=terminal_width()).pprint(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@ -31,7 +30,7 @@ def print_tag_args(tags: list[str], extags: list[str], otags: list[str]) -> None
|
|||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
def append_message(chat: ChatType,
|
def append_message(chat: list[dict[str, str]],
|
||||||
role: str,
|
role: str,
|
||||||
content: str
|
content: str
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -39,7 +38,7 @@ def append_message(chat: ChatType,
|
|||||||
|
|
||||||
|
|
||||||
def message_to_chat(message: dict[str, str],
|
def message_to_chat(message: dict[str, str],
|
||||||
chat: ChatType,
|
chat: list[dict[str, str]],
|
||||||
with_tags: bool = False,
|
with_tags: bool = False,
|
||||||
with_file: bool = False
|
with_file: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -62,7 +61,7 @@ def display_source_code(content: str) -> None:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def print_chat_hist(chat: ChatType, dump: bool = False, source_code: bool = False) -> None:
|
def print_chat_hist(chat, dump=False, source_code=False) -> None:
|
||||||
if dump:
|
if dump:
|
||||||
pp(chat)
|
pp(chat)
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user