轻松打造一个可以切换AI平台的网站


亮色主题

暗色主题

停止按键

设置界面

浏览器缓存设置

Kimi

通义千问

ChatGPT

手机版


部分代码如下:

python 复制代码
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify, render_template, Response
import requests
import json
import os
from gevent import pywsgi
app = Flask(__name__)

@app.route("/", methods=["GET"])
def index():
    return render_template("chat.html")

@app.route("/chat", methods=["POST"])
def chat():
    platform = request.form.get("platform", "kimi")
    messages = request.form.get("prompts", None)
    apiKey = request.form.get("apiKey", None)
    model = request.form.get("model", "moonshot-v1-8k")
    if messages is None:
        return jsonify({"error": {"message": "请输入prompts!", "type": "invalid_request_error", "code": ""}})

    if apiKey is None:
        apiKey = os.environ.get('OPENAI_API_KEY',app.config["OPENAI_API_KEY"])

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {apiKey}",
    }

    # json串转对象
    prompts = json.loads(messages)

    data={}
    url = ""
    if platform=="kimi":
        data = {
            "messages": prompts,
            "model": model,
            "temperature": 0.8,
            "top_p": 0.95,
            "n": 1,
            "stream": True,
        }
        url = app.config["URL"]
    elif platform=="qwen":
        if model in ["qwen-turbo","qwen-plus","qwen-max"]:
            data = {
                "messages": prompts,
                "model": model,
                "temperature": 0.8,
                "top_p": 0.95,
                "stream": True,
                "enable_search":True
            }
        else:            
            data = {
                "messages": prompts,
                "model": model,
                "temperature": 0.8,
                "top_p": 0.95,
                "stream": True
            }
        url = app.config["URL1"]
    elif platform=="doubao":
        data = {
            "messages": prompts,
            "model": model,
            "temperature": 0.8,
            "top_p": 0.95,
            "stream": True,
        }
        url = app.config["URL2"]
    elif platform=="chatgpt":
        data = {
            "messages": prompts,
            "model": model,
            "temperature": 0.8,
            "top_p": 0.95,
            "n": 1,
            "stream": True,
        }
        url = app.config["URL3"]
    try:
        resp = requests.post(
            url=url,
            headers=headers,
            json=data,
            stream=True,
            timeout=(10, 10)  # 连接超时时间为10秒,读取超时时间为10秒
        )
    except requests.exceptions.Timeout:
        return jsonify({"error": {"message": "请求超时,请稍后再试!", "type": "timeout_error", "code": ""}})
    
    # 迭代器实现流式响应
    def generate():
        errorStr = ""
        for chunk in resp.iter_lines():
            if chunk:
                streamStr = chunk.decode("utf-8").replace("data: ", "")
                try:
                    streamDict = json.loads(streamStr)  # 如果不是正常数据,则处理可能是错误信息
                except:
                    errorStr += streamStr.strip()  # 错误流式数据累加
                    continue
                # 获取 choices 列表中的第一个元素
                delData = streamDict.get("choices", [{}])[0]
                finish_reason = delData.get("finish_reason")
                if finish_reason is not None:
                    break
                else:
                    delta = delData.get("delta", {})
                    content = delta.get("content")
                    if content:
                        yield content

        # 如果出现错误,此时错误信息迭代器已处理完,app_context已经出栈,要返回错误信息,需要将app_context手动入栈
        if errorStr != "":
            with app.app_context():
                yield errorStr

    return Response(generate(), content_type='application/event-stream')

if __name__ == '__main__':
    server = pywsgi.WSGIServer(('localhost', 5200), app)
    server.serve_forever()

部分代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="icon" href="https://down.zhaomi.cn/web/assistant/56.png">
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../static/css/github-dark-dimmed.min.css">
    <link rel="stylesheet" href="../static/css/style.css">
    <title>AI助手</title>
</head>
<body>
  <div class="container">
    <div class="row">
      <div class="box col-xs-12">
        <div class="title">
            <h2 class="text-center" id="platform_title"></h2>
        </div>
        <div class="answer">
          <div id="chatWindow"></div>
          <div class="function">
            <div class="others">
              <div class="left">
                <div class="settings common dropup">
                  <a class="dropdown-toggle icon-style" id="dropdownMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="设置">
                    <i class="fa fa-cogs fa-lg" aria-hidden="true"></i>
                  </a>
                  <div class="dropdown-menu" style="padding:0;" onclick="event.stopPropagation()">
                    <div class="settings-common">
                      <span><i class="fa fa-linode fa-lg" aria-hidden="true"></i>&nbsp; 主题</span>
                      <div class="chck-btn">
                        <input id="chck-3" type="checkbox">
                        <label for="chck-3" class="check-trail">
                        <div class="check-handler"></div>
                        </label>
                      </div>
                    </div>
                    <div class="settings-common">
                      <span><i class="fa fa fa-dashboard fa-lg" aria-hidden="true"></i>&nbsp; 平台</span>
                      <select class="form-control ipt-common platform" id="platform">
                        <option value="kimi">Kimi</option>
                        <option value="qwen">通义千问</option>
                        <option value="doubao">豆包</option>
                        <option value="chatgpt">ChatGPT</option>
                      </select>
                    </div>
                    <div class="settings-common">
                      <span><i class="fa fa-reddit-alien fa-lg" aria-hidden="true"></i>&nbsp;模型</span>
                      <select class="form-control ipt-common model" id="model">
                      </select>
                    </div>

                    <div class="settings-common">
                      <span><i class="fa fa-key fa-lg" aria-hidden="true"></i>&nbsp;Key</span>
                      <input type="password" class="form-control ipt-common api-key" placeholder="输入api key"  id="api-key-input">  
                      <i class="fa fa-eye toggle-password" style="cursor: pointer;" aria-hidden="true"></i>
                    </div>

                    <div class="settings-common">
                      <span><i class="fa fa-floppy-o fa-lg" aria-hidden="true"></i>&nbsp; 保存对话内容</span>
                      <div class="chck-btn">
                        <input id="chck-1" type="checkbox">
                        <label for="chck-1" class="check-trail">
                        <div class="check-handler"></div>
                        </label>
                      </div>
                    </div>
                    <div class="settings-common">
                      <span><i class="fa fa-retweet fa-lg" aria-hidden="true"></i>&nbsp; 开启连续对话</span>
                      <div class="chck-btn">
                        <input id="chck-2" type="checkbox">
                        <label for="chck-2" class="check-trail">
                        <div class="check-handler"></div>
                        </label>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="center">
                <div class="stop common">
                  <a class="icon-style stop-icon" title="停止响应" style="text-decoration: none;"><i class="fa fa-stop-circle-o fa-lg" aria-hidden="true"></i> 停止</a>
                </div>
              </div>
              <div class="right">
                <div class="screenshot common">
                  <a class="icon-style" title="截图保存对话"><i class="fa fa-file-image-o fa-lg" aria-hidden="true"></i></a>
                </div>
                <div class="delete common">
                  <a class="icon-style" title="删除历史记录"><i class="fa fa-trash-o fa-lg" aria-hidden="true"></i></a>
                </div>
              </div>
            </div>
            <div class="ipt">
              <div class="col-xs-12">
                <textarea id="chatInput" class="form-control" rows="1" placeholder="输入问题" ></textarea>
              </div>
              <button id="chatBtn" class="btn btn-primary" type="button">发送</button>
            </div>
          </div>
        </div>
        <footer class="foot" style="margin-top: 20px;">
          <p class="lead text-center"> 内容由 AI 大模型生成,请仔细甄别</p>
        </footer>
      </div>
    </div>
  </div>
</body>
<script src="../static/js/jquery-2.1.1.js"></script>
<script src="../static/js/bootstrap.min.js"></script>
<script src="../static/js/highlight.min.js"></script>
<script src="../static/js/marked.min.js"></script>
<script src="../static/js/html2canvas.min.js"></script>
<script src="../static/js/custom.js"></script>

</html>
相关推荐
Chef_Chen9 分钟前
从0开始学习机器学习--Day22--优化总结以及误差作业(上)
人工智能·学习·机器学习
Mr.简锋14 分钟前
opencv常用api
人工智能·opencv·计算机视觉
DevinLGT43 分钟前
6Pin Type-C Pin脚定义:【图文讲解】
人工智能·单片机·嵌入式硬件
宋一诺331 小时前
机器学习—高级优化方法
人工智能·机器学习
龙的爹23331 小时前
论文 | The Capacity for Moral Self-Correction in LargeLanguage Models
人工智能·深度学习·机器学习·语言模型·自然语言处理·prompt
Mr.简锋1 小时前
opencv视频读写
人工智能·opencv·音视频
Baihai_IDP1 小时前
「混合专家模型」可视化指南:A Visual Guide to MoE
人工智能·llm·aigc
寰宇视讯2 小时前
“津彩嘉年,洽通天下” 2024中国天津投资贸易洽谈会火热启动 首届津彩生活嘉年华重磅来袭!
大数据·人工智能·生活
Light602 小时前
低代码牵手 AI 接口:开启智能化开发新征程
人工智能·python·深度学习·低代码·链表·线性回归
墨绿色的摆渡人2 小时前
用 Python 从零开始创建神经网络(六):优化(Optimization)介绍
人工智能·python·深度学习·神经网络