внедри Основные изменения: Все .docx файлы в папке контекста загружаются в контекст…
- User
внедри Основные изменения:
codeВсе .docx файлы в папке контекста загружаются в контекст модели. Инструкции загружаются из одного файла, если он существует. Модель решает, что делать на основе предоставленного контекста и инструкций. Если задача не ясна, модель может запросить дополнительную информацию. import osimport sys import logging import configparser from pathlib import Path from typing import List, Dict import requests from docx import Document
Настройка логирования
logging.basicConfig( level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[ logging.StreamHandler(sys.stdout), logging.FileHandler("automation.log", encoding='utf-8') ] ) logger = logging.getLogger(name)
def load_config(config_path: str) -> Dict[str, str]: config = configparser.ConfigParser() if not os.path.exists(config_path): logger.critical(f"Файл конфигурации '{config_path}' не найден.") sys.exit(1) config.read(config_path, encoding='utf-8') try: openrouter_api_key = config['API']['openrouter_api_key'] openrouter_endpoint = config['API']['openrouter_endpoint'] if not openrouter_api_key or not openrouter_endpoint: raise ValueError logger.info("API-ключ и эндпоинт OpenRouter успешно загружены из конфигурации.") return { "api_key": openrouter_api_key, "endpoint": openrouter_endpoint } except (KeyError, ValueError): logger.critical("API-ключ или эндпоинт OpenRouter не найдены в файле конфигурации.") sys.exit(1)
def read_docx(file_path: Path) -> str: try: document = Document(file_path) text = "\n".join([para.text for para in document.paragraphs]) logger.debug(f"Файл '{file_path}' успешно прочитан.") return text except Exception as e: logger.error(f"Ошибка при чтении файла '{file_path}': {e}") return ""
def get_all_docx_files(directory: Path) -> List[Path]: return list(directory.rglob("*.docx"))
def is_incomplete(response: Dict) -> bool: return response.get('finish_reason') == 'length'
def generate_content(api_key: str, endpoint: str, prompt: str, model: str = "anthropic/claude-3.5-sonnet", max_tokens: int = 1000, temperature: float = 0.7) -> Dict: headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" } payload = { "model": model, "prompt": prompt, "max_tokens": max_tokens, "temperature": temperature } try: response = requests.post(endpoint, headers=headers, json=payload) response.raise_for_status() data = response.json() logger.debug("Контент успешно сгенерирован.") return { "content": data.get('choices', [{}])[0].get('message', {}).get('content', '').strip(), "finish_reason": data.get('choices', [{}])[0].get('finish_reason', '') } except requests.exceptions.RequestException as e: logger.error(f"Ошибка при запросе к OpenRouter API: {e}") return {"content": "", "finish_reason": ""}
- Assistant