alpaca.cpp
alpaca.cpp copied to clipboard
How to Interact with the Shell Outside of the Console
I made a simple web page using flask in Python but I'm having trouble sending and receiving to/from the interactive shell. I'm calling chat.exe with subprocess.run() but the output is always only the first 3 lines of chat.exe's output, or just an unknown error. I'm using -p to submit the prompt, but I can't tell if it works or not because I don't get the full output.
Any help would be very much appreciated!
Python script for reference(simplified the command to just --help, still doesn't work):
from flask import Flask, request, jsonify
import subprocess
import time
import logging
app = Flask(__name__)
# Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
html_code = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Shell</title>
<style>
#output {
background-color: blue;
padding: 10px;
color: white;
}
</style>
</head>
<body>
<textarea id="commandInput" placeholder="Enter command"></textarea>
<button onclick="executeCommand()">Execute</button>
<div id="output"></div>
<script>
function executeCommand() {
const command = document.getElementById('commandInput').value;
fetch('/execute-command', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ command: command })
})
.then(response => response.json())
.then(data => {
// Check for the 'execution_complete' signal
if (data.execution_complete) {
document.getElementById('output').innerText = data.output || data.error || "Error executing command!";
} else {
// If execution is not complete, recursively call executeCommand after a short delay
setTimeout(executeCommand, 500);
}
});
}
</script>
</body>
</html>
'''
@app.route('/')
def index():
return html_code
@app.route('/execute-command', methods=['POST'])
def execute_command():
try:
command = ['C:\\Users\\Intel NUC\\Desktop\\alpaca\\alpaca.cpp\\build\\Release\\chat.exe', '--help']
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False)
output = result.stdout if result.returncode == 0 else result.stderr
response_data = {'output': output, 'execution_complete': True}
# Log any errors to the console
if result.returncode != 0:
logging.error(f"Error executing command: {output}")
return jsonify(response_data)
except Exception as e:
# Log any exceptions to the console
logging.exception(f"An exception occurred: {str(e)}")
return jsonify({'error': str(e), 'execution_complete': True})
if __name__ == '__main__':
app.run(debug=True)
Edit: I noticed the -i, --interactive flag exists, but I'm a bit confused; isn't an interactive shell the default? I want the opposite of that so I can pipe the output somewhere.