Hi Stijn,
So, this is an image of the interface. Now the chat just give a random answer from a list. But instead of that, I would like to get the agent’s answer from a function called: get_response()
The question is, how do I get this line to call get_response
without loosing the conversation in the chat interface. A job will reload the webview.
const SAMPLE_RESPONSES = """ + str(SAMPLE_RESPONSES) + """ <==== Call get_response instead;
Here the complete code. I reduced it so much as I could:
import viktor as vkt
import random
import os
# Sample responses for testing
SAMPLE_RESPONSES = [
"I understand you're asking about that. Let me help you with your request.",
"That's an interesting question! Here's what I think about it.",
"I'd be happy to assist you with that. Here's what I can tell you.",
"Let me process that request for you.",
"I've analyzed your question and here's my response."
]
history = []
def get_response(self, message, history):
history.append({"role": "user", "content": message})
response = random.choice(SAMPLE_RESPONSES)
history.append({"role": "assistant", "content": response})
return response
class Parametrization(vkt.Parametrization):
pass
class Controller(vkt.Controller):
parametrization = Parametrization
@vkt.WebView("Chat", duration_guess=1)
def show_chat(self, params, **kwargs):
# Get the current directory
current_dir = os.path.dirname(os.path.abspath(__file__))
# Read the CSS file
with open(os.path.join(current_dir, 'static', 'css', 'styles.css'), 'r') as f:
css_content = f.read()
# Read the JS file
with open(os.path.join(current_dir, 'static', 'js', 'chat.js'), 'r') as f:
js_content = f.read()
html = """
<!DOCTYPE html>
<html>
<head>
<style>
""" + css_content + """
</style>
</head>
<body>
<div id="chat-container">
<div id="messages">
<div class="message bot-message">Hello! How can I help you today?</div>
</div>
<div id="input-container">
<input type="text" id="message-input" placeholder="Type your message...">
<button id="send-button">Send</button>
</div>
</div>
<script>
const SAMPLE_RESPONSES = """ + str(SAMPLE_RESPONSES) + """;
</script>
<script>
""" + js_content + """
</script>
</body>
</html>
"""
return vkt.WebResult(html=html)
document.getElementById('message-input').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
function sendMessage() {
const input = document.getElementById('message-input');
const message = input.value.trim();
if (message) {
addMessage(message, 'user');
input.value = '';
// Simulate bot response after a short delay
setTimeout(() => {
const randomResponse = SAMPLE_RESPONSES[Math.floor(Math.random() * SAMPLE_RESPONSES.length)];
addMessage(randomResponse, 'bot');
}, 1000);
}
}
Maybe hackers @khameeteman or @mslootweg know how?