From 6672e5ee3a73427341df8ea6db51208f616a2b40 Mon Sep 17 00:00:00 2001 From: juk0de Date: Wed, 8 Nov 2023 20:10:30 +0100 Subject: [PATCH] translation: added check for valid document format when using OpenAI --- chatmastermind/commands/translation.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/chatmastermind/commands/translation.py b/chatmastermind/commands/translation.py index 9ed2945..31fdfa5 100644 --- a/chatmastermind/commands/translation.py +++ b/chatmastermind/commands/translation.py @@ -1,4 +1,5 @@ import argparse +import mimetypes from pathlib import Path from .common import invert_input_tag_args, read_text_file from ..configuration import Config @@ -13,11 +14,16 @@ class TranslationCmdError(Exception): text_separator: str = 'TEXT:' +def assert_document_type_supported_openai(document_file: Path) -> None: + doctype = mimetypes.guess_type(document_file) + if doctype != 'text/plain': + raise TranslationCmdError("AI 'OpenAI' only supports document type 'text/plain''") + + def translation_prompt_openai(source_lang: str, target_lang: str) -> str: """ Return the prompt for GPT that tells it to do the translation. """ - # FIXME: specify the document format if known return f"Translate the text below the line {text_separator} from {source_lang} to {target_lang}." @@ -51,7 +57,8 @@ def create_message_openai(chat: ChatDB, args: argparse.Namespace) -> Message: user_text: str user_prompt: str if args.input_document is not None: - user_text = read_text_file(args.input_document) + assert_document_type_supported_openai(Path(args.input_document)) + user_text = read_text_file(Path(args.input_document)) user_prompt = '\n\n'.join([str(s) for s in text_args]) else: user_text = text_args[-1]