Skip to content

Commit

Permalink
✨ feat: Add concurrency pMap to lobe seo
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Mar 24, 2024
1 parent 0b3ec46 commit fd5141d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 78 deletions.
19 changes: 10 additions & 9 deletions packages/lobe-seo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ Some additional configurations are provided in this project, set using environme

## 🔍 Configuration

| Property Name | Required | Type | Default Value | Description |
| -------------- | -------- | -------------- | --------------- | -------------------------------- |
| entry | `*` | `string` | - | Entry file or folder |
| entryExtension | | `string` | `.mdx` | Entry file extension |
| groupKey | | `string` | - | Set group key for SEO matters |
| tagStringify | | `boolean` | `false` | Stringify the tags array |
| modelName | | `string` | `gpt-3.5-turbo` | Model used |
| temperature | | `number` | `0` | Sampling temperature used |
| experimental | | `experimental` | `{}` | Experimental features, see below |
| Property Name | Required | Type | Default Value | Description |
| -------------- | -------- | -------------- | --------------- | ------------------------------------------------ |
| entry | `*` | `string` | - | Entry file or folder |
| entryExtension | | `string` | `.mdx` | Entry file extension |
| groupKey | | `string` | - | Set group key for SEO matters |
| tagStringify | | `boolean` | `false` | Stringify the tags array |
| modelName | | `string` | `gpt-3.5-turbo` | Model used |
| temperature | | `number` | `0` | Sampling temperature used |
| concurrency | | `number` | `5` | Number of concurrently pending promises returned |
| experimental | | `experimental` | `{}` | Experimental features, see below |

#### `experimental`

Expand Down
1 change: 1 addition & 0 deletions packages/lobe-seo/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ $ lobe-seo -c './custom-config.js' # or use the full flag --config
| tagStringify | | `boolean` | `false` | 将 tags 字符串化 |
| modelName | | `string` | `gpt-3.5-turbo` | 使用的模型 |
| temperature | | `number` | `0` | 使用的采样温度 |
| concurrency | | `number` | `5` | 同时并发的队列请求数量 |
| experimental | | `experimental` | `{}` | 实验性功能,见下文 |

#### `experimental`
Expand Down
48 changes: 29 additions & 19 deletions packages/lobe-seo/src/commands/Seo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from 'chalk';
import { consola } from 'consola';
import { globSync } from 'glob';
import matter from 'gray-matter';
import pMap from 'p-map';

import { SeoCore, SeoQueryItem } from '@/core/SeoCore';
import { selectors } from '@/store';
Expand Down Expand Up @@ -54,25 +55,34 @@ class Seo {
)}) ${this.config.experimental?.jsonMode ? chalk.red(' [JSON Mode]') : ''}}`,
);
let totalTokenUsage = 0;
for (const item of this.query) {
const data = await this.seo.run({
...item,
onProgress: ({ isLoading }) => {
if (isLoading) {
consola.start(item.entry);
}
},
});

if (data?.result && Object.keys(data.result).length > 0) {
const result = matter.stringify(item.content, data.result);
writeMarkdown(item.entry, result);
totalTokenUsage += data.tokenUsage;
consola.success(chalk.yellow(item.entry), chalk.gray(`[Token usage: ${data.tokenUsage}]`));
} else {
consola.warn('No translation result was found:', chalk.yellow(item.entry));
}
}

await pMap(
this.query,
async (item) => {
const data = await this.seo.run({
...item,
onProgress: ({ isLoading }) => {
if (isLoading) {
consola.start(item.entry);
}
},
});

if (data?.result && Object.keys(data.result).length > 0) {
const result = matter.stringify(item.content, data.result);
writeMarkdown(item.entry, result);
totalTokenUsage += data.tokenUsage;
consola.success(
chalk.yellow(item.entry),
chalk.gray(`[Token usage: ${data.tokenUsage}]`),
);
} else {
consola.warn('No translation result was found:', chalk.yellow(item.entry));
}
},
{ concurrency: this.config.concurrency || 5 },
);

if (totalTokenUsage > 0) consola.info('Total token usage:', chalk.cyan(totalTokenUsage));
}

Expand Down
50 changes: 0 additions & 50 deletions packages/lobe-seo/src/components/Progress.tsx

This file was deleted.

0 comments on commit fd5141d

Please sign in to comment.