Skip to content

Commit

Permalink
Use handleChainEnd (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer committed Jun 15, 2023
1 parent 1dc2a11 commit be90740
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-guests-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ai": patch
---

- Switches `LangChainStream` helper callback `handler` to return use `handleChainEnd` instead of `handleLLMEnd` so as to work with sequential chains
2 changes: 1 addition & 1 deletion docs/pages/docs/api-reference/langchain-stream.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The `AIStreamCallbacks` object has the following properties:
The `LangChainCallbacks` object has the following properties which are compatible with [LangChain's `BaseCallbackHandlerMethodsClass`](https://js.langchain.com/docs)

- `handleLLMNewToken: (token: string) => Promise<void>`: A function that is called for each new token generated by the language model. The token is passed as a parameter.
- `handleLLMEnd: () => Promise<void>`: A function that is called when the LangChain process is complete.
- `handleChainEnd: () => Promise<void>`: A function that is called when the LangChain process is complete.
- `handleLLMError: (e: any) => Promise<void>`: A function that is called when there is an error in the LangChain process. The error is passed as a parameter.

## Example
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/docs/guides/langchain.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function POST(req: Request) {

The `handlers` object returned by `LangChainStream` is used to handle certain
[callbacks](https://js.langchain.com/docs/api/callbacks/) provided by LangChain on your behalf.
Under the hood it is a wrapper over LangChain's [`handleLLMEnd`](https://js.langchain.com/docs/api/callbacks/classes/BaseCallbackHandler#handlellmend), [`handleLLMNewToken`](https://js.langchain.com/docs/api/callbacks/classes/BaseCallbackHandler#handlellmnewtoken), and [`handleLLMError`](https://js.langchain.com/docs/api/callbacks/classes/BaseCallbackHandler#handlellmerror).
Under the hood it is a wrapper over LangChain's [`handleChainEnd`](https://js.langchain.com/docs/api/callbacks/classes/BaseCallbackHandler#handlechainend), [`handleLLMNewToken`](https://js.langchain.com/docs/api/callbacks/classes/BaseCallbackHandler#handlellmnewtoken), and [`handleLLMError`](https://js.langchain.com/docs/api/callbacks/classes/BaseCallbackHandler#handlellmerror).
We wrap over these methods to provide which then write to the `stream` which can then be passed directly to [`StreamingTextResponse`](../api-reference/streaming-text-response#streamingtextresponse).

The result is that ther Vercel AI SDK's [`useChat`](../api-reference/use-chat) and [`useCompletion`](../api-reference/use-completion) work flawlessly:
Expand Down
2 changes: 1 addition & 1 deletion packages/core/streams/langchain-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function LangChainStream(callbacks?: AIStreamCallbacks) {
await writer.ready
await writer.write(token)
},
handleLLMEnd: async () => {
handleChainEnd: async () => {
await writer.ready
await writer.close()
},
Expand Down

1 comment on commit be90740

@vercel
Copy link

@vercel vercel bot commented on be90740 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai – ./

ai-git-main-vercel-labs.vercel.app
ai-vercel-labs.vercel.app
ai-three-xi.vercel.app

Please sign in to comment.