| |
| import re |
| from groq import Groq |
| from typing import List, Dict, Optional |
|
|
|
|
| class ChatService: |
| def __init__(self, api_key: str): |
| self.client = Groq(api_key=api_key) |
|
|
| def get_groq_response(self, messages: List[Dict[str, str]]) -> str: |
| chat_completion = self.client.chat.completions.create( |
| messages=messages, |
| model="llama3-8b-8192", |
| temperature=0.7, |
| max_tokens=1000, |
| ) |
| return chat_completion.choices[0].message.content |
|
|
| @staticmethod |
| def is_arabic(text: str) -> bool: |
| arabic_pattern = re.compile(r"[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]+") |
| return bool(arabic_pattern.search(text)) |
|
|
| @staticmethod |
| def get_chat_preview(chat: List[Dict[str, str]]) -> str: |
| for message in chat: |
| if message["role"] == "user": |
| return ( |
| message["content"][:30] + "..." |
| if len(message["content"]) > 30 |
| else message["content"] |
| ) |
| return "دردشة جديدة" |
|
|