File size: 2,210 Bytes
a2b1dcd
 
 
 
a0722f3
a2b1dcd
 
a0722f3
a2b1dcd
a0722f3
a2b1dcd
 
a0722f3
 
a2b1dcd
a0722f3
 
a2b1dcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0722f3
 
a2b1dcd
 
a0722f3
 
a2b1dcd
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 flask import Flask, request, jsonify, Response
import subprocess
import json
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

app = Flask(__name__)

@app.route('/completions', methods=['POST'])
def get_completion():
    try:
        data = request.get_json()
        if not data or 'prompt' not in data:
            return jsonify({"error": "Missing 'prompt' in request body"}), 400

        prompt = data['prompt']

        def generate():
            try:
                process = subprocess.Popen(
                    ["ollama", "run", "llama2", "-p", prompt],  # Replace "llama2" with your model
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    text=True,
                    encoding="utf-8"
                )

                for line in process.stdout:
                    try:
                        json_line = json.loads(line)
                        if "response" in json_line:
                            yield f"data: {json.dumps({'text': json_line['response']})}\n\n"
                        elif "done" in json_line: #check for done message
                            break
                    except json.JSONDecodeError:
                        logger.warning(f"Invalid JSON line from Ollama: {line.strip()}")

                stderr = process.stderr.read()
                if stderr:
                    logger.error(f"Ollama stderr: {stderr}")

                process.wait()

            except FileNotFoundError:
                yield f"data: {json.dumps({'text': 'Error: Ollama not found. Is it installed and in your PATH?'})}\n\n"
            except Exception as e:
                logger.exception("Error in Ollama subprocess:")
                yield f"data: {json.dumps({'text': f'Error in Ollama: {e}'})}\n\n"

        return Response(generate(), mimetype='text/event-stream')

    except Exception as e:
        logger.exception("Error in /completions route:")
        return jsonify({"error": "An error occurred during processing."}), 500

if __name__ == "__main__":
    app.run(debug=False, host='0.0.0.0', port=5000)