在云服务器上部署简单的聊天机器人网站(源1.0接口版)

诸神缄默不语-个人CSDN博文目录

http://www.scholarease.top/chat

(我后来更新了其他功能,在本博文中没有涵盖)

在这里插入图片描述

1. 安装环境配置

↓记得每次改完Apache配置文件要重启Apache:sudo systemctl restart httpd

  1. 白嫖了阿里云的学生计划服务器:https://developer.aliyun.com/plan/student
    这个只要用支付宝认证学生就可以直接获得1个月的免费云服务器,然后完成一个非常简单的课程与考试就能再获得6个月的。
  2. 先按照这个教程开一下远程登录服务器的账号和密码:通过密码或密钥认证登录Linux实例
  3. 根据这个教程(Linux系统实例快速入门)的步骤2和4设置开一下端口和Apache服务
  4. 我个人习惯用VSCode,所以我就是用VSCode连的。可以用ssh,也可以用账号密码直接登录root账号
  5. 然后开一个新的用户账号:
    useradd user_name
    设置密码
  6. 之所以不能直接用root,是因为Apache不能用root账号,否则会报错:Error:\tApache has not been designed to serve pages while\n\trunning as root. There are known race conditions that\n\twill allow any local user to read any file on the system.\n\tIf you still desire to serve pages as root then\n\tadd -DBIG_SECURITY_HOLE to the CFLAGS env variable\n\tand then rebuild the server.\n\tIt is strongly suggested that you instead modify the User\n\tdirective in your httpd.conf file to list a non-root\n\tuser.\n
  7. 用新账号进/home/user_name,新建虚拟环境:
pip3 install --user pipx
python -m pipx install virtualenv
python -m pipx ensurepath
cd 放包的路径
virtualenv venv_name
source bin/activate
  1. 安装所需要的工具包:
    pip install flask
    yum -y install httpd-devel
    sudo yum makecache
    pip install mod_wsgi
    pip install pytz
    pip install requests
    yum install tmux
    
    mod_wsgi复杂一点:
    wget https://github.com/GrahamDumpleton/mod_wsgi/archive/refs/tags/4.9.4.tar.gz
    tar 4.9.4.tar.gz
    cd mod_wsgi-4.9.4
    ./configure --with-apxs=/usr/bin/apxs --with-python=env_path/bin/python
    make
    make install
    
    这个apxs路径可以通过whereis apxs查询得到
    在/etc/httpd/conf/httpd.conf里加上LoadModule wsgi_module modules/mod_wsgi.so(要加在WSGI其他内容出现之前)
  2. 配置Apache端口转发:
    在/etc/httpd/conf/httpd.conf中增加如下内容:
<VirtualHost *:80>
    ServerName 域名
    ProxyPreserveHost On
    ProxyRequests Off
    ProxyPass / http://公网IP地址:8000/
    ProxyPassReverse / http://公网IP地址:8000/
</VirtualHost>

ServerName localhost

2. 前后台代码编写

测试时直接运行get_input.py,正式上线时在tmux中挂起mod_wsgi-express start-server wsgi.py --processes 4
(如果想调整端口,设置port参数1

get_input.py

from flask import Flask,request,render_template
from chatbot_reply import yuan10_result
import datetime,json

app = Flask(__name__)

today_date=datetime.datetime.today()

@app.route("/")
def get_home():
    return "Hello world"

@app.route('/chat',methods={'GET','POST'})
def chat():
    if request.method=='POST':
        total_yuan10_remain=((today_date-datetime.datetime.strptime('2023-5-26',"%Y-%m-%d")).days+1)*323  #放在前面似乎会导致不每天更新的问题

        if json.load(open('counter.json'))['yuan1.0_used']>total_yuan10_remain:
            return render_template('chat.html',chat_output='今天的试用次数已到达上限,请明日再来!',
                                   remain_token_counter=total_yuan10_remain-json.load(open('counter.json'))['yuan1.0_used'])
        
        user_input=request.form['user_input']
        counter=json.load(open('counter.json'))
        counter['yuan1.0_used']+=1
        json.dump(counter,open('counter.json','w'),ensure_ascii=False)
        return render_template('chat.html',chat_output='回复:'+yuan10_result(user_input),
                               remain_token_counter=total_yuan10_remain-json.load(open('counter.json'))['yuan1.0_used'])
    else:
        return render_template('chat.html',remain_token_counter=total_yuan10_remain-json.load(open('counter.json'))['yuan1.0_used'])

if __name__ == "__main__":
    app.run()

wsgi.py

from get_input import app

application = app

chatbot_reply/yuan10.py

from datetime import datetime
import pytz,os,hashlib,requests,json,time,uuid

SUBMIT_URL = "http://api-air.inspur.com:32102/v1/interface/api/infer/getRequestId?"
REPLY_URL = "http://api-air.inspur.com:32102/v1/interface/api/result?"

def code_md5(str):
    code=str.encode("utf-8")
    m = hashlib.md5()
    m.update(code)
    result= m.hexdigest()
    return result

def header_generation():
    """Generate header for API request."""
    t = datetime.now(pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d")
    global ACCOUNT, PHONE
    ACCOUNT, PHONE = os.environ.get('YUAN_ACCOUNT').split('||')
    token=code_md5(ACCOUNT+PHONE+t)
    headers = {'token': token}
    return headers

def rest_get(url, header,timeout, show_error=False):
    '''Call rest get method'''
    try:
        response = requests.get(url, headers=header,timeout=timeout, verify=False)
        return response
    except Exception as exception:
        if show_error:
            print(exception)
        return None

def submit_request(query,temperature,topP,topK,max_tokens,engine, frequencyPenalty,responsePenalty,noRepeatNgramSize):
    """Submit query to the backend server and get requestID."""
    headers=header_generation()
    url=SUBMIT_URL + "engine={0}&account={1}&data={2}&temperature={3}&topP={4}&topK={5}&tokensToGenerate={6}" \
                     "&type={7}&frequencyPenalty={8}&responsePenalty={9}&noRepeatNgramSize={10}".\
        format(engine,ACCOUNT,query,temperature,topP,topK, max_tokens,"api", frequencyPenalty,responsePenalty,noRepeatNgramSize)
    response=rest_get(url,headers,30)
    response_text = json.loads(response.text)
    if  response_text["flag"]:
        requestId = response_text["resData"]
        return requestId
    else:
        raise  RuntimeWarning(response_text)

def reply_request(requestId,cycle_count=5):
    """Check reply API to get the inference response."""
    url = REPLY_URL + "account={0}&requestId={1}".format(ACCOUNT, requestId)
    headers=header_generation()
    response_text= {"flag":True, "resData":None}
    for i in range(cycle_count):
        response = rest_get(url, headers, 30, show_error=True)
        response_text = json.loads(response.text)
        if response_text["resData"] != None:
            return response_text
        if response_text["flag"] == False and i ==cycle_count-1:
            raise  RuntimeWarning(response_text)
        time.sleep(3)
    return response_text

#Yuan类
class Yuan:
    """The main class for a user to interface with the Inspur Yuan API.
    A user can set account info and add examples of the API request.
    """

    def __init__(self, 
                engine='base_10B',
                temperature=0.9,
                max_tokens=100,
                input_prefix='',
                input_suffix='\n',
                output_prefix='答:',
                output_suffix='\n\n',
                append_output_prefix_to_query=False,
                topK=1,
                topP=0.9,
                frequencyPenalty=1.2,
                responsePenalty=1.2,
                noRepeatNgramSize=2):
        
        self.examples = {}
        self.engine = engine
        self.temperature = temperature
        self.max_tokens = max_tokens
        self.topK = topK
        self.topP = topP
        self.frequencyPenalty = frequencyPenalty
        self.responsePenalty = responsePenalty
        self.noRepeatNgramSize = noRepeatNgramSize
        self.input_prefix = input_prefix
        self.input_suffix = input_suffix
        self.output_prefix = output_prefix
        self.output_suffix = output_suffix
        self.append_output_prefix_to_query = append_output_prefix_to_query
        self.stop = (output_suffix + input_prefix).strip()

    def add_example(self, ex):
        """Add an example to the object.
        Example must be an instance of the Example class."""
        assert isinstance(ex, Example), "Please create an Example object."
        self.examples[ex.get_id()] = ex

    def delete_example(self, id):
        """Delete example with the specific id."""
        if id in self.examples:
            del self.examples[id]

    def get_example(self, id):
        """Get a single example."""
        return self.examples.get(id, None)

    def get_all_examples(self):
        """Returns all examples as a list of dicts."""
        return {k: v.as_dict() for k, v in self.examples.items()}

    def get_prime_text(self):
        """Formats all examples to prime the model."""
        return "".join(
            [self.format_example(ex) for ex in self.examples.values()])

    def get_engine(self):
        """Returns the engine specified for the API."""
        return self.engine

    def get_temperature(self):
        """Returns the temperature specified for the API."""
        return self.temperature

    def get_max_tokens(self):
        """Returns the max tokens specified for the API."""
        return self.max_tokens

    def craft_query(self, prompt):
        """Creates the query for the API request."""
        q = self.get_prime_text(
        ) + self.input_prefix + prompt + self.input_suffix
        if self.append_output_prefix_to_query:
            q = q + self.output_prefix

        return q

    def format_example(self, ex):
        """Formats the input, output pair."""
        return self.input_prefix + ex.get_input(
        ) + self.input_suffix + self.output_prefix + ex.get_output(
        ) + self.output_suffix
    
    def response(self, 
                query,
                engine='base_10B',
                max_tokens=20,
                temperature=0.9,
                topP=0.1,
                topK=1,
                frequencyPenalty=1.0,
                responsePenalty=1.0,
                noRepeatNgramSize=0):
        """Obtains the original result returned by the API."""

        try:
            # requestId = submit_request(query,temperature,topP,topK,max_tokens, engine)
            requestId = submit_request(query, temperature, topP, topK, max_tokens, engine, frequencyPenalty,
                                       responsePenalty, noRepeatNgramSize)
            response_text = reply_request(requestId)
        except Exception as e:
            raise e
        
        return response_text


    def del_special_chars(self, msg):
        special_chars = ['<unk>', '<eod>', '#', '▃', '▁', '▂', ' ']
        for char in special_chars:
            msg = msg.replace(char, '')
        return msg


    def submit_API(self, prompt, trun=[]):
        """Submit prompt to yuan API interface and obtain an pure text reply.
        :prompt: Question or any content a user may input.
        :return: pure text response."""
        query = self.craft_query(prompt)
        res = self.response(query,engine=self.engine,
                            max_tokens=self.max_tokens,
                            temperature=self.temperature,
                            topP=self.topP,
                            topK=self.topK,
                            frequencyPenalty = self.frequencyPenalty,
                            responsePenalty = self.responsePenalty,
                            noRepeatNgramSize = self.noRepeatNgramSize)
        if 'resData' in res and res['resData'] != None:
            txt = res['resData']
        else:
            txt = '模型返回为空,请尝试修改输入'
        # 单独针对翻译模型的后处理
        if self.engine == 'translate':
            txt = txt.replace(' ##', '').replace(' "', '"').replace(": ", ":").replace(" ,", ",") \
                .replace('英文:', '').replace('文:', '').replace("( ", "(").replace(" )", ")")
        else:
            txt = txt.replace(' ', '')
        txt = self.del_special_chars(txt)

        # trun多结束符截断模型输出
        if isinstance(trun, str):
            trun = [trun]
        try:
            if trun != None and isinstance(trun, list) and  trun != []:
                for tr in trun:
                    if tr in txt and tr!="":
                        txt = txt[:txt.index(tr)]
                    else:
                        continue
        except:
            return txt
        return txt

def set_yuan_account(user, phone):
    os.environ['YUAN_ACCOUNT'] = user + '||' + phone

class Example:
    """ store some examples(input, output pairs and formats) for few-shots to prime the model."""
    def __init__(self, inp, out):
        self.input = inp
        self.output = out
        self.id = uuid.uuid4().hex

    def get_input(self):
        """return the input of the example."""
        return self.input

    def get_output(self):
        """Return the output of the example."""
        return self.output

    def get_id(self):
        """Returns the unique ID of the example."""
        return self.id

    def as_dict(self):
        return {
            "input": self.get_input(),
            "output": self.get_output(),
            "id": self.get_id(),
        }

# 1. set account
set_yuan_account("user_name", "mobile_phone")

yuan = Yuan(input_prefix="对话:“",
            input_suffix="”",
            output_prefix="答:“",
            output_suffix="”",
            max_tokens=300)
# 3. add examples if in need.
#yuan.add_example(Example(inp="我有10元钱,花了1元钱,我还剩多少钱?",
#                        out="9元"))

def yuan10_result(prompt,trun="”"):
    return yuan.submit_API(prompt,trun)[1:]

templates/chat.html

<!DOCTYPE html>
<html>
<body>

<h2>Chat with 源1.0</h2>
每天晚21:00左右更新网页,会有一段时间不能用
今日剩余使用次数:{{remain_token_counter}}

<form action="/chat" method="post">
  <label for="user_input">请输入您想问源1.0的内容:</label><br>
  <input type="text" id="user_input" name="user_input"><br>
  <input type="submit" value="Submit">
</form>

<p>{{ chat_output }}</p>

</body>
</html>

3. 未完成的工作

  1. 备案:https://beian.aliyun.com/
    总之差不多按照流程走就行,先通过阿里云进行ICP备案(工信部备案域名),然后需要在网站首页写:
    在这里插入图片描述
    然后参考这篇文章完成公安联网备案申请:https://help.aliyun.com/zh/icp-filing/user-guide/the-public-security-network-for-record-and-cancellation  备案网站是http://www.beian.gov.cn/
    公安联网备案信息填写指南:https://help.aliyun.com/zh/icp-filing/user-guide/the-public-security-network-for-the-record-information-fill-in-the-guide
  2. 数据库
  3. 登录功能

4. 其他参考资料

  1. gcc: fatal error: cannot read spec file ‘/usr/lib/rpm/redhat/redhat-hardened-cc1 · Issue #167 · chrippa/ds4drv
  2. c++ - G++ error:/usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory - Stack Overflow
  3. How To Install redhat-rpm-config on CentOS 7 | Installati.one
  4. Apache mod_wsgi模块简介_易生一世的博客-CSDN博客
  5. python - Could not find a version that satisfies the requirement - Stack Overflow
  6. Python3.5.1 => RuntimeError: The ‘apxs’ command appears not to be installed or is not executable · Issue #136 · GrahamDumpleton/mod_wsgi
  7. https://pypi.org/project/mod-wsgi/
  8. mod_wsgi — Flask Documentation (2.2.x)
  9. 偷个懒,公号抠腚早报80%自动化——3.Flask速成大法(上)-阿里云开发者社区
  10. 2021-11-16 flask部署在windows云服务器上进行公网访问_flask外网访问本地server_初冬的早晨的博客-CSDN博客
  11. Flask 报错:WARNING: This is a development server. Do not use it in a production deployment._吴家健ken的博客-CSDN博客
  12. 将flask程序部署在apache上_flask部署到apache_小鹰丶的博客-CSDN博客
  13. 第 3 章:模板 - Flask 入门教程
  14. Python:Flask简单实现统计网站访问量_python统计网站访问量_wangzirui32的博客-CSDN博客:也是本网站中采用的方案
  15. flask 计数器_afn2826的博客-CSDN博客:只能维持在session内
  16. command-not-found.com – tmux

  1. mod_wsgi-express start-server -h ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值