• AI大模型-流式处理 (以百度接口为例)


    No bb , show code

    效果

    后端代码

    1. from flask import Flask, request, Response
    2. import json
    3. import requests
    4. from flask_cors import CORS
    5. app = Flask(__name__)
    6. CORS(app) # Enable CORS for all routes
    7. def get_access_token(ak, sk):
    8. auth_url = "https://aip.baidubce.com/oauth/2.0/token"
    9. resp = requests.get(auth_url, params={"grant_type": "client_credentials", "client_id": ak, 'client_secret': sk})
    10. return resp.json().get("access_token")
    11. def get_stream_response(prompt):
    12. ak = "你的AK"
    13. sk = "你的SK"
    14. source = "&sourceVer=0.0.1&source=app_center&appName=streamDemo"
    15. # ERNIE-Bot-turbo
    16. base_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant"
    17. # ERNIE-Bot 4.0
    18. # base_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro"
    19. # base_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/plugin/57wt8rb5b3kzhnrd"
    20. url = base_url + "?access_token=" + get_access_token(ak, sk) + source
    21. data = {
    22. "messages": [{"role": "user", "content": prompt}],
    23. "stream": True
    24. }
    25. payload = json.dumps(data)
    26. headers = {'Content-Type': 'application/json'}
    27. return requests.post(url, headers=headers, data=payload, stream=True)
    28. def gen_stream(prompt):
    29. response = get_stream_response(prompt)
    30. for chunk in response.iter_lines():
    31. chunk = chunk.decode("utf8")
    32. if chunk[:5] == "data:":
    33. chunk = chunk[5:]
    34. yield chunk
    35. @app.route('/eb_stream', methods=['POST'])
    36. def eb_stream():
    37. body = request.json
    38. prompt = body.get("prompt")
    39. return Response(gen_stream(prompt), mimetype='text/event-stream')
    40. if __name__ == '__main__':
    41. app.run(host='0.0.0.0', port=8000)

    前端代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Sampletitle>
    6. head>
    7. <body>
    8. <label for="textInput">Prompt:label>
    9. <input type="textarea" id="textInput" placeholder="您有什么问题">
    10. <button onclick="run_prompt()">执行promptbutton>
    11. <p><textarea id="answer" rows="10" cols="50" readonly>textarea>p>
    12. <script>
    13. current_text = document.getElementById('answer');
    14. text = "";
    15. char_index = 0
    16. function run_prompt() {
    17. var inputValue = document.getElementById('textInput').value;
    18. document.getElementById('answer').value = "";
    19. // 调用服务端的流式接口, 修改为自己的服务器地址和端口号
    20. fetch('http://127.0.0.1:8000/eb_stream', {
    21. method: 'post',
    22. headers: {'Content-Type': 'application/json'},
    23. body: JSON.stringify({'prompt': inputValue})
    24. })
    25. .then(response => {
    26. return response.body;
    27. })
    28. .then(body => {
    29. const reader = body.getReader();
    30. const decoder = new TextDecoder();
    31. function read() {
    32. return reader.read().then(({ done, value }) => {
    33. if (done) { // 读取完成
    34. return;
    35. }
    36. data = decoder.decode(value, { stream: true });
    37. text += JSON.parse(data).result;
    38. type(); // 打字机效果输出
    39. return read();
    40. });
    41. }
    42. return read();
    43. })
    44. .catch(error => {
    45. console.error('发生错误:', error);
    46. });
    47. }
    48. function type() {
    49. let enableCursor = true; // 启用光标效果
    50. if (char_index < text.length) {
    51. let txt = document.getElementById('answer').value;
    52. let cursor = enableCursor ? "|" : "";
    53. if (enableCursor && txt.endsWith("|")) {
    54. txt = txt.slice(0, -1);
    55. }
    56. document.getElementById('answer').value = txt + text.charAt(char_index) + cursor;
    57. char_index++;
    58. setTimeout(type, 1000/5); // 打字机速度控制, 每秒5个字
    59. }
    60. }
    61. script>
    62. body>
    63. html>

  • 相关阅读:
    机器学习笔记 - 使用机器学习进行鸟类物种分类
    CSS 常用样式 显示模式
    MySQL学习笔记:锁2
    微软:Octo Tempest是最危险的金融黑客组织之一
    c++新特性 语言运行期强化
    【前端面试题】
    基于c++的简易web服务器搭建(初尝socket编程)
    牛客-模拟、枚举、贪心 2022.11.15
    抵御风险网站防攻击,国产浏览器能做的有很多
    Swift的NSClassFromString转换
  • 原文地址:https://blog.csdn.net/langchao7946/article/details/136345519