Skip to content

Commit

Permalink
Ensure that run managers are returned from handleLLMStart/handleChatM…
Browse files Browse the repository at this point in the history
…odelStart always in the same order as the prompts passed in

What was happening was that the runManager was only added to the managers array after the callback handlers finished executing, so eg if the callback handlers for the second run finished first the runManager for prompt #2 would actually be in position #1 of the returned array.
  • Loading branch information
nfcampos committed Jun 27, 2023
1 parent fe7b388 commit 5611117
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions langchain/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ export class CallbackManager
_parentRunId: string | undefined = undefined,
extraParams: Record<string, unknown> | undefined = undefined
): Promise<CallbackManagerForLLMRun[]> {
const managers: CallbackManagerForLLMRun[] = [];

await Promise.all(
return Promise.all(
prompts.map(async (prompt) => {
const runId = uuidv4();

Expand All @@ -377,20 +375,16 @@ export class CallbackManager
)
);

managers.push(
new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
)
return new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
);
})
);

return managers;
}

async handleChatModelStart(
Expand All @@ -400,9 +394,7 @@ export class CallbackManager
_parentRunId: string | undefined = undefined,
extraParams: Record<string, unknown> | undefined = undefined
): Promise<CallbackManagerForLLMRun[]> {
const managers: CallbackManagerForLLMRun[] = [];

await Promise.all(
return Promise.all(
messages.map(async (messageGroup) => {
const runId = uuidv4();

Expand Down Expand Up @@ -441,20 +433,16 @@ export class CallbackManager
)
);

managers.push(
new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
)
return new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
);
})
);

return managers;
}

async handleChainStart(
Expand Down

0 comments on commit 5611117

Please sign in to comment.