Skip to content

Commit

Permalink
Update concurrency limits and refactor promise queue handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 25, 2024
1 parent 328da6d commit dfaa700
Show file tree
Hide file tree
Showing 23 changed files with 391 additions and 108 deletions.
25 changes: 20 additions & 5 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/scripts/inline-prompts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defTool(
await Promise.all(env.files => prompt`Summarize the ${file}`)

Check notice on line 72 in docs/src/content/docs/reference/scripts/inline-prompts.mdx

View workflow job for this annotation

GitHub Actions / build

The use of `await` with `Promise.all` is incorrect. `Promise.all` takes an array of promises, not a function. The correct usage would be `Promise.all(arrayOfPromises)`.
```

Internally, GenAIScript applies a concurrent limit of 5 per model by default. You can change this limit using the `modelConcurrency` option.
Internally, GenAIScript applies a concurrent limit of 8 per model by default. You can change this limit using the `modelConcurrency` option.

```js "modelConcurrency"
script({
Expand Down
25 changes: 20 additions & 5 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion packages/core/src/concurrency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pLimit, { LimitFunction } from "p-limit"
import { runtimeHost } from "./host"
import { normalizeInt } from "./util"
import { PROMISE_QUEUE_CONCURRENCY_DEFAULT } from "./constants"

export type ConcurrentLimitFunction = LimitFunction

Expand All @@ -20,7 +21,23 @@ export function concurrentLimit(
export class PLimitPromiseQueue implements PromiseQueue {
private queue: LimitFunction
constructor(private readonly options: PromiseQueueOptions) {
this.queue = pLimit(options?.concurrency ?? 5)
this.queue = pLimit(
options?.concurrency ?? PROMISE_QUEUE_CONCURRENCY_DEFAULT
)
}

async mapAll<T extends unknown, Arguments extends unknown[], ReturnType>(
values: T[],
fn: (value: T, ...arguments_: Arguments) => Awaitable<ReturnType>,
...arguments_: Arguments
): Promise<ReturnType[]> {
return await Promise.all(
values.map((value) => this.queue(fn, value, ...arguments_))
)
}

async all<T = any>(fns: (() => Awaitable<T>)[]): Promise<T[]> {
return await Promise.all(fns.map((fn) => this.queue(fn)))
}

add<Arguments extends unknown[], ReturnType>(
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,5 @@ export const OPENAI_RETRY_DEFAULT_DEFAULT = 1000
export const TEMPLATE_ARG_FILE_MAX_TOKENS = 4000
export const TEMPLATE_ARG_DATA_SLICE_SAMPLE = 2000

export const CHAT_REQUEST_PER_MODEL_CONCURRENT_LIMIT = 5
export const CHAT_REQUEST_PER_MODEL_CONCURRENT_LIMIT = 8
export const PROMISE_QUEUE_CONCURRENCY_DEFAULT = 16

Check failure on line 246 in packages/core/src/constants.ts

View workflow job for this annotation

GitHub Actions / build

The values for CHAT_REQUEST_PER_MODEL_CONCURRENT_LIMIT and PROMISE_QUEUE_CONCURRENCY_DEFAULT are hard-coded. This could lead to scalability issues if these values need to be adjusted based on the environment or other factors. Consider making these values configurable through environment variables or a configuration file.
25 changes: 20 additions & 5 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export async function createPromptContext(
await runtimeHost.select(message, options),
input: async (message) => await runtimeHost.input(message),
confirm: async (message) => await runtimeHost.confirm(message),
pQueue: (options) => new PLimitPromiseQueue(options),
promiseQueue: (options) => new PLimitPromiseQueue(options),
})

const ctx: PromptContext & RunPromptContextNode = {
Expand Down
25 changes: 20 additions & 5 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2284,11 +2284,26 @@ interface PromiseQueue {
* @param fn
*/
add<Arguments extends unknown[], ReturnType>(
function_: (
...arguments_: Arguments
) => PromiseLike<ReturnType> | ReturnType,
function_: (...arguments_: Arguments) => Awaitable<ReturnType>,
...arguments_: Arguments
): Promise<ReturnType>

/**
* Runs all the functions in the queue with limited concurrency
* @param fns
*/
all<T = any>(fns: (() => Awaitable<T>)[]): Promise<T[]>

/**
* Applies a function to all the values in the queue with limited concurrency
* @param values
* @param fn
*/
async mapAll<T extends unknown, Arguments extends unknown[], ReturnType>(
values: T[],
fn: (value: T, ...arguments_: Arguments) => Awaitable<ReturnType>,
...arguments_: Arguments
): Promise<ReturnType[]>
}

interface PromiseQueueOptions {
Expand All @@ -2303,9 +2318,9 @@ interface PromptHost extends ShellHost {
container(options?: ContainerOptions): Promise<ContainerHost>

/**
* Create a new job promise queue
* Create a new promise queue to run async functions with limited concurrency
*/
pQueue(options?: PromiseQueueOptions): PromiseQueue
promiseQueue(options?: PromiseQueueOptions): PromiseQueue
}

interface ContainerHost extends ShellHost {
Expand Down
25 changes: 20 additions & 5 deletions packages/sample/genaisrc/blog/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dfaa700

Please sign in to comment.