Can you give me an example of a python script that opens an api point and serves a string?
- User
Can you give me an example of a python script that opens an api point and serves a string?
- Assistant
Here's a simple example of a Flask server in Python which creates an endpoint which sends a string back to a user:
from flask import Flask, request app = Flask(__name__) @app.route('/', methods=['POST']) def echo(): request_body = request.get_json(force=True) return request_body['input'] app.run(port=3000)This application can be tested with curl as:
curl -X POST -H 'Content-Type: application/json' -d '{"input": "Hello server"}' http://127.0.0.1:3000/ - User
Instead of returning the request body, could you make it so it encrypts the request body with the caesar cipher and returns the cipher text?
- Assistant
Sure!
from flask import Flask, request app = Flask(__name__) @app.route('/', methods=['POST']) def encrypt(): request_body = request.get_json(force=True) input_str = request_body['input'] shift = request_body.get('shift', 1) # default shift is 1 if not specified output_str = '' for char in input_str: if char.isalpha(): char_code = ord(char) if char.isupper(): base = ord('A') else: base = ord('a') char_code = (char_code - base + shift) % 26 + base char = chr(char_code) output_str += char return output_str app.run(port=3000)This modified application also takes an optional shift parameter in the request body, which specifies the number of characters to shift by in the Caesar cipher. If the shift parameter is not specified, it defaults to 1.
You can test this modified application with curl as:
curl -X POST -H 'Content-Type: application/json' -d '{"input": "Hello server", "shift": 5}' http://127.0.0.1:3000/
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)