File size: 1,091 Bytes
3ccfba8
 
598538d
3ccfba8
598538d
 
3ccfba8
 
598538d
 
3ccfba8
598538d
3ccfba8
598538d
 
3ccfba8
4e482de
 
 
 
 
598538d
 
4e482de
 
 
 
598538d
3ccfba8
 
 
598538d
 
 
 
3ccfba8
 
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
from flask import Flask, request, jsonify
import requests

app = Flask(__name__)
OLLAMA_API = "http://localhost:11434"

@app.route('/')
def home():
    return "Ollama API is running. Use /api/* endpoints to access Ollama."

@app.route('/api/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy(path):
    url = f"{OLLAMA_API}/{path}"
    resp = requests.request(
        method=request.method,
        url=url,
            headers={key: value for (key, value) in request.headers if key != 'Host'},
            data=request.get_data(),
            cookies=request.cookies,
            allow_redirects=False
        )
    
    excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
    headers = [
        (name, value) for (name, value) in resp.raw.headers.items()
        if name.lower() not in excluded_headers
    ]
    
    response = app.response_class(
        response=resp.content,
        status=resp.status_code,
        headers=headers,
    )
    return response

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