update
This commit is contained in:
		| @@ -0,0 +1,22 @@ | ||||
| from flask import Flask, request | ||||
|  | ||||
| app = Flask(__name__) | ||||
|  | ||||
| def get_response(user_input): | ||||
|     response = f"你说了'{user_input}',我想了想。" | ||||
|     return response | ||||
|  | ||||
| @app.route('/', methods=['POST']) | ||||
| def API_server(): | ||||
|     try: | ||||
|         data = request.get_json()  # 从请求的 JSON 数据中获取用户输入 | ||||
|         user_input = data.get('prompt', '')  # 获取 'prompt' 字段 | ||||
|     except Exception as e: | ||||
|         return '请求错误!请联系 API 管理员。'  # 如果解析失败,则返回错误信息 | ||||
|     if not user_input: | ||||
|         return "请求错误!请联系 API 管理员。"  # 如果没有输入,则返回错误 | ||||
|     ai_response = get_response(user_input) | ||||
|     return ai_response | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     app.run(debug=True, threaded=True, port=123) | ||||
							
								
								
									
										11
									
								
								2024.02.26_flask_example/API_server_direct/user_direct.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								2024.02.26_flask_example/API_server_direct/user_direct.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import requests | ||||
|  | ||||
| url = "http://localhost:123"  # API 地址 | ||||
| data = { | ||||
|     "prompt": "Hello, how are you?"  # 请求数据,prompt 为用户输入 | ||||
| }  | ||||
| response = requests.post(url, json=data)  # 发送 POST 请求,传递 JSON 数据 | ||||
| if response.status_code == 200:  # 检查响应是否成功 | ||||
|     print(response.text)  # 直接获取并打印返回的完整响应 | ||||
| else: | ||||
|     print(f"请求失败,状态码: {response.status_code}") | ||||
| @@ -0,0 +1,24 @@ | ||||
| from flask import Flask, Response, request | ||||
|  | ||||
| app = Flask(__name__) | ||||
|  | ||||
| def get_response(user_input): | ||||
|     import time | ||||
|     ai_response = f"你说了'{user_input}',我想了想。" | ||||
|     for char in ai_response: | ||||
|         yield f"{char}\n\n" | ||||
|         time.sleep(0.2) | ||||
|  | ||||
| @app.route('/', methods=['POST']) | ||||
| def API_server(): | ||||
|     try: | ||||
|         data = request.get_json() # 从请求的 JSON 数据中获取用户输入 | ||||
|         user_input = data.get('prompt', '')  # 获取 'prompt' 字段 | ||||
|     except Exception as e: | ||||
|         return '请求错误!请联系 API 管理员。' # 如果解析失败,则返回错误信息 | ||||
|     if not user_input: | ||||
|         return "请求错误!请联系 API 管理员。" # 如果没有输入,则返回错误 | ||||
|     return Response(get_response(user_input), content_type='text/event-stream') # 返回流式响应 | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     app.run(debug=True, threaded=True, port=123) | ||||
							
								
								
									
										13
									
								
								2024.02.26_flask_example/API_server_stream/user_stream.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								2024.02.26_flask_example/API_server_stream/user_stream.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| import requests | ||||
|  | ||||
| url = "http://localhost:123" # API 地址 | ||||
| data = { | ||||
|     "prompt": "Hello, how are you?"   # 请求数据,prompt 为用户输入 | ||||
| }  | ||||
| response = requests.post(url, json=data, stream=True)  # 发送 POST 请求,传递 JSON 数据并启用流式响应 | ||||
| if response.status_code == 200: # 检查响应是否成功 | ||||
|     for line in response.iter_lines(): # 逐步读取并打印流式响应 | ||||
|         if line: | ||||
|             print(line.decode('utf-8'), end='', flush=True) # 解码并打印每一行流式返回的数据 | ||||
| else: | ||||
|     print(f"请求失败,状态码: {response.status_code}") | ||||
		Reference in New Issue
	
	Block a user