Lagt till vidarebefordran av LLM från backen.py till frontend (index.html, client.html, frontend.js

This commit is contained in:
2024-07-29 00:47:42 +02:00
parent 534874e810
commit 941b426574
4 changed files with 20 additions and 18 deletions
+13 -9
View File
@@ -7,11 +7,16 @@ import requests
import json
import logging
import os
# from utils import set_local_logger
import utils
from utils import GlobalState
# Create a logger for this module
logger = logging.getLogger(__name__) # This logger will be used to log messages from this module
logger.debug("Logging level of backend logger has been configured")
# logger = logging.getLogger(__name__) # This logger will be used to log messages from this module
# logger.debug("Logging level of backend logger has been configured")
global_state = GlobalState() # Import the singleton that holds global states (e.g., logger)
logger = global_state.getLogger(__name__) # Logger for this module, inherit properties of the root logger
# llm = global_state.get_llm()
# Find out the path to current directory according to the Python interpreter (venv)
logger.debug("Current working directory: %s", os.getcwd())
@@ -31,6 +36,7 @@ def index():
logger.debug("Entering route '/'")
api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable
logger.debug("API endpoint: %s", api_endpoint)
use_model = global_state.get_llm()
with open('smartassist/src/html/client.html', 'r') as f:
client_html = f.read()
logger.debug("Client HTML (first few characters): %s", client_html[:50]) # Print to see if it's loading
@@ -114,12 +120,10 @@ def smartassist():
return jsonify({"response": response})
def get_response(user_query):
# Create a client object for interacting with OLLAMA API
client = Client()
# Generate and retrieve the response based on user's query
response = client.generate_response(user_query)
# Return the generated response
return response
client = Client() # Create a client object for interacting with OLLAMA API
response = client.generate_response(user_query) # Generate and retrieve the response based on user's query
return response
def run_flask(fport=5005):
"""
+3 -8
View File
@@ -17,16 +17,11 @@
<!-- Get the apiEndpoint -->
<script>
const apiEndpoint = window.apiEndpoint;
// Debugging log messages
// if (apiEndpoint) {
// console.log("client.html - apiEndpoint: ", apiEndpoint);
// }
// else {
// console.log("client.html - cannot find apiEndpoint");
// }
const useModel = window.useModel;
</script>
<!-- Get the javascript handling communication with the backene -->
<!-- Get the javascript handling communication with the backend -->
<script src="/js/frontend.js"></script>
<script>
+1 -1
View File
@@ -14,7 +14,7 @@ function sendMessage() {
fetch(`${apiEndpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, model: "phi3:mini" }), // Add these parameters here
body: JSON.stringify({ query, model: `${useModel}` }), // Add these parameters here
// body: JSON.stringify({ query, url_server: "http://your-custom-url", model: "phi3:mini" }), // Add these parameters here
})
.then(response => response.json())
+3
View File
@@ -18,9 +18,12 @@
<script>
// Extract apiEndpoint for use in your frontend code...
const apiEndpoint = '{{ api_endpoint }}'; // Templating syntax (Jinja2)
const useModel = '{{ use_model }}'; // Templating syntax (Jinja2)
// Tell the iframe about the apiEndpoing
document.getElementById('client-frame').contentWindow.apiEndpoint = apiEndpoint;
document.getElementById('client-frame').contentWindow.useModel = useModel;
console.log("index.html - API Endpoint: ", apiEndpoint);
console.log("index.html - use model: ", useModel);
</script>
<!-- Responsive scaling and some padding -->