背景:简单的通过API或者SDK在线调用阿里云大模型(基于百炼平台),基于在线知识库
参考地址:安装阿里云百炼SDK_大模型服务平台百炼(Model Studio)-阿里云帮助中心 (aliyun.com)
1、获取API-KEY
当您通过API/SDK调用大模型或应用时,需要获取API-KEY作为调用时的鉴权凭证。
(1)地址:https://bailian.console.aliyun.com/?spm=a2c4g.11186623.0.0.d76a60beZohIcg#/home
(2) 右上角用户头像点击,然后获取api-key
(3)配置api-key到系统环境变量(也可以在调用接口中直接使用api_key)
需要再powerShell中配置
a. [Environment]::SetEnvironmentVariable("DASHSCOPE_API_KEY", "YOUR_DASHSCOPE_API_KEY", [EnvironmentVariableTarget]::User)
b. echo $env:DASHSCOPE_API_KEY
配置完环境变量后一定一定要重启电脑,否则不生效!!!!!!!
2、获取SDK
(1)本地java环境需要在java8及以上
(2)引入pom依赖,地址:https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
3、创建知识库
step1: 数据管理-导入数据
在数据中心,选择“默认类目”后,点击“导入数据”,然后通过“本地上传”的方式导入数据在数据中心,选择“默认类目”后,点击“导入数据”,然后通过“本地上传”的方式导入数据
step2:创建知识索引 (知识库)
在数据应用-知识索引页面,创建知识库,输入知识库描述,选择推荐配置即可。在数据应用-知识索引页面,创建知识库,输入知识库描述,选择推荐配置即可。
step3:导入数据到知识库
4、创建应用,开通调用模型
主要目的:获取app_id
进入我的应用后,点击新增应用。然后在应用配置中,进行以下操作:
选择模型。同时,还支持配置与模型生成内容相关的参数,例如,温度系数等。
开启“知识检索增强”。
选择知识库,即在Step2中创建的知识索引。
点击“保存并发布”按钮。
5、通过api进行调用(本次demo只是简单的对话)
(1)前端主要代码
seeAlibabaChat() {
if (window.EventSource) {
const ssehost = process.env.VUE_APP_BASE_API
const eventSource = new EventSource(`${window.SITE_CONFIG.alisseurl}/alisse/createSseConnect?clientId=${this.$cookie.get('token')}`)
console.log(eventSource)
eventSource.onmessage = event => {
console.log(`onmessage:: ${event.data}`)
if (event.data === 'send start') {
const happyEnding = {
type: 'leftinfo',
time: this.getTodayTime(),
content: '',
question: []
}
this.info.push(happyEnding)
} else if (event.data === 'send end') {
this.customerText = ''
this.disableFlag = false
this.$nextTick(() => {
const contentHeight = document.getElementById('content')
contentHeight.scrollTop = contentHeight.scrollHeight
})
} else {
const messObj = this.info[this.info.length - 1]
messObj.content = messObj.content.concat(event.data)
this.info[this.info.length - 1] = messObj
}
}
eventSource.onopen = event => {
console.log(`onopen:: ${event}`)
}
eventSource.onerror = event => {
console.log(`onerror :: ${event}`)
}
eventSource.close = event => {
console.log(`close :: ${event}`)
}
} else {
console.log('你的浏览器不支持SSE~')
}
console.log(' 测试 打印')
},
(2)后端调用(官网都有,只是改造成了sse方式调用)
@GetMapping("/aliModel/chat")
public void alibabChat(@RequestParam String clientId,String qMes) throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
.apiKey(apiKeyValue)// sk-923297064fb84d2dae130b516be4f8cf
.appId(appIdValue)// f996225af62b4b11b50d05ce95481252
.topP(0.8) // set streaming output
.incrementalOutput(true) // get streaming output incrementally
.prompt(qMes)
.build();
Application application = new Application();
Flowable<ApplicationResult> result = application.streamCall(param);
StringBuilder fullContent = new StringBuilder();
sseUtils.sendMessage(clientId, "123456789","send start" );
result.blockingForEach(message -> {
// fullContent.append(message.getOutput().getDocReferences().get(0).getMessage().getContent());
sseUtils.sendMessage(clientId, "123456789", message.getOutput().getText());
System.out.println(message.getOutput().getText());
});
sseUtils.sendMessage(clientId, "123456789","send end" );
}