当前位置: 首页 > news >正文

西昌做网站佛山做网站推广的公司

西昌做网站,佛山做网站推广的公司,景德镇网站建设公司,公关公司如何处理危机Demo介绍 本demo对接阿里云和百度的大模型API,实现一个简单的对话应用。 DecEco Studio版本:DevEco Studio 3.1.1 Release HarmonyOS API版本:API9 关键点:ArkTS、ArkUI、UIAbility、网络http请求、列表布局 官方接口文档 此…

Demo介绍

本demo对接阿里云和百度的大模型API,实现一个简单的对话应用。

DecEco Studio版本:DevEco Studio 3.1.1 Release

HarmonyOS API版本:API9

关键点:ArkTS、ArkUI、UIAbility、网络http请求、列表布局

官方接口文档

此链接为当前时间(2024-1-26)文档链接地址,可能发生迁移变更,以官方为准。

阿里云通义千文API接口文档地址:通义千问API如何使用_模型服务灵积(DashScope)-阿里云帮助中心

百度智能云千帆大模型API接口文档地址:鉴权介绍 - 千帆大模型平台 | 百度智能云文档

新建项目

API9,Stage模型(需要联网)

资源同步下载结束后,打开预览器,可正常预览页面

实现API接口调用

新建http目录,在其中并在此目录下新建两个ts文件,分别实现调用阿里云和百度的API接口

申请网络权限

1、对接阿里云API

开通服务并获得API-KEY

请参照:如何开通DashScope并创建API-KEY_模型服务灵积(DashScope)-阿里云帮助中心

开通完成后,可在工作台拿到接口请求的鉴权信息:API-KEY,发起http时请求头header需要携带

参照文档实现请求方法

根据接口文档构造请求体,发起http请求,完成大模型对话

接口文档:通义千问API如何使用_模型服务灵积(DashScope)-阿里云帮助中心

实现ALiYunHttpUtils 类的 request 方法。


import http from '@ohos.net.http';
import hilog from '@ohos.hilog';
class ALiYunHttpUtils {request(question: string) {hilog.info(0x0000, 'testTag', 'ALiYunHttpUtils request invoke. question: %{public}s', question);// 1 createHttp接口创建请求let httpRequest = http.createHttp();// 2 发起请求httpRequest.request(// 请求地址"https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation",// 请求options: HttpRequestOptions{// 请求方式method: http.RequestMethod.POST,// 请求头header: {"Content-Type": "application/json",// 这里的Authorization 就是刚才工作台查看的 API-KEY"Authorization": "sk-0bxxxxxxxxxxxxxxxxc3" // 脱敏处理},// 请求体extraData: {"model": "qwen-plus", // 指定用于对话的通义千问模型名"input": {"messages": [{"role": "user","content": question // 请求发起方传入的问题}]}}}, (err, data: http.HttpResponse) => {if (err) {hilog.error(0x0000, 'testTag', 'Failed to request ALiYun. Cause: %{public}s', JSON.stringify(err) ?? '');httpRequest.destroy();} else {hilog.error(0x0000, 'testTag', 'Request ALiYun success. data: %{public}s', JSON.stringify(data.result));httpRequest.destroy();}})}}
export default new ALiYunHttpUtils;
调用http请求方法

在index页面加载的时候,调用ALiYunHttpUtils.request方法

刷新页面触发请求

刷新或重启Preview预览器,index页面重新加载,会执行aboutToAppear方法;

打开Log控制台,可看到请求结果

2、对接百度API

创建应用

登录平台,创建应用,可得到应用的API Key 和 Secret Key;这两个信息在调用鉴权信息接口时会用到。

开通服务(计费)

添加应用后,会默认预置一部分服务,可在【在线服务】菜单页查看;

大部分服务都需要付费开通,有几个免费的本人不太会用(尴尬。。)有免费的策略各位也可以分享一下 嘿嘿~

开通了一个计费的,个人测试使用,调用不会很频繁

鉴权认证

根据鉴权接口文档,实现鉴权接口请求

文档地址:获取access_token - 千帆大模型平台 | 百度智能云文档

实现BaiduHttpUtils类的 request 方法。

注意:此处BaiduHttpUtils.request方法仅完成了鉴权接口调用,还未进行真正的对话

import hilog from '@ohos.hilog';
import http from '@ohos.net.http';
class BaiduHttpUtils {request(question: string) {hilog.info(0x0000, 'testTag', 'BaiduHttpUtils request invoke. question: %{public}s', question);// 先鉴权// 1 createHttp接口创建请求let httpRequest = http.createHttp();// 2 发起请求httpRequest.request(// 请求地址// 参数grant_type 固定值client_credentials// 参数client_id 应用的API Key// 参数client_secret 应用的Secret Key"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=asxxxxxxxxxxHo&client_secret=ihxxxxxxxxgG",{method: http.RequestMethod.POST,header: {"Content-Type": "application/json"}}, (err, data: http.HttpResponse) => {if (err) {hilog.error(0x0000, 'testTag', 'Failed to request BaiDu. Cause: %{public}s', JSON.stringify(err) ?? '');httpRequest.destroy();} else {hilog.error(0x0000, 'testTag', 'Request BaiDu success. data: %{public}s', JSON.stringify(data.result));httpRequest.destroy();}})}}
export default new BaiduHttpUtils;

在index页面加载的时候,调用BaiduHttpUtils.request方法

鉴权通过后,可在接口返回中获取后续对话接口所需要的鉴权信息access_token

发起对话请求

拿到access_token后,可根据上述开通的服务对应的接口文档,发起对话请求

实现BaiduHttpUtils类的 chatRequest方法,在鉴权结束后调用

import hilog from '@ohos.hilog';
import http from '@ohos.net.http';
class BaiduHttpUtils {request(question: string) {hilog.info(0x0000, 'testTag', 'BaiduHttpUtils request invoke. question: %{public}s', question);// 先鉴权// 1 createHttp接口创建请求let httpRequest = http.createHttp();// 2 发起请求httpRequest.request(// 请求地址// 参数grant_type 固定值client_credentials// 参数client_id 应用的API Key// 参数client_secret 应用的Secret Key"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=asxxxxxxxxHo&client_secret=ihxxxxxxxxgG",{method: http.RequestMethod.POST,header: {"Content-Type": "application/json"}}, (err, data: http.HttpResponse) => {if (err) {hilog.error(0x0000, 'testTag', 'Failed to request BaiDu. Cause: %{public}s', JSON.stringify(err) ?? '');httpRequest.destroy();} else {hilog.error(0x0000, 'testTag', 'Request BaiDu success. data: %{public}s', JSON.stringify(data.result));httpRequest.destroy();// 携带认证信息 发起对话请求let respToken: BaiDuToken = JSON.parse(data.result.toString())this.chatRequest(respToken.access_token, question)}})}chatRequest(token: string, question: string) {// 通常情况不建议把token打印出来 此处为了方便调试hilog.info(0x0000, 'testTag', 'BaiduHttpUtils chaRequest invoke. token: %{public}s, question: %{public}s', token, question);// 1 createHttp接口创建请求let httpRequest = http.createHttp();// 2 发起请求httpRequest.request(// 请求地址"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + token,{method: http.RequestMethod.POST,header: {"Content-Type": "application/json"},extraData: {"messages": [{"role": "user","content": question}]}}, (err, data: http.HttpResponse) => {if (err) {hilog.error(0x0000, 'testTag', 'Failed to request BaiDu. Cause: %{public}s', JSON.stringify(err) ?? '');httpRequest.destroy();} else {hilog.error(0x0000, 'testTag', 'Request BaiDu success. data: %{public}s', JSON.stringify(data.result));httpRequest.destroy();}})}}
export default new BaiduHttpUtils;class BaiDuToken {access_token: stringexpires_in: numbersession_key: string// ...
}
刷新页面触发请求

刷新或重启Preview预览器,index页面重新加载,会执行aboutToAppear方法;

打开Log控制台,可看到请求结果

至此,两个大模型的API接口对接完成,下一步可以开始设计对话页面。

http://www.dt0577.cn/news/13195.html

相关文章:

  • 上海网站建设市场分析搜索引擎营销的优势和劣势
  • python网站入口上海网络营销公司
  • 辽宁省水利建设市场信用信息平台网站谷歌商店下载不了软件
  • 如何做网站推广及优化永久免费国外域名注册
  • 江西建设银行社会招聘网站福州网站制作推广
  • 做网站包含微信公众号吗搜索引擎seo推广
  • vue做公司网站上海百度推广优化
  • 六盘水网站建设求职简历app优化建议
  • 温岭市建设工程质量安全网站品牌营销和市场营销的区别
  • 协会网站建站杭州优化建筑设计
  • 海南网站建设平台黄金网站app大全
  • 最新软件推广seo外包网络公司
  • 做设计用的素材下载网站北京网站sem、seo
  • 学校网站建设用哪个系统怎么样优化网站seo
  • 网站未备案被阻断怎么做夫唯老师seo
  • 关于节约化建设网站的表态发言网络公关公司
  • 家装设计效果图专业网站中国最新疫情最新消息
  • wordpress多站点 域名网站做优化好还是推广好
  • wordpress 2017电商seo是什么意思啊
  • 徐州教育平台网站建设武汉seo 网络推广
  • 网站互动设计方式网络营销推广平台
  • 做国外的众筹网站seo实战技巧
  • 移动网站用什么建设seo关键词排名优化软件怎么选
  • 杭州网站制作站长工具的使用seo综合查询排名
  • 网站二维码链接怎么做的国家优化防控措施
  • ui图标素材网站产品关键词的搜索渠道
  • 和硕网站建设seo查询系统源码
  • 网站页面设计制作费google推广平台怎么做
  • 做网站香港备案花西子网络营销案例分析
  • 怎么做外链到其他网站广东网站优化公司