#1 前端js脚本中代码的执行顺序问题
在实践时遇到的,一开始按照习惯性的自上而下的顺序去写,结果发现传值传不到前端表单中,在控制台中却能够找到定义的变量。
后面经过一点点debug,在代码一点点输出变量,发现问题出现在传值出现于绘制表单之后了,而不是按照写代码时自然的从上倒下的顺序。
经过上网查询学习,认识到了在发送请求的时候,如果请求较多/数据量较大/请求比较复杂的情况下,执行传值较久,而传值结束前前端已经绘制结束的问题,造成异步操作bug。
例:
1.
const handleSubmit = async() => {
console.log(blogForm.value)
if(blogForm.value.content === "")
{
console.log(blogForm.value.content)
await openAnotherDialog();
}
else if(blogForm.value.doctor === "")
{
await openOtherDialog();
}
else if(blogForm.value.content !== "" && blogForm.value.doctor !== "")
{
//显示加载界面 element-plus
loadingInstance = ElLoading.service({
fullscreen: true,
text: '正在生成病历,预计等待30秒...',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
}
)
// 在这里传给后端数据
await axios.post('http://localhost:8000/medic/generate/'+pid.value+'/', querystring.stringify({
message: blogForm.value.content,
doctor_id: blogForm.value.doctor
})).then(res => {
console.log(res.data)
theMessage.value = JSON.parse(JSON.stringify(res.data.message));
console.log(theMessage)
});
await openDialog();
await closeLoading();
}
};
使用异步函数加以避免:
async
使用 async
关键字定义一个异步函数。一个异步函数总是返回一个 Promise
对象。
await
等待异步结果:await
关键字只能在 async
函数内部使用,用于等待一个 Promise
对象的结果。await
会暂停函数的执行,直到 Promise
解析完成。
2.
load()
{
request.get("/api/books/counta").then(res =>
{
console.log(res)
this.valueas = res.data
request.get("/api/books/countb").then(res =>
{
console.log(res)
this.valuebs = res.data
// console.log(this.valuebs[1]);
const dom = this.$refs['myChart']; // 获取dom节点
const myChart = echarts.init(dom); // 初始化echarts实例
console.log(this.valuebs[1])
console.log('mounted hook is called')
const option = {
title: {
text: '各位作者入选作品数目',
left: 'center'
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: this.valueas
},
series: [
{
data: this.valuebs,
type: 'line',
label: {
show: true, // 显示标签
position: 'insideBottom', // 标签位置(可选:'top', 'insideTop', 'inside', 'insideBottom', 'insideTopLeft', 'insideBottomLeft', 'insideTopRight', 'insideBottomRight', 'left', 'right', 'topLeft', 'topRight', 'bottomLeft', 'bottomRight')
formatter: '{c}', // 标签内容格式('{c}' 表示使用数据值)
}
},
]
};
// 设置实例参数
myChart.setOption(option);
}
)
// console.log(this.valueas[1]);
}
)
},
顺便学习js中的then方法,简单明了地规定代码执行顺序。
then
方法用于注册在 Promise
成功完成时要调用的回调函数。它接收两个参数:
onFulfilled
:当 Promise 成功时要执行的回调函数。onRejected
(可选):当 Promise 失败时要执行的回调函数。
#2 大模型本地部署多重报错
pip install -r requirements.txt
这里有问题:xtuner1.20需要4.36.0及以上但是不是4.38.0等版本的transformer,这里和requirements.txt文件中要求的版本冲突,报错如下:
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. xtuner 0.1.21 requires transformers!=4.38.0,!=4.38.1,!=4.38.2,>=4.36.0, but you have transformers 4.30.2 which is incompatible.
解决方案:这里手动将transformers版本更新到4.36.0即可
下载完成后进入到大模型文件夹中,运行
python web_demo.py
又报错:
Traceback (most recent call last): File "F:\ChatGLM2-6B-main\ChatGLM2-6B-main\web_demo.py", line 7, in <module> model = AutoModel.from_pretrained("F:\\glm2", trust_remote_code=True).cuda() File "F:\Python3.0\lib\site-packages\transformers\modeling_utils.py", line 2432, in cuda return super().cuda(*args, **kwargs) File "F:\Python3.0\lib\site-packages\torch\nn\modules\module.py", line 918, in cuda return self._apply(lambda t: t.cuda(device)) File "F:\Python3.0\lib\site-packages\torch\nn\modules\module.py", line 810, in _apply module._apply(fn) File "F:\Python3.0\lib\site-packages\torch\nn\modules\module.py", line 810, in _apply module._apply(fn) File "F:\Python3.0\lib\site-packages\torch\nn\modules\module.py", line 810, in _apply module._apply(fn) File "F:\Python3.0\lib\site-packages\torch\nn\modules\module.py", line 833, in _apply param_applied = fn(param) File "F:\Python3.0\lib\site-packages\torch\nn\modules\module.py", line 918, in <lambda> return self._apply(lambda t: t.cuda(device)) File "F:\Python3.0\lib\site-packages\torch\cuda\__init__.py", line 289, in _lazy_init raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled
重点在最后一行报错,经过上网查询,这个是由于CUDA和pytorch版本冲突了
查询自己的CUDA版本为12.2,这里降到12.1(先卸载12.2然后去安装12.1)
原因为pytorch官方提供了和cuda12.1相容的版本,但是我没找到12.2的:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
安装成功之后 再次输入指令
python web_demo.py
即解决成功