initial
This commit is contained in:
62
ReactDev/core/ai_interface.py
Normal file
62
ReactDev/core/ai_interface.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from openai import OpenAI
|
||||
from ..config import OPENAI_API_KEY, OPENAI_BASE_URL, DEFAULT_MODEL
|
||||
from ..utils.logger import log
|
||||
|
||||
class AIInterface:
|
||||
def __init__(self):
|
||||
self.client = OpenAI(
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url=OPENAI_BASE_URL,
|
||||
)
|
||||
|
||||
def send_request(self, prompt: str, context: dict = None) -> dict:
|
||||
"""
|
||||
Отправляет запрос на эндпоинт ИИ через OpenAI и возвращает ответ.
|
||||
"""
|
||||
try:
|
||||
log(f"Sending request to AI endpoint with prompt: {prompt}")
|
||||
|
||||
# Подготовка сообщений для чат-комплитшена
|
||||
messages = [{"role": "user", "content": prompt}]
|
||||
if context:
|
||||
# Преобразуем контекст в строку
|
||||
context_str = self._format_context(context)
|
||||
messages.append({"role": "system", "content": context_str})
|
||||
|
||||
# Создание запроса к OpenAI
|
||||
response = self.client.chat.completions.create(
|
||||
model=DEFAULT_MODEL,
|
||||
messages=[{"role": "user", "content": "Write a poem about a tree"}],
|
||||
stream=False, # Пока отключаем стриминг для простоты
|
||||
)
|
||||
|
||||
# Обработка ответа
|
||||
return {"response": response.choices[0].message.content}
|
||||
|
||||
except Exception as e:
|
||||
log(f"Error communicating with AI endpoint: {e}", level="error")
|
||||
raise
|
||||
|
||||
def _format_context(self, context: dict) -> str:
|
||||
"""
|
||||
Преобразует контекст в строку для передачи в OpenAI API.
|
||||
"""
|
||||
formatted_context = []
|
||||
for key, value in context.items():
|
||||
formatted_context.append(f"{key}: {value}")
|
||||
return "\n".join(formatted_context)
|
||||
|
||||
|
||||
def analyze_project(self, project_structure: str, prompt: str) -> dict:
|
||||
"""
|
||||
Анализирует структуру проекта и запрос пользователя через ИИ.
|
||||
"""
|
||||
context = {"project_structure": project_structure}
|
||||
return self.send_request(prompt, context)
|
||||
|
||||
def request_additional_info(self, message: str) -> str:
|
||||
"""
|
||||
Запрашивает дополнительную информацию у пользователя.
|
||||
"""
|
||||
log("Requesting additional information from user.")
|
||||
return input(f"{message}\n> ")
|
||||
Reference in New Issue
Block a user