alitajs / alita

A React framework based on umi.
https://alitajs.com
MIT License
788 stars 85 forks source link

feat: add plugin-azure #623

Closed xiaohuoni closed 9 months ago

xiaohuoni commented 9 months ago

@alita/plugin-azure

增加 azure api 对接插件

配置

配置中需要提供三个变量

import { defineConfig } from 'alita';

export default defineConfig({
  plugins: [
    require.resolve('@alita/plugin-azure'),
  ],
  azure: {
    apiVersion: '2023-07-01-preview',
    model: 'alita4',
    resource: 'alita',
  },
});

环境变量中必须提供 AZURE_OPENAI_API_KEY

.env.local

AZURE_OPENAI_API_KEY=azureee754027ac362ec351d6bd93

使用

项目中使用

从 alita 中导出 sendOpenAI 和 openai

import { sendOpenAI, openai } from 'alita';
import { useState } from 'react';

export default () => {
  const [message, setMessage] = useState<string | null>();
  return (
    <>
      {message}
      <button
        onClick={async () => {
          const result = await sendOpenAI('你好');
          setMessage(result.choices[0]!.message?.content);
        }}
      >
        点我向 GPT 打招呼
      </button>
    </>
  );
};

插件中使用

增加 umi 插件 api - onIntlAzure 可以取到 send 和 openai 对象,你可以保存下来,供其他生命周期使用

import { IApi } from 'umi';

let _send = (_: any) => {};
export default (api: IApi) => {
  // @ts-ignore
  api.onIntlAzure(async({send,openai})=>{
    _send = send;
  })
  api.onDevCompileDone(async()=>{
    const result = await _send('你好');
    console.log(result.choices[0]!.message?.content);
  })
};
changeset-bot[bot] commented 9 months ago

🦋 Changeset detected

Latest commit: c4c24c1e531a57bab0d21995363eb1abe685f9d7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------------------- | ----- | | @alita/plugin-azure | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

vercel[bot] commented 9 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
alita-alita ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2023 7:32am