File size: 1,092 Bytes
23b8b85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# chat_service.py
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 "دردشة جديدة"