Electron Forge【实战】阿里百炼大模型 —— AI 聊天

获取 apiKey

登录并开通阿里云百炼
https://bailian.console.aliyun.com/#/home

新人有半年免费的使用福利,在模型详情中,可以查看剩余的免费额度
https://bailian.console.aliyun.com/?tab=model#/model-market/detail/qwen-turbo

在下方链接中创建 apiKey
https://bailian.console.aliyun.com/?tab=model#/api-key

安装 OpenAI SDK

TS 复制代码
npm install openai

src/providers/OpenAIProvider.ts

ts 复制代码
import OpenAI from 'openai'

interface ChatMessageProps {
  role: string;
  content: string;
}

interface UniversalChunkProps {
  is_end: boolean;
  result: string;
}

export class OpenAIProvider {
  private client: OpenAI;
  constructor(apiKey: string, baseURL: string) {
    this.client = new OpenAI({
      apiKey,
      baseURL
    })
  }
  async chat(messages: ChatMessageProps[], model: string) {
    const stream = await this.client.chat.completions.create({
      model,
      messages,
      stream: true
    })
    const self = this
    return {
      async *[Symbol.asyncIterator]() {
        for await (const chunk of stream) {
          yield self.transformResponse(chunk)
        }
      }
    }
  }
  protected transformResponse(chunk: OpenAI.Chat.Completions.ChatCompletionChunk): UniversalChunkProps {
    const choice = chunk.choices[0]
    return {
      is_end: choice.finish_reason === 'stop',
      result: choice.delta.content || ''
    }
  }
}

src/providers/createProvider.ts

ts 复制代码
import { QianfanProvider } from "./QianfanProvider";
import { OpenAIProvider } from './OpenAIProvider'

export function createProvider(providerName: string) {

  const providerConfigs = {
    aliyun: {
      apiKey: "换成第一步获取的apiKey",
      baseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
    }
  };

  // 为了解决类型错误,先进行类型断言,确保可以通过 providerName 访问 providerConfigs
  const providerConfig = (providerConfigs as { [key: string]: any })[providerName];

  switch (providerName) {
    case "qianfan":
      if (!providerConfig.accessKey || !providerConfig.secretKey) {
        throw new Error(
          "缺少千帆API配置:请在设置中配置 accessKey 和 secretKey"
        );
      }
      return new QianfanProvider(
        providerConfig.accessKey,
        providerConfig.secretKey
      );
    case 'aliyun':
      if (!providerConfig.apiKey || !providerConfig.baseUrl) {
        throw new Error('缺少阿里云百炼API配置:请在设置中配置 apiKey 和 baseUrl')
      }
      return new OpenAIProvider(providerConfig.apiKey, providerConfig.baseUrl)
    default:
      throw new Error(`不支持的AI服务提供商: ${providerName}`);
  }
}

其他通用代码见
https://blog.csdn.net/weixin_41192489/article/details/147492144

效果预览


相关推荐
EndingCoder2 天前
Electron Fiddle:快速实验与原型开发
前端·javascript·electron·前端框架
EndingCoder2 天前
Electron 进程模型:主进程与渲染进程详解
前端·javascript·electron·前端框架
!执行3 天前
electron + react +react-router-dom 打包桌面应用白屏
javascript·react.js·electron
呲溜滑_3 天前
electron-vite 配合python
javascript·python·electron
秉承初心3 天前
Electron 项目来实现文件下载和上传功能(AI)
javascript·electron·iphone
EndingCoder4 天前
安装与环境搭建:准备你的 Electron 开发环境
前端·javascript·electron·前端框架
Lucky_Turtle4 天前
【electron】一、安装,打包配置
javascript·arcgis·electron
赵民勇4 天前
如果已经安装了electron的一个版本,再次使用命令npm install electron不指定electron版本时,会下载安装新版本么?
javascript·electron·npm
EndingCoder4 天前
Electron 简介:Node.js 桌面开发的起点
开发语言·前端·javascript·electron·node.js·桌面端
现在没有牛仔了5 天前
小试牛刀,用electron+vue3做了一个文件归纳程序~
前端·electron