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();
    }
}
相关推荐
La Pulga10 小时前
【FreeRTOS源码学习笔记】任务延时列表
笔记·mcu·学习·嵌入式·freertos·实时操作系统
Peter-OK15 小时前
PostgreSQL学习笔记(入门-进阶-精通说明)
笔记·学习·postgresql
成都蜀味缘餐饮技术培训学校17 小时前
手撕烤兔技术培训实战指南,秘制腌料配方比例与调制手法
大数据·经验分享·笔记·其他·微信公众平台
希和202718 小时前
免费AI视频生成工具汇总(国内优先,适合做漫剧、短视频,Obsidian笔记版)
人工智能·笔记·音视频
蒸蒸yyyyzwd19 小时前
秋招学习笔记 day109
笔记·八股
只愿云淡风清21 小时前
正则表达式学习笔记 02:基础语法
笔记·学习·正则表达式
希和20271 天前
考研英语阅读词汇笔记|城市野径、抄近道步道(非正式小路)
笔记·考研
一个人旅程~1 天前
关于imac27inch因改短用户名导致混合硬盘和系统崩溃的问题分析与故障解决
经验分享·笔记·macos·新浪微博
心祭丫丫1 天前
【每天一个 Python 小案例17】Python 实现通讯录
开发语言·人工智能·笔记·python·学习
派大猩猩_2 天前
Obsidian 笔记怎么同步多端:Nutstore Sync 大笔记库同步测评
笔记·obsidian·obsidian插件·obsidian同步