AI模拟器

一、介绍

基于鸿蒙Next模拟一个ai对话过程
二、场景需求

客户服务、数据分析、个性化推荐、图像和视频处理、智能家居、交通管理、教育行业、制造等等。

三、业务步骤

第一步:输入框提出问题,发送问题,

第二部:下次发送,先清除之前提问,避免重复提问

第三步:获取ai反馈相关问题答案,目前虚拟数据,可自行设置答案结果
四、效果展示

五:代码展示:

复制代码
import { MsgRoleEnum } from '.../common/CommonConstant'
import { MsgP, textShow } from '.../common/MsgPassageway'
import { HidingHeadVerseData } from '.../database/HidingHeadVerse'

@Entry
@Component
struct Index02{
@State textShowArr: MsgP[] = textShow
@State textMsg: string = ''
@State inputValue: string = ''//获取输入文本数据
@State inputText: string = '' // 输入框展示文本数据
@State btnMsg: string = "发送"
@State btnInputShow: boolean = true // 清除/发送

build(){
Column(){
List(){
ForEach(this.textShowArr,(item:MsgP,index:number)=>{
if (item.role === MsgRoleEnum.Mine) {
ListItem(){
Row(){
//用户消息
Row(){
Text(item.msg)
.fontSize(16)
.textAlign(TextAlign.End)
.padding(6)
.backgroundColor(0xffcbebff)
.borderRadius(4)
}.width("67%")
.justifyContent(FlexAlign.End)
//用户头像
Image($r('app.media.ic_user_portrait')).width(30).height(30).margin({left:10,right:10})

          }.width("100%")
          .padding({top:4,bottom:4})
          .justifyContent(FlexAlign.End)
          .alignItems(VerticalAlign.Top)
        }
      }else {
        ListItem(){
          Row(){
            //AI头像
            Image($r('app.media.ic_gallery_ai_photography_on')).width(30).height(30).margin({left:10,right:10})
            //AI返回消息
            Row(){
              Text(item.msg + this.textMsg)
                .fontSize(16)
                .textAlign(TextAlign.Start)
                .padding(6)
                .backgroundColor(0xffffff)
                .borderRadius(4)
            }.width("67%")
            .justifyContent(FlexAlign.Start)

          }.width("100%")
          .padding({top:4,bottom:4})
          .justifyContent(FlexAlign.Start)
          .alignItems(VerticalAlign.Top)
        }
      }

    },(item:string)=>item)
  }

  Blank()//填充容器组件

  //底部输入框
  if (this.btnInputShow){
    //底部输入框
    Row(){
      //输入框
      TextInput({text:this.inputText,placeholder:"请输入您的关键词~"})
        .width("80%")
        .backgroundColor(0x999999)
        .height(40)
        .margin({left:12,right:12})
        .borderRadius(4)
        .backgroundColor(0xeeeeee)
        .onChange((value:string)=>{
          this.inputValue = value
          this.inputText = value
        })
      //发送图标
      Text(this.btnMsg)
        .margin({right:12})
        .fontColor(0x0091FF)
        .onClick(()=>{
          if (this.btnMsg === "清除"){
            this.textShowArr = []
            this.btnMsg = "发送"
          }else {
            this.textMsg = ""
            this.HidingHeadVerseFun()
          }

        })
    }.width("100%")
    .height(70)
    .backgroundColor(0xffffff)
    .alignItems(VerticalAlign.Center)
    .justifyContent(FlexAlign.Start)
  }

}
.width("100%")
.height("100%")
.borderRadius({ bottomLeft:12,bottomRight:12 })
.backgroundColor(0xeeeeee)
.justifyContent(FlexAlign.Start)
}

HidingHeadVerseFun(){
if (this.inputValue !== "") {
this.textShowArr.push({
role:0,
msg:this.inputText
})
this.inputText = ""
this.btnMsg = "清除"
this.Search_HidingHeadVerseFun()
}else {
console.info("输入文本数据为空")
}
}
//打字机效果
HidingHeadVerseTypewriter(v_idx:number){
if (v_idx == 999) {
let strLen = "我还在成长中,试试以下关键词:时间的片段、陌生的信..."
let idx = 0
let timer = setInterval(()=>{
if (idx < strLen.length) {
this.textMsg += strLen[idx++];
} else {
clearInterval(timer);
}
},100)
}else {
let strLen = HidingHeadVerseData[v_idx].title as string + HidingHeadVerseData[v_idx].content as string
let idx = 0
let timer = setInterval(()=>{
if (idx < strLen.length) {
this.textMsg += strLen[idx++];
} else {
clearInterval(timer);
}
},100)
}
}

Search_HidingHeadVerseFun(){
let searchStr = this.inputValue.includes("时间的片段") || this.inputValue.includes("时间")
let searchStr2 = this.inputValue.includes("陌生的信") || this.inputValue.includes("陌生的信")
if (searchStr) {
this.textShowArr.push({
role: 1,
msg: ""
})
this.HidingHeadVerseTypewriter(0)
}if (searchStr2){
this.textShowArr.push({
role: MsgRoleEnum.Other,
msg: ""
})
this.HidingHeadVerseTypewriter(2)

六、代码结构及原理:
1.整体结构:

使用了ArkTS的装饰器语法,如@Entry和@Component组件。使用了ArkTS的渲染语法if/else等
2.状态管理:

组件使用@State装饰器定义了几个响应式状态变量,如btnInputShow这些变量的变化会自动触发UI的更新。
3.UI结构:

界面使用嵌套的Column和Row组件构建。使用了ForEach遍历循环数据信息。
4.数据传递:

当点击"发送"按钮时,会调用HidingHeadVerseFun()回调函数,先获取用户问题数据,然后在数据库中对比,调用Search_HidingHeadVerseFun()回调函数,获取答案数据信息。

总的来说,这段代码展示了如何使用ArkTS和ArkUI框架创建一个交互式的ai问答界面。它利用了声明式UI、响应式编程和组件化的概念,使得代码结构清晰,易于理解和维护。

相关推荐
AIGC方案1 小时前
deepseek热度已过?
人工智能·大模型
88号技师1 小时前
模糊斜率熵Fuzzy Slope entropy+状态分类识别!2024年11月新作登上IEEE Trans顶刊
人工智能·机器学习·分类·数据挖掘·信号处理·故障诊断·信息熵
死磕代码程序媛2 小时前
谷歌25年春季新课:15小时速成机器学习
人工智能·机器学习
有Li2 小时前
弥合多层次差距:用于超声心动图中基于文本引导的标签高效分割的双向循环框架|文献速递-深度学习医疗AI最新文献
人工智能
橙色小博2 小时前
Transformer模型解析与实例:搭建一个自己的预测语言模型
人工智能·深度学习·神经网络·机器学习·transformer
意.远3 小时前
PyTorch实现二维卷积与边缘检测:从原理到实战
人工智能·pytorch·python·深度学习·神经网络·计算机视觉
电报号dapp1193 小时前
2025年智能合约玩法创新白皮书:九大核心模块与收益模型重构Web3经济范式
人工智能·重构·web3·去中心化·区块链·智能合约
三道杠卷胡4 小时前
【AI News | 20250411】每日AI进展
人工智能·python·计算机视觉·语言模型·aigc
汪子熙4 小时前
利用 Trae 开发平面直角坐标系的教学动画
人工智能·trae
区块链蓝海4 小时前
Cables 现已正式启动积分计划 Alpha 阶段,开放早期白名单申请
人工智能·区块链