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

效果预览


相关推荐
前端老兵AI2 天前
Electron 桌面应用开发入门:前端工程师的跨平台利器
前端·electron
极客小云2 天前
【Electron-Vue 企业级安全启动模板:electron-vue-theme-template 使用指南】
vue.js·安全·electron
kyle~2 天前
Electron桌面容器
前端·javascript·electron
H_老邪3 天前
Vue + Electron 项目快速上手指南
前端·vue.js·electron
floret. 小花3 天前
Vue3 + Electron 知识点总结 · 2026-03-21
前端·面试·electron·学习笔记·vue3
码路飞4 天前
Electron 太胖了?试试 Electrobun,12MB 打包一个 AI 桌面助手
typescript·electron
floret. 小花4 天前
Vue3 知识点总结 · 2026-03-20
前端·面试·electron·学习笔记·vue3
梦鱼5 天前
🖥️ 告别 Electron 托盘图标模糊:一套精准的 PNG 生成方案
前端·electron
家里有蜘蛛6 天前
Electron 桌面应用多实例实践:数据隔离与跨进程互斥
electron