Canstralian's picture
Update app.py
eca9523 verified
raw
history blame
2.31 kB
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."