diff --git a/ollama-api/api.py b/ollama-api/api.py new file mode 100644 index 0000000..4494090 --- /dev/null +++ b/ollama-api/api.py @@ -0,0 +1,49 @@ +from flask import Flask, request, jsonify +from flask_cors import CORS +import requests +import json + +app = Flask(__name__) +CORS(app) + +def call_ollama_api(input_data): + url = "http://localhost:11434/api/generate" + + model = input_data.get("model") + stream = input_data.get("stream", False) + messages = input_data.get("messages", []) + + system_message = next((msg['content'] for msg in messages if msg['role'] == 'system'), "") + user_message = next((msg['content'] for msg in messages if msg['role'] == 'user'), "") + images = next((msg.get('images', []) for msg in messages if msg['role'] == 'user'), []) + + payload = { + "model": model, + "prompt": f"{system_message}\n\n{user_message}", + "stream": stream, + "images": images + } + + response = requests.post(url, data=json.dumps(payload)) + + if response.status_code == 200: + response_json = response.json() + print("Ollama API Response:", response_json) + return response_json.get('response', {}) + else: + error_response = { + "error": f"Request failed with status code: {response.status_code}", + "response": response.text + } + print("Ollama API Error Response:", error_response) + return error_response + +@app.route('/', methods=['POST']) +def generate(): + input_data = request.json + print("input_data:", input_data) + result = call_ollama_api(input_data) + return jsonify(result) + +if __name__ == "__main__": + app.run(host='0.0.0.0', port=5000) diff --git a/ollama-api/readme.md b/ollama-api/readme.md new file mode 100644 index 0000000..8335d61 --- /dev/null +++ b/ollama-api/readme.md @@ -0,0 +1,24 @@ +This is just a flask api that passes the info to ollama + +Required packages are +``` +Flask +Flask-CORS +requests +``` + +Simply use +``` +pip -r requirements.txt +``` + +then run it with +``` +python api.py +``` + +the api url will either be +``` +http://127.0.0.1:5000 +``` +or your local pc on the port 5000 \ No newline at end of file diff --git a/ollama-api/requirements.txt b/ollama-api/requirements.txt new file mode 100644 index 0000000..c2a93fc --- /dev/null +++ b/ollama-api/requirements.txt @@ -0,0 +1,3 @@ +Flask +Flask-CORS +requests \ No newline at end of file