File size: 2,314 Bytes
eca9523
 
918a703
 
 
 
 
 
 
c207609
918a703
 
 
 
 
 
 
 
 
 
 
 
 
 
eca9523
 
 
 
 
 
 
918a703
 
 
 
 
 
 
 
 
 
 
cde132e
 
 
918a703
 
 
cde132e
 
 
 
 
918a703
 
eca9523
918a703
 
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
from typing import List, Tuple

def generate_response(
    user_input: str, 
    history: List[Tuple[str, str]], 
    max_tokens: int, 
    temperature: float, 
    top_p: float
) -> str:
    """
    Generates a response from the AI model.
    Args:
        user_input: The user's input message.
        history: A list of tuples containing the conversation history 
                 (user input, AI response).
        max_tokens: The maximum number of tokens in the generated response.
        temperature: Controls the randomness of the generated response.
        top_p: Controls the nucleus sampling probability.
    Returns:
        str: The generated response from the AI model.
    """
    try:
        # Build the message list with system message and history
        messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
        
        # Iterate through the history list and format accordingly
        for user_message, assistant_message in history:
            messages.append({"role": "user", "content": user_message})
            messages.append({"role": "assistant", "content": assistant_message})
        
        # Add the current user input
        messages.append({"role": "user", "content": user_input})

        # Generate response from the model
        response = ""
        for msg in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            # Check if 'choices' is present and non-empty
            if msg and 'choices' in msg and msg['choices']:
                # Ensure the 'delta' and 'content' properties exist before using them
                token = msg['choices'][0].get('delta', {}).get('content', '')
                if token:
                    response += token
            else:
                # Handle unexpected response format or empty choices
                print("Warning: Unexpected response format or empty 'choices'.")
                break
        return response or "Sorry, I couldn't generate a response. Please try again."

    except Exception as e:
        # Log the error for debugging purposes
        print(f"An error occurred: {e}")
        return "Error: An unexpected error occurred while processing your request."