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();
    }
}
相关推荐
麟城Lincoln44 分钟前
【RHCSA-Linux考试题目笔记(自用)】servera的题目
linux·笔记·考试·rhcsa
丰锋ff1 小时前
计网学习笔记第2章 物理层(灰灰题库)
笔记·学习
阿絮~3 小时前
Apache RocketMQ进阶之路阅读笔记和疑问
笔记·apache·rocketmq
天水幼麟8 小时前
动手学深度学习-学习笔记(总)
笔记·深度学习·学习
天水幼麟10 小时前
动手学深度学习-学习笔记【二】(基础知识)
笔记·深度学习·学习
绿皮的猪猪侠11 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
沧海一笑-dj11 小时前
【51单片机】51单片机学习笔记-课程简介
笔记·学习·51单片机·江科大·江科大学习笔记·江科大单片机·江科大51单片机
老虎062711 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
大苏打seven12 小时前
Docker学习笔记:Docker网络
笔记·学习·docker
kikikidult16 小时前
(2025.07)解决——ubuntu20.04系统开机黑屏,左上角光标闪烁
笔记·ubuntu