首页 > 其他分享 >nest.js学习笔记(七) --知识点拾遗

nest.js学习笔记(七) --知识点拾遗

时间:2023-03-22 17:22:28浏览次数:38  
标签:知识点 插件 const -- nest nestjs new import chatGpt

1、nestjs中引用esm插件

nestjs是使用commonjs规范进行开发,但是目前市场上很多插件是使用module的形式进行开发,所以遇到引用问题时,建议开发都绕过去,使用功能差不多的插件,但是如果遇到绕不过去的情况,那可以使用以下的方法进行引用

import { ConfigService } from '@nestjs/config';
import { Injectable } from '@nestjs/common';
import { FailException } from '@app/exceptions/fail.exception';
import { ErrorCode } from '@app/constants/error.constant';

export const importDynamic = new Function(
  'modulePath',
  'return import(modulePath)',
);

@Injectable()
export class ChatService {
  private chatGpt: any;
  public constructor(private readonly configService: ConfigService) {}

  private async initInstance(): Promise<void> {
    if (!this.chatGpt) {
      const { ChatGPTAPI } = await importDynamic('chatgpt');

      this.chatGpt = new ChatGPTAPI({
        apiKey: this.configService.get('chat.apiKey'),
      });
    }
  }

  public async getChatResponse() {
    try {
      if (!this.chatGpt) await this.initInstance();

      const res = await this.chatGpt.sendMessage('用js生成一个计算器');

      return res;
    } catch (err) {
      console.log(err);
      throw new FailException(ErrorCode.SERVER_ERROR);
    }
  }
}

 

标签:知识点,插件,const,--,nest,nestjs,new,import,chatGpt
From: https://www.cnblogs.com/venblogs/p/17244773.html

相关文章

  • 利用反射将获取的数据封装成对象
    publicclassMyServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException......
  • 安装Jenkins
     #105whichgit/usr/bin/gitwhichjava/usr/bin/javawhichmvn/opt/apache-maven-3.8.7/bin/mvnwhichjenkins/usr/bin/jenkins安装jdk安装git安装......
  • MySQL基础用法
    一、mysql启动/停止服务1、启动服务:netstartmysql2、停止服务:netstopmysql 二、mysql连接1、mysql-uroot-p-P13306(未修改默认端口号的不用加......
  • 复数的物理意义2
    复数不仅有意义,而且可以用图示来优雅地解释。1、实函数与数轴变换大家都认识y=e^x,对于这样的初等函数,我们从小就学会使用直角坐标系来刻画它们:它们的特点都大同小异:把......
  • 【杂题乱写】ARC107
    AtCoderRegularContest107ASimpleMath把\(a,b,c\)提出即可。BQuadruple改成\(a+b=k+c+d\),显然可以枚举\(c+d\)的值从而得到\(a+b\)的值,在此基础上求出每......
  • 添加右键检验文件hash
    去除代码点击查看代码WindowsRegistryEditorVersion5.00[-HKEY_CLASSES_ROOT\*\shell\hash]文件另存为reg后缀执行注意文件编码方式utf8带签名点击查看代......
  • Ubuntu 20.04 制作Linux系统的qcow2镜像
    本篇以制作kali2023操作系统为例,在一台Ubuntu20.04的服务器上制作2GB内存,30GB硬盘的qcow2镜像,如果是VM虚拟机需要开启CPU硬件虚拟化。1.安装依赖软件aptinstallqemu-......
  • Docker限制日志文件大小办法
    1.新建/etc/docker/daemon.json,如果存在就不用重新创建了,内容如下{"log-driver":"json-file","log-opts":{"max-size":"500m","max-file":"3"}}max-size=500......
  • spring.rabbitmq.listener.simple.acknowledge-mode = manual and auto
    spring.rabbitmq.listener.simple.acknowledge-mode=manual*配置改成手动之后,spring不会报错。所以需要消费者抛异常*channel属性    basicNack和basicReject原......
  • 图解 Windows 11 安装
                   ......