chat / app /services /openai_service.py
MuhammadSaad16's picture
Add application file
9e8f2ae
# app/services/openai_service.py
from openai import OpenAI
from app.config import settings
from typing import List
import json
class OpenAIService:
def __init__(self):
self.client = OpenAI(
api_key=settings.OPENAI_API_KEY
)
self.model = settings.OPENAI_MODEL_CHAT
async def get_chat_response(self, prompt: str, history: List[dict] = None) -> str:
"""Generate chat response using OpenAI API."""
messages = []
if history:
for msg in history:
if msg["role"] != "system":
messages.append({
"role": msg["role"],
"content": msg["content"]
})
messages.append({"role": "user", "content": prompt})
response = self.client.chat.completions.create(
model=self.model,
messages=messages
)
return response.choices[0].message.content
async def translate_to_urdu(self, content: str) -> str:
"""Translate English content to Urdu using OpenAI API."""
messages = [
{
"role": "system",
"content": "You are a professional translator. Translate the following English text to Urdu. Maintain technical terms. Provide only the Urdu translation without any explanation or additional text."
},
{
"role": "user",
"content": content
}
]
response = self.client.chat.completions.create(
model=self.model,
messages=messages
)
return response.choices[0].message.content
async def personalize_content(
self,
content: str,
software_level: str,
hardware_level: str,
learning_goals: str
) -> dict:
"""Personalize content based on user's background."""
system_prompt = f"""You are an expert educational content adapter. Your task is to personalize the following content based on the user's background.
USER PROFILE:
- Software/Programming Level: {software_level}
- Hardware/Electronics Level: {hardware_level}
- Learning Goals: {learning_goals if learning_goals else 'Not specified'}
PERSONALIZATION RULES:
For Software Level:
- beginner: Add detailed explanations, use simpler terminology, break down complex concepts, provide examples
- intermediate: Maintain moderate complexity, brief explanations for advanced concepts only
- advanced: Add technical depth, skip basic explanations, use precise technical terminology
For Hardware Level:
- none: Explain all hardware concepts from scratch, use analogies
- basic: Brief hardware explanations, define technical terms
- experienced: Use technical hardware terminology without explanation
If learning goals are specified, emphasize and connect content to those objectives.
OUTPUT FORMAT:
Respond with a JSON object containing exactly two fields:
1. "personalized_content": The adapted content
2. "adjustments_made": A brief description of what changes were made
Example response format:
{{"personalized_content": "...", "adjustments_made": "..."}}"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": content}
]
response = self.client.chat.completions.create(
model=self.model,
messages=messages
)
result = json.loads(response.choices[0].message.content)
return result