诸神缄默不语-个人CSDN博文目录
又不是不能用.jpg
http://47.113.197.198:8000/chat
集成代码可参考:花月与剑/scholar_ease
之所以先用源1.0,一是因为我API都申请了,不用白不用;二是因为源1.0可以直接用国内网络连接,所以分享过程中没有法律风险。
文章目录
- 1. 安装环境配置
- 2. 前后台代码编写
- 3. 未完成的工作
- 4. 其他参考资料
1. 安装环境配置
- 白嫖了阿里云的学生计划服务器:https://developer.aliyun.com/plan/student
这个只要用支付宝认证学生就可以直接获得1个月的免费云服务器,然后完成一个非常简单的课程与考试就能再获得6个月的。 - 先按照这个教程开一下远程登录服务器的账号和密码:通过密码或密钥认证登录Linux实例
- 根据这个教程(Linux系统实例快速入门)的步骤2和4设置开一下端口和Apache服务
- 我个人习惯用VSCode,所以我就是用VSCode连的。可以用ssh,也可以用账号密码直接登录root账号
- 然后开一个新的用户账号:
useradd user_name
设置密码 - 之所以不能直接用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
- 用新账号进
/home/user_name
,新建虚拟环境:
pip3 install --user pipx
python -m pipx install virtualenv
python -m pipx ensurepath
cd 放包的路径
virtualenv venv_name
source bin/activate
- 安装所需要的工具包:
pip install flask
yum -y install httpd-devel
sudo yum makecache
pip install mod_wsgi
pip install pytz
pip install requests
yum install tmux
- 配置Apache端口转发(但这玩意得有域名了才用得上,但我的域名还没有备案):Apache如何配置端口转发_linux 的 apache 如何端口跳转_Leon Jens的博客-CSDN博客 apache 80端口转发 - 掘金 apache配置端口转发的具体方法-Apache-PHP中文网 配置文件(httpd.conf)的路径及辅助配置文件的读取 | Apache Apache配置文件httpd.conf详解 - 简书 CentOS 7 Apache httpd 管理 - 阿笨哥
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. 未完成的工作
- 备案:https://beian.aliyun.com/
- 数据库
- 登录功能
4. 其他参考资料
- gcc: fatal error: cannot read spec file ‘/usr/lib/rpm/redhat/redhat-hardened-cc1 · Issue #167 · chrippa/ds4drv
- c++ - G++ error:/usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory - Stack Overflow
- How To Install redhat-rpm-config on CentOS 7 | Installati.one
- Apache mod_wsgi模块简介_易生一世的博客-CSDN博客
- python - Could not find a version that satisfies the requirement - Stack Overflow
- Python3.5.1 => RuntimeError: The ‘apxs’ command appears not to be installed or is not executable · Issue #136 · GrahamDumpleton/mod_wsgi
- https://pypi.org/project/mod-wsgi/
- mod_wsgi — Flask Documentation (2.2.x)
- 偷个懒,公号抠腚早报80%自动化——3.Flask速成大法(上)-阿里云开发者社区
- 2021-11-16 flask部署在windows云服务器上进行公网访问_flask外网访问本地server_初冬的早晨的博客-CSDN博客
- Flask 报错:WARNING: This is a development server. Do not use it in a production deployment._吴家健ken的博客-CSDN博客
- 将flask程序部署在apache上_flask部署到apache_小鹰丶的博客-CSDN博客
- 第 3 章:模板 - Flask 入门教程
- Python:Flask简单实现统计网站访问量_python统计网站访问量_wangzirui32的博客-CSDN博客:也是本网站中采用的方案
- flask 计数器_afn2826的博客-CSDN博客:只能维持在session内
- command-not-found.com – tmux
mod_wsgi-express start-server -h ↩︎