-
Notifications
You must be signed in to change notification settings - Fork 435
Update vercel ai sdk to v4 #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
04c5c02
3a7da13
f6fb638
b7080b8
d484b3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,6 @@ next-env.d.ts | |
!.yarn/releases | ||
!.yarn/sdks | ||
!.yarn/versions | ||
.env | ||
.env | ||
|
||
.vscode | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||
import { NextRequest, NextResponse } from "next/server"; | ||||||
import { Message as VercelChatMessage, StreamingTextResponse } from "ai"; | ||||||
import { Message as VercelChatMessage, LangChainAdapter } from "ai"; | ||||||
|
||||||
import { createReactAgent } from "@langchain/langgraph/prebuilt"; | ||||||
import { ChatOpenAI } from "@langchain/openai"; | ||||||
|
@@ -26,23 +26,28 @@ const convertVercelMessageToLangChainMessage = (message: VercelChatMessage) => { | |||||
}; | ||||||
|
||||||
const convertLangChainMessageToVercelMessage = (message: BaseMessage) => { | ||||||
if (message._getType() === "human") { | ||||||
if (message.getType() === "human") { | ||||||
return { content: message.content, role: "user" }; | ||||||
} else if (message._getType() === "ai") { | ||||||
} else if (message.getType() === "ai") { | ||||||
return { | ||||||
content: message.content, | ||||||
role: "assistant", | ||||||
tool_calls: (message as AIMessage).tool_calls, | ||||||
parts: (message as AIMessage).tool_calls, | ||||||
}; | ||||||
} else if (message.getType() === "tool") { | ||||||
return { | ||||||
content: message.content, | ||||||
role: "system", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vercel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm weird. id need to read the docs then but i dont think system would be the right role for this |
||||||
}; | ||||||
} else { | ||||||
return { content: message.content, role: message._getType() }; | ||||||
return { content: message.content, role: message.getType() }; | ||||||
} | ||||||
}; | ||||||
|
||||||
const AGENT_SYSTEM_TEMPLATE = `You are a talking parrot named Polly. All final responses must be how a talking parrot would respond. Squawk often!`; | ||||||
|
||||||
/** | ||||||
* This handler initializes and calls an tool caling ReAct agent. | ||||||
* This handler initializes and calls an tool calling ReAct agent. | ||||||
* See the docs for more information: | ||||||
* | ||||||
* https://langchain-ai.github.io/langgraphjs/tutorials/quickstart/ | ||||||
|
@@ -89,36 +94,14 @@ export async function POST(req: NextRequest) { | |||||
/** | ||||||
* Stream back all generated tokens and steps from their runs. | ||||||
* | ||||||
* We do some filtering of the generated events and only stream back | ||||||
* the final response as a string. | ||||||
* | ||||||
* For this specific type of tool calling ReAct agents with OpenAI, we can tell when | ||||||
* the agent is ready to stream back final output when it no longer calls | ||||||
* a tool and instead streams back content. | ||||||
* | ||||||
* See: https://langchain-ai.github.io/langgraphjs/how-tos/stream-tokens/ | ||||||
*/ | ||||||
const eventStream = await agent.streamEvents( | ||||||
{ messages }, | ||||||
{ version: "v2" }, | ||||||
); | ||||||
|
||||||
const textEncoder = new TextEncoder(); | ||||||
const transformStream = new ReadableStream({ | ||||||
async start(controller) { | ||||||
for await (const { event, data } of eventStream) { | ||||||
if (event === "on_chat_model_stream") { | ||||||
// Intermediate chat model generations will contain tool calls and no content | ||||||
if (!!data.chunk.content) { | ||||||
controller.enqueue(textEncoder.encode(data.chunk.content)); | ||||||
} | ||||||
} | ||||||
} | ||||||
controller.close(); | ||||||
}, | ||||||
}); | ||||||
|
||||||
return new StreamingTextResponse(transformStream); | ||||||
const eventStream = agent.streamEvents({ messages }, { version: "v2" }); | ||||||
return LangChainAdapter.toDataStreamResponse(eventStream); | ||||||
} else { | ||||||
/** | ||||||
* We could also pick intermediate steps out from `streamEvents` chunks, but | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we dont want to gitignore this