Skip to content

Commit

Permalink
feat: add plugin-azure
Browse files Browse the repository at this point in the history
feat: add plugin-azure
  • Loading branch information
xiaohuoni authored Nov 22, 2023
2 parents 4022b88 + c4c24c1 commit ad1b1c8
Show file tree
Hide file tree
Showing 22 changed files with 507 additions and 9 deletions.
1 change: 1 addition & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@examples/tabs-custom",
"@examples/tabs-routes",
"@example/test-test",
"@examples/azure",
"umi-scripts"
]
}
5 changes: 5 additions & 0 deletions .changeset/tiny-dodos-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alita/plugin-azure': patch
---

feat: init azure
Binary file added examples/boilerplate/src/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions examples/with-azure/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=8888
7 changes: 7 additions & 0 deletions examples/with-azure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.env.local
/.umirc.local.ts
/.umirc.local.js
/config/config.local.ts
/config/config.local.js
/src/.umi
/.umi
23 changes: 23 additions & 0 deletions examples/with-azure/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineConfig } from 'alita';

export default defineConfig({
appType: 'h5',
// 这个值在 appts 中被 getKeepAlive 修改
keepalive: [/./],
mobileLayout: true,
legacyBuild: false,
// mainPath:'users',
mfsu: false,
hash: false,
reactRouter5Compat: {},
exportStatic: {},
plugins: [
require.resolve('@alita/plugin-azure'),
require.resolve('./pluginazure'),
],
azure: {
apiVersion: '2023-07-01-preview',
model: 'alita4',
resource: 'alita',
},
});
13 changes: 13 additions & 0 deletions examples/with-azure/config/pluginazure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IApi } from 'umi';

let _send = (_: any) => {};
export default (api: IApi) => {
// @ts-ignore
// api.onIntlAzure(async({send})=>{
// _send = send;
// })
// api.onDevCompileDone(async()=>{
// const result = await _send('你好');
// console.log(result.choices[0]!.message?.content);
// })
};
15 changes: 15 additions & 0 deletions examples/with-azure/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@examples/azure",
"private": true,
"scripts": {
"dev": "alita dev",
"build": "alita build",
"start": "npm run dev"
},
"dependencies": {
"@alita/flow": "workspace:*",
"@alita/plugin-azure": "workspace:*",
"alita": "workspace:*",
"antd-mobile": "^5.10.0"
}
}
68 changes: 68 additions & 0 deletions examples/with-azure/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type {
NavBarListItem,
NavBarProps,
TabBarListItem,
TabBarProps,
TitleListItem,
} from 'alita';

export const request = {
prefix: '/api',
method: 'get',
errorHandler: (error) => {
// 集中处理错误
console.log(error);
},
};

const titleList: TitleListItem[] = [
{
pagePath: '/',
title: '首页',
},
{
pagePath: '/users',
title: '列表',
},
{
pagePath: '/users/foo',
title: '设置',
},
];
const navList: NavBarListItem[] = [
{
pagePath: '/',
navBar: {
pageBackground: '#fff',
},
},
{
pagePath: '/users',
navBar: {
pageBackground: '#000',
},
},
];
const navBar: NavBarProps = {
navList,
fixed: false,
onLeftClick: () => {
// router.goBack();
},
};
const tabList: TabBarListItem[] = [];

const tabBar: TabBarProps = {
color: `#999999`,
selectedColor: '#00A0FF',
borderStyle: 'white',
position: 'bottom',
list: tabList,
};

export const mobileLayout = {
documentTitle: '默认标题',
navBar,
tabBar,
titleList,
};
Binary file added examples/with-azure/src/favicon.ico
Binary file not shown.
12 changes: 12 additions & 0 deletions examples/with-azure/src/pages/index/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.title {
font-size: 30px;
:global {
.adm-button{
font-size: 30px
}
}
}

.adm-button{
font-size: 30px
}
27 changes: 27 additions & 0 deletions examples/with-azure/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Button } from 'antd-mobile';
import React, { useState } from 'react';
import styles from './index.less';
import { sendOpenAI } from 'alita';
export default () => {
const [message, setMessage] = useState<string | null>();
return (
<>
<div className={styles['title']}>
{message}
<Button
type="button"
color="primary"
fill="solid"
block
size="large"
onClick={async () => {
const result = await sendOpenAI('你好');
setMessage(result.choices[0]!.message?.content);
}}
>
点我向 GPT 打招呼
</Button>
</div>
</>
);
};
3 changes: 3 additions & 0 deletions examples/with-azure/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./src/.umi/tsconfig.json"
}
1 change: 1 addition & 0 deletions examples/with-azure/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'alita/typings';
2 changes: 1 addition & 1 deletion packages/alita/assets/bundle-status.html

Large diffs are not rendered by default.

Binary file not shown.
Binary file removed packages/create-alita/templates/app/src/favicon.png
Binary file not shown.
80 changes: 80 additions & 0 deletions packages/plugin-azure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# @alita/plugin-azure

See our website [alitajs](https://alitajs.com) for more information.

增加 azure api 对接插件

## 配置

配置中需要提供三个变量

```ts
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`

```sh
AZURE_OPENAI_API_KEY=azureee754027ac362ec351d6bd93
```

## 使用

### 项目中使用

从 alita 中导出 sendOpenAI 和 openai

```tsx
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 对象,你可以保存下来,供其他生命周期使用

```ts
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);
})
};
```
31 changes: 31 additions & 0 deletions packages/plugin-azure/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@alita/plugin-azure",
"version": "3.0.0",
"description": "@alita/plugin-azure",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "pnpm tsc",
"build:deps": "umi-scripts bundleDeps",
"dev": "pnpm build --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/alitajs/alita"
},
"authors": [
"xiaohuoni <[email protected]> (https://github.com/xiaohuoni)"
],
"license": "MIT",
"bugs": "https://github.com/alitajs/alita/issues",
"homepage": "https://github.com/alitajs/alita/tree/master/packages/plugin-azure#readme",
"publishConfig": {
"access": "public"
},
"dependencies": {
"openai": "^4.19.1"
}
}
Loading

0 comments on commit ad1b1c8

Please sign in to comment.