File size: 954 Bytes
97b42fd
 
 
 
 
 
 
 
 
cf1ca98
97b42fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from groq import Groq 

from scripts.llm.runner import run_groq

from scripts.llm.services import save_history , load_history

async def run_llm(
    product_details : dict , 
    product_id : int ,
    user_id : int , 
    query : str , 
    system_prompt : str , 
    groq_client : Groq , 
    redis_client = ''
) -> str : 
    
    system_prompt += str(product_details)
    session_id : str= str(product_id) + str(user_id)
    
    history : list = await load_history(redis_client , session_id)

    if history == [] : history = [
        {
            'role' : 'system' , 
            'content' : system_prompt
        }
    ]

    history.append({
        'role' : 'user' , 
        'content' : query
    })

    response : str  = await run_groq(history , groq_client)

    history.append({
        'role' : 'assistant' , 
        'content' : response
    })
    

    await save_history(redis_client , history , session_id)
    
    return response