Skip to content

Added a python flask api for ollama #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions ollama-api/api.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions ollama-api/readme.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions ollama-api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask
Flask-CORS
requests