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

chili3d调试笔记5 直接加入js和大模型对话 trae

// 定义一个异步函数来生成请求选项并发送请求
export async function send_to_llm(mycontent:String ): Promise<string> {const myHeaders = new Headers();myHeaders.append("Authorization", "Bearer sk-240e8a769152439594a0c4c17618db9c");myHeaders.append("Content-Type", "application/json");const raw = JSON.stringify({messages: [{ content: mycontent, role: "user" },{ content: "you are a helpful assistant", role: "system" }],model: "deepseek-chat"});const requestOptions: RequestInit = {method: 'POST',headers: myHeaders,body: raw,redirect: 'follow' as RequestRedirect};try {const response = await fetch("https://api.deepseek.com/v1/chat/completions", requestOptions);if (!response.ok) {throw new Error(`HTTP error! Status: ${response.status}`);}const result = await response.text();console.log(result); // 可选:打印日志以便调试return result;} catch (error) {console.error('Error in send_to_llm:', error);throw error; // 将错误抛出以便调用方处理}}

import { I18n, Logger } from "chili-core";
import { button, div, input, label } from "../components";
import style from "../dialog.module.css";
import { send_to_llm } from "./send_to_llm";
export class njsgcs_Dialog {private constructor() {}static show() { const dialog = document.createElement("dialog");document.body.appendChild(dialog);// 创建输入框并保存引用const user_say_input = input({type: "text",id: "njsgcs_test_input",onkeydown: (e: KeyboardEvent) => {e.stopPropagation();},});// 创建 label 元素并保存引用const resultLabel = label({ textContent: "" });dialog.appendChild(div({ className: style.root },div({ className: style.title }, label({ textContent: I18n.translate("njsgcs_showDialog") })),div({ className: style.input }, user_say_input),div({ className: style.buttons },button({textContent: I18n.translate("common.confirm"),onclick: async () => {try {// 动态获取输入框的值let response: string = await send_to_llm(user_say_input.value);// 将 response 解析为 JSON 对象const jsonResponse = JSON.parse(response);let content_response: string = jsonResponse.choices[0].message.content;Logger.info(content_response);// 将 content_response 赋值给 labelresultLabel.textContent = content_response;} catch (error) {Logger.error("Failed to parse response as JSON:", error);}},}),),// 添加结果显示区域div({ className: style.result }, resultLabel)),);dialog.showModal();}
}

相关文章:

  • windows上部署本地知识库(RAG)ollama + docker + ragflow方案
  • 组装一台intel n95纯Linux Server服务器
  • RBF(径向基神经网络)基础解析与代码实例:拟合任意函数
  • Java从入门到“放弃”(精通)之旅——类和对象全面解析⑦
  • HBuilder X:前端开发的终极生产力工具
  • 【C语言】srand() rand seed其实是设置一个初始值
  • 百级Function架构集成DeepSeek实践:Go语言超大规模AI工具系统设计
  • kotlin知识体系(五) :Android 协程全解析,从作用域到异常处理的全面指南
  • 深入理解组合实体模式(Composite Entity Pattern)在 C# 中的应用与实现
  • 基于SpringAI Alibaba实现RAG架构的深度解析与实践指南
  • 【数据结构_12】二叉树(4)
  • C 语言的未来:在变革中坚守与前行
  • Windows串口通信
  • 进程管理,关闭进程
  • PCA——主成分分析数学原理及代码
  • 【图像处理基石】什么是去马赛克算法?
  • springboot+vue3+mysql+websocket实现的即时通讯软件
  • 热门算法面试题第19天|Leetcode39. 组合总和40.组合总和II131.分割回文串
  • PyTorch基础笔记
  • 【笔记】SpringBoot实现图片上传和获取图片接口
  • 数智时代出版专业技能人才培养研讨会在沪举行
  • 经济日报金观平:拥抱中国就是拥抱确定性
  • 85岁眼科专家、武汉大学人民医院原眼科主任喻长泰逝世
  • 上海这台人形机器人完成半马:无故障、无摔倒,冲过终点不忘挥手致意
  • 加快从数量增长向品质跃升转变,促进生态空间与城市功能有机共生!龚正调研公园城市建设工作
  • 人民网评:官方轻踩刹车,智能驾驶不能“蒙眼狂奔”