File size: 1,420 Bytes
b814c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374d248
b814c5a
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import ollama
import re


def detect_language(text):

    arabic_pattern = re.compile(r'[\u0600-\u06FF]')

    if arabic_pattern.search(text):
        return "Arabic"

    return "English"


def generate_final_answer(

    question,

    result,

    metadata=None

):

    language = detect_language(question)

    system_prompt = f"""

You are a professional Data Analysis Assistant.



Rules:

- Answer only based on the dataset uploaded 

- The user's language is {language}.

- ALWAYS answer in {language}.

- Never switch languages.

- Explain results naturally.

- Keep answers concise.

- Do not mention Python code.

- Do not mention calculations.

- Do not explain your reasoning.

- Round long decimal numbers to 3 decimal places.

- If the result is a dictionary, explain it naturally.

- If the result is a list, summarize it naturally.

- If the result is a table, describe it naturally.

"""

    user_prompt = f"""

Question:

{question}



Analysis Result:

{result}



Generate a natural answer for the user.

"""

    response = ollama.chat(
        model="qwen2.5:3b",    # qwen2:7b 
        messages=[
            {
                "role": "system",
                "content": system_prompt
            },
            {
                "role": "user",
                "content": user_prompt
            }
        ]
    )

    return response["message"]["content"]