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

TypeScript 复制代码
// 定义一个异步函数来生成请求选项并发送请求
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; // 将错误抛出以便调用方处理
    }
  }
TypeScript 复制代码
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 赋值给 label
                                resultLabel.textContent = content_response;
                            } catch (error) {
                                Logger.error("Failed to parse response as JSON:", error);
                            }
                        },
                    }),
                ),
                // 添加结果显示区域
                div({ className: style.result }, resultLabel)
            ),
        );

        dialog.showModal();
    }
}
相关推荐
爱看大明王朝15664 小时前
磁件学习-磁性元器件的极限计算
笔记·学习
问心无愧05134 小时前
ctf show web入门 40
笔记
@蓝莓果粒茶6 小时前
【Unity笔记】保姆级AssetBundle详解(含代码+避坑指南)
笔记·游戏·unity
kobesdu7 小时前
【ROS2实战笔记-20】ROS2 bag 录播与时间模拟:从基础操作到高级调试技巧
笔记·机器人·ros·ros2
kobesdu8 小时前
【ROS2实战笔记-18】ROS2 通信的隐秘控制:DDS 配置参数如何决定系统性能
网络·人工智能·笔记·机器人·开源·ros·人形机器人
nnsix9 小时前
Unity 动画 Avatar 笔记
笔记·unity·游戏引擎
中草药z11 小时前
【测试基础】Python 核心语法,一篇搞定测试脚本开发基础
开发语言·笔记·python·学习·测试·语法
一口吃俩胖子11 小时前
【脉宽调制DCDC功率变换学习笔记020】频域性能准则
笔记·学习
被考核重击12 小时前
WASM学习笔记
笔记·学习·wasm
三品吉他手会点灯13 小时前
C语言学习笔记 - 27.C编程预备计算机专业知识 - 什么是字节
c语言·开发语言·笔记·学习