一:引入Maven依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>broadscope-bailian-sdk-java</artifactId>
<version>1.2.0</version>
</dependency>
二:编写阿里云百炼Client类
配置文件加入如下配置
相关参数请从阿里云百炼官网获取
yun:
bailian:
accessKeyId: **************************
accessKeySecret: **************************
agentKey: **************************
appId: **************************
@Component
public class BaiLianClient {
@Resource
private AlbaiLianConfig baiLianConfig;
private AccessTokenClient accessTokenClient;
@Resource
private ChatCustomerInfoMapper chatCustomerInfoMapper;
@PostConstruct
public void init(){
String accessKeyId = baiLianConfig.getAccessKeyId();
String agentKey = baiLianConfig.getAgentKey();
String accessKeySecret = baiLianConfig.getAccessKeySecret();
accessTokenClient = new AccessTokenClient(accessKeyId,accessKeySecret,agentKey);
}
public Flux<CompletionsResponse> createStreamCompletion(ChatCustomerInfo chatCustomerInfo) {
String content = chatCustomerInfo.getInfoContent();
String token = accessTokenClient.getToken();
BaiLianConfig config = new BaiLianConfig()
.setApiKey(token);
CompletionsRequest request = new CompletionsRequest()
.setAppId(baiLianConfig.getAppId())
.setPrompt(content)
.setStream(true);
ApplicationClient client = new ApplicationClient(config);
chatCustomerInfoMapper.sendInfo(chatCustomerInfo.getUserId(),chatCustomerInfo.getInfoContent(),false);
Flux<CompletionsResponse> flux = client.streamCompletions(request);
return client.streamCompletions(request);
}
}
三:编写Controller层
为了更快实现功能,本人只是简单调用回复消息接口,并将历史消息保存到数据库表中,具体复杂API请参考阿里云百炼官网手册,后端流式输入采用FLux实现,<