步骤一:部署百川AI大模型
参考:Linux环境下部署百度百川AI大模型-基于厚德云-CSDN博客
或
Linux环境下部署百度百川AI大模型-基于ModelScope-CSDN博客
步骤二:编写服务器端服务代码
参考:基于厚德云部署的百川大模型编写Django后台服务-CSDN博客
本文章基已发布的服务http://deq1.dc.houdeyun.cn:52665/
或
基于ModelScope部署的百川大模型编写Django服务-CSDN博客
步骤三:编写model层代码
创建sourceai/model/chat/soft863/baichuan.py文件,代码如下:
import requests
import json
def chat(context):
url = "http://deq1.dc.houdeyun.cn:52665/baichuanto?context=" + context
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("GET", url, headers=headers)
print(response.text)
res = json.loads(response.text).get('res')
return res
if __name__ == '__main__':
res =chat("你是谁")
print(res)
如果用魔塔算力,因为需要与阿里云授权,需要进行登录,可通过cookie实现
import requests
import json
def chat(content):
url = "https://dsw-gateway-cn-hangzhou.data.aliyun.com/dsw-654886/proxy/8090/baichuanto?context=" + content
cookies_str = '_xsrf=2|a814f9d4|4267f7b613d41805594c948b4773ae34|1725844387; cna=ox8BH7XiWzECAQHGBsJP3GNE; _bl_uid=bwlUqy3dsm4fhj9kU6p357yzwybg; t=014f2a4f553ca6677b3b45eee0e75b4b; tongyi_guest_ticket=lrHpY4KXSfn_AYVf2l6B753u2yQcQcUoDW8u8_y$EmLKDdxyxObYf5DlAU4DoEyQKZU_7o8PPjQr0; login_aliyunid_pk=1862448785345606; login_current_pk=1862448785345606; aliyun_lang=zh; yunpk=1862448785345606; cnaui=%2522129244****%2540qq.com%2522; aui=%2522129244****%2540qq.com%2522; partitioned_cookie_flag=addPartitioned; _samesite_flag_=true; sca=4b2feaf4; login_aliyunid="129244****@qq.com"; login_aliyunid_ticket=vpeV*0*Cm58slMT1tJw3_t$$0iPqTIgm3eh3x60MjXGsSTf1DEMDqjLr7ntyGL6og2of_BNpwU_TOTNChZBoeM1KJexdfb9zhYnsN5Zos6qISCrRt7mGxbigG2Cd4fWaCmBZHIzsgdZq64XXWQgyKFfu0; login_aliyunid_csrf=_csrf_tk_1336325844385410; login_aliyunid_pks="BG+x7D6zj/IQnH4PBQ+bsHV0LulerZjkJDVa0WoxuOWDv0="; hssid=bf1f4095-c0ee-4d5d-9b44-99feedc2c0ec; hsite=6; aliyun_country=CN; aliyun_site=CN; cookie2=13cd6b87459b774ae537f865fa0e4150; _tb_token_=53f78be7e8351; atpsida=46623d98fc0479e4716d095c_1725845531_2; isg=BO7uNRIPBAelsXDHeYShh9VtP0Sw77Lp83bSgRi3YfGs-45VgHsh-Zbyt2cXI6oB; tfstk=fmdnvafD9pWCNdeGSA5Ie--dALk9Ry1Wa3FR2_IrQGS6wWFp8ufklnqKRJLFqLj9Y26J98UkqEI9w2s-ddfkfH4LpULPq3xOfQFL9eya_Z_U485FwT-kRexlF2sR4gxJzp3tMjLBR_1yqmhxMXCVK3Oh4uCr29Ln6mnxMfLBR_1rDLUfdTBGPG7F8a7rSFS1-_PFUWyabGsN4_PPz1lNRaFP835rSFS1zjP-LMOy_CoUsQ_fGHPJUwXhq7IgZygR-9X98Gmq0CmFKi8Fj7oYhieGqGOqXAY6mp-d53cmjsJXjQXVZXqf-d8c_MCqnRfHWHOcTIm39NC1AtJPI2leS6jhEQXalRCHUHOldp4SlF5efKsfLAi1SBdAEi6gbD89SMWDEHn_aMpD7hXv6lFAgKtMZt5V4Sea3DMYNNuJ_8w5LN_GDXd1Gcwj8vjESV2qP971Ri3iS8w5LN_GDV0g3b6F5wsA.'
headers = {'cookie': cookies_str}
response = requests.request("GET", url, headers=headers)
print(response.text)
tempResult = json.loads(response.text)
res = tempResult.get('res')
print(res)
return res
if __name__ == '__main__':
print(chat('你好'))
步骤四:编写控制层代码
创建sourceai/controller/chat_view.py,代码如下:
from django.shortcuts import render
from django.http import HttpResponse
from sourceai.model.chat.soft863 import baichuan
import json
# Create your views here.
def baichuan_index(request):
return render(request, 'chat/baichuan.html')
def baihcuan_to_create(request):
context = request.POST.get('context')
result = baichuan.chat(context)
return HttpResponse(json.dumps({"res": result}))
步骤五:添加映射
在soft863ai/urls.py中新增
from sourceai.controller import chat_view
path('baichuan', chat_view.baichuan_index),
path('baichuanto', chat_view.baihcuan_to_create),
步骤六:编写前端代码
创建templates/chat/baichuan.html,代码如下:
<!doctype html>
<html>
<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, maximum-scale=1.0, user-scalable=0">
<link rel="icon" href="/static/img/imgshibie.png"> <!-- 网页图标 -->
<link rel="stylesheet" type="text/css" href="/static/css/index.css"/>
<link rel="stylesheet" href="/static/css/index.css">
<link rel="stylesheet" href="/static/css/animate.css">
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script src="/static/js/typeit.min.js"></script>
<script src="/static/js/rem.js" type="text/javascript" charset="utf-8"></script>
</head>
<body style="cursor: url(img/body2.png);">
<div class="canvas" style="opacity: .2">
<iframe frameborder="0" src="/static/js/index.html" style="width: 100%; height: 100%"></iframe>
</div>
<div id="app">
<div class="header_title">智能问答系统</div>
<div class="header_time"></div>
<!-- //左侧显示信息 -->
<div class="side_left ">
<textarea id="context1"
style="width: 4.2rem;min-height: 5rem;background: rgba(255, 255, 255, 0);color:#3cf7f1;border:0;outline: none;margin-top: 1rem;margin-left:0.23rem;display: inline-block;">
</textarea>
</div>
<div class="face-capture" id="face-capture">
<div class="faceCon">
<div class="videoFaceCon">
<div class="title">点击开始</div>
<div class="imgCoverCon">
<div class="retiveCon">
<img src="/static/img/1233.png" class="retiveConImg allRoteAnmi">
<img src="/static/img/imgshibie.png" class="videoFace">
</div>
</div>
</div>
</div>
</div>
<div class="side_right">
<div class="rightCon">
<div class="textConImg"></div>
</div>
</div>
<script type="text/javascript">
window.onload = function () {
$('.side_left').show();
$('.side_right').show();
// 时间
var timer = '';
var taker = '';
timer && clearInterval(_this.timer);
timer = setInterval(function () {
var show_day = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
var nowtime = new Date();
var year = nowtime.getFullYear();
var month = nowtime.getMonth() + 1;
var date = nowtime.getDate();
var day = nowtime.getDay();
var hour = nowtime.getHours();
var minutes = nowtime.getMinutes();
var second = nowtime.getSeconds();
month < 10 ? month = '0' + month : month;
hour < 10 ? hour = '0' + hour : hour;
minutes < 10 ? minutes = '0' + minutes : minutes;
second < 10 ? second = '0' + second : second;
var show_day_s = day === 0 ? show_day[6] : show_day[day - 1];
// _this.mytime = year + "年" + month + "月" + date + "日 " + show_day_s+ nowtime.toLocaleTimeString('chinese', { hour12: false });
var mytimer = year + "年" + month + "月" + date + "日" + show_day_s + ' ' +
hour + ':' + minutes;
$('.header_time').html(mytimer)
}, 1000);
//时间
function goanimation() {
taker = '';
taker = new TypeIt('.rightCon', {
lifeLike: true,
cursorSpeed: 1000,
waitUntilVisible: false,
speed: 100
}).go();
}
$('.videoFaceCon').click(() => {
log = $("#context1").val()
$('.title').html('正在创作..');
//创建一个存储表单数据的对象
var fd = new FormData();
fd.append("context", log);
fd.append("csrfmiddlewaretoken", '{{ csrf_token }}')
$.ajax({
url: '/baichuanto',
type: 'post',
data: fd,
dataType: 'json',
cache: false,
processData: false,
contentType: false,
success: function (obj) {
console.log(obj)
console.log(obj.res)
$('.textConImg').text(obj.res)
$('.title').html('恭喜您,创作成功!')
$('.rightCon').show()
$('.rightCon').addClass('fadeInRightBig');
goanimation();
taker.reset();
}
})
})
}
</script>
</body>
</html>
步骤七:启动与调试
http://127.0.0.1:8000/baichuan