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

效果预览


相关推荐
一拳不是超人3 天前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
柯南95277 天前
Electron 无边框窗口拖拽实现
vue.js·electron
卸任7 天前
Windows判断是笔记本还是台式
前端·react.js·electron
一拳不是超人7 天前
Electron 实战全解析:基于 WebContentView 的多视图管理系统
前端·javascript·electron
lpfasd12311 天前
Tauri vs Electron:高质量Word/PDF导出效果深度对比
electron·pdf·word
卸任12 天前
Electron判断是内置摄像头还是接摄像头
前端·react.js·electron
贺今宵14 天前
Capacitor打包electron为apk
electron
一文解千机15 天前
wine 优化配置及显卡加速,完美运行Electron 编译的程序(新榜小豆芽、作家助手、小V猫等)
linux·ubuntu·electron·wine·wine优化配置·wine显卡加速·wine大型游戏
weixin_4255437319 天前
TREA CN 3.3.30 + GLM-5 王炸更新
ai·electron
一枚小太阳20 天前
想学 Electron?这份「能跑的示例集」一篇搞懂
前端·electron