2024-09-20 22:42:46 +02:00
|
|
|
"""AI services."""
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
from core import enums
|
|
|
|
|
|
|
|
|
|
AI_ACTIONS = {
|
|
|
|
|
"prompt": (
|
2025-03-12 10:14:47 +01:00
|
|
|
"Answer the prompt in markdown format. "
|
|
|
|
|
"Preserve the language and markdown formatting. "
|
|
|
|
|
"Do not provide any other information. "
|
|
|
|
|
"Preserve the language."
|
2024-09-20 22:42:46 +02:00
|
|
|
),
|
|
|
|
|
"correct": (
|
|
|
|
|
"Correct grammar and spelling of the markdown text, "
|
|
|
|
|
"preserving language and markdown formatting. "
|
2025-03-12 10:14:47 +01:00
|
|
|
"Do not provide any other information. "
|
|
|
|
|
"Preserve the language."
|
2024-09-20 22:42:46 +02:00
|
|
|
),
|
|
|
|
|
"rephrase": (
|
|
|
|
|
"Rephrase the given markdown text, "
|
|
|
|
|
"preserving language and markdown formatting. "
|
2025-03-12 10:14:47 +01:00
|
|
|
"Do not provide any other information. "
|
|
|
|
|
"Preserve the language."
|
2024-09-20 22:42:46 +02:00
|
|
|
),
|
|
|
|
|
"summarize": (
|
|
|
|
|
"Summarize the markdown text, preserving language and markdown formatting. "
|
2025-03-12 10:14:47 +01:00
|
|
|
"Do not provide any other information. "
|
|
|
|
|
"Preserve the language."
|
2024-09-20 22:42:46 +02:00
|
|
|
),
|
2025-03-12 11:44:58 +01:00
|
|
|
"beautify": (
|
|
|
|
|
"Add formatting to the text to make it more readable. "
|
2025-03-13 11:29:03 +01:00
|
|
|
"Do not provide any other information. "
|
|
|
|
|
"Preserve the language."
|
|
|
|
|
),
|
|
|
|
|
"emojify": (
|
|
|
|
|
"Add emojis to the important parts of the text. "
|
|
|
|
|
"Do not provide any other information. "
|
2025-03-12 11:44:58 +01:00
|
|
|
"Preserve the language."
|
|
|
|
|
),
|
2024-09-20 22:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AI_TRANSLATE = (
|
2025-05-13 16:00:12 +02:00
|
|
|
"Keep the same html structure and formatting. "
|
2025-03-12 10:14:47 +01:00
|
|
|
"Translate the content in the html to the specified language {language:s}. "
|
|
|
|
|
"Check the translation for accuracy and make any necessary corrections. "
|
2024-09-20 22:42:46 +02:00
|
|
|
"Do not provide any other information."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AIService:
|
|
|
|
|
"""Service class for AI-related operations."""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""Ensure that the AI configuration is set properly."""
|
|
|
|
|
if (
|
|
|
|
|
settings.AI_BASE_URL is None
|
|
|
|
|
or settings.AI_API_KEY is None
|
|
|
|
|
or settings.AI_MODEL is None
|
|
|
|
|
):
|
|
|
|
|
raise ImproperlyConfigured("AI configuration not set")
|
|
|
|
|
self.client = OpenAI(base_url=settings.AI_BASE_URL, api_key=settings.AI_API_KEY)
|
|
|
|
|
|
|
|
|
|
def call_ai_api(self, system_content, text):
|
|
|
|
|
"""Helper method to call the OpenAI API and process the response."""
|
|
|
|
|
response = self.client.chat.completions.create(
|
|
|
|
|
model=settings.AI_MODEL,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": system_content},
|
2025-03-12 10:14:47 +01:00
|
|
|
{"role": "user", "content": text},
|
2024-09-20 22:42:46 +02:00
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
content = response.choices[0].message.content
|
|
|
|
|
|
2025-03-12 10:14:47 +01:00
|
|
|
if not content:
|
2024-09-20 22:42:46 +02:00
|
|
|
raise RuntimeError("AI response does not contain an answer")
|
|
|
|
|
|
2025-03-12 10:14:47 +01:00
|
|
|
return {"answer": content}
|
2024-09-20 22:42:46 +02:00
|
|
|
|
|
|
|
|
def transform(self, text, action):
|
|
|
|
|
"""Transform text based on specified action."""
|
|
|
|
|
system_content = AI_ACTIONS[action]
|
|
|
|
|
return self.call_ai_api(system_content, text)
|
|
|
|
|
|
|
|
|
|
def translate(self, text, language):
|
|
|
|
|
"""Translate text to a specified language."""
|
|
|
|
|
language_display = enums.ALL_LANGUAGES.get(language, language)
|
|
|
|
|
system_content = AI_TRANSLATE.format(language=language_display)
|
|
|
|
|
return self.call_ai_api(system_content, text)
|