Skip to content

Instantly share code, notes, and snippets.

@S1M0N38
Created October 7, 2023 11:00
Show Gist options
  • Save S1M0N38/f861ca42e2899b198168e2724fadc1d8 to your computer and use it in GitHub Desktop.
Save S1M0N38/f861ca42e2899b198168e2724fadc1d8 to your computer and use it in GitHub Desktop.
dummy-ollama
import json
import time
app = Flask(__name__)
@app.route("/api/generate", methods=["POST"])
def generate_response():
assert request.json
model = request.json["model"]
prompt = request.json["prompt"]
def generate_messages():
for word in prompt.split():
response_message = {
"model": model,
"created_at": time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"response": f"{word} ",
"done": False,
}
yield json.dumps(response_message) + "\n"
time.sleep(1) # Simulate some processing time
# Final message with some no-sense additional information
final_message = {
"model": model,
"created_at": time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"context": [1, 2, 3],
"done": True,
"total_duration": 5589157167,
"load_duration": 3013701500,
"sample_count": 114,
"sample_duration": 81442000,
"prompt_eval_count": 46,
"prompt_eval_duration": 1160282000,
"eval_count": 113,
"eval_duration": 1325948000,
}
yield json.dumps(final_message) + "\n"
return Response(generate_messages(), content_type="application/json", status=200)
if __name__ == "__main__":
app.run(host="localhost", port=11434, debug=True)
@S1M0N38
Copy link
Author

S1M0N38 commented Oct 7, 2023

  • Run server with
python serve.py
  • Send HTTP request with curl
curl \
-X POST  \
-H "Content-Type: application/json" \
-d '{
  "model": "dummy-model",
  "prompt":"Why is the sky blue?"
}' \
http://localhost:11434/api/generate

-H "Content-Type: application/json" header is required for this Flask server but is not required in standard ollama API calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment