Compare commits

...

2 Commits

Author SHA1 Message Date
Logan Markewich 9fac6e3752 changeset 2025-07-21 15:26:50 -06:00
Logan Markewich 6ba09ff5ff minor nits on docs 2025-07-21 15:26:17 -06:00
4 changed files with 52 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@llamaindex/doc": patch
---
Minor updates in deployment docs
@@ -77,7 +77,7 @@ export async function POST(request: NextRequest) {
const agent = await initializeAgent();
const result = await agent.run(message);
return NextResponse.json({ response: result.result });
return NextResponse.json({ response: result.data });
} catch (error) {
console.error("Chat error:", error);
return NextResponse.json(
@@ -132,7 +132,7 @@ export default async function handler(
const agent = await initializeAgent();
const result = await agent.run(message);
res.json({ response: result.result });
res.json({ response: result.data });
} catch (error) {
console.error("Chat error:", error);
res.status(500).json({ error: "Internal server error" });
@@ -220,7 +220,7 @@ export async function POST(request: NextRequest) {
});
const result = await myAgent.run(message);
return NextResponse.json({ response: result.result });
return NextResponse.json({ response: result.data });
} catch (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
@@ -233,11 +233,40 @@ Implement streaming for better user experience:
```typescript
// app/api/chat-stream/route.ts
import { agent } from "@llamaindex/workflow";
import { tool } from "llamaindex";
import { openai } from "@llamaindex/openai";
import { agentStreamEvent } from "@llamaindex/workflow";
import { NextRequest } from "next/server";
import { z } from "zod";
// Assume myAgent is initialized elsewhere
declare const myAgent: any;
// Initialize agent once (consider using a singleton pattern)
let myAgent: any = null;
async function initializeAgent() {
if (myAgent) return myAgent;
try {
const greetTool = tool({
name: "greet",
description: "Greets a user with their name",
parameters: z.object({
name: z.string(),
}),
execute: ({ name }) => `Hello, ${name}! How can I help you today?`,
});
myAgent = agent({
tools: [greetTool],
llm: openai({ model: "gpt-4o-mini" }),
});
return myAgent;
} catch (error) {
console.error("Failed to initialize agent:", error);
throw error;
}
}
export async function POST(request: NextRequest) {
const { message } = await request.json();
@@ -245,9 +274,10 @@ export async function POST(request: NextRequest) {
const stream = new ReadableStream({
async start(controller) {
try {
const context = myAgent.runStream(message);
const agent = await initializeAgent();
const events = agent.runStream(message);
for await (const event of context) {
for await (const event of events) {
if (agentStreamEvent.include(event)) {
controller.enqueue(new TextEncoder().encode(event.data.delta));
}
@@ -63,7 +63,7 @@ app.post('/api/chat', async (req, res) => {
try {
const { message } = req.body;
const result = await myAgent.run(message);
res.json({ response: result.result });
res.json({ response: result.data });
} catch (error) {
res.status(500).json({ error: 'Chat failed' });
}
@@ -110,7 +110,7 @@ fastify.post('/api/chat', async (request, reply) => {
try {
const { message } = request.body as { message: string };
const result = await myAgent.run(message);
return { response: result.result };
return { response: result.data };
} catch (error) {
reply.status(500).send({ error: 'Chat failed' });
}
@@ -162,7 +162,7 @@ app.post("/api/chat", async (c) => {
try {
const result = await myAgent.run(message);
return c.json({ response: result.result });
return c.json({ response: result.data });
} catch (error) {
return c.json({ error: error.message }, 500);
}
@@ -187,9 +187,9 @@ app.post('/api/chat-stream', async (req, res) => {
});
try {
const context = myAgent.runStream(message);
const events = myAgent.runStream(message);
for await (const event of context) {
for await (const event of events) {
if (agentStreamEvent.include(event)) {
res.write(event.data.delta);
}
@@ -34,7 +34,7 @@ export default {
const { message } = await request.json();
const result = await myAgent.run(message);
return new Response(JSON.stringify({ response: result.result }), {
return new Response(JSON.stringify({ response: result.data }), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
@@ -83,7 +83,7 @@ export default async function handler(req, res) {
try {
const result = await myAgent.run(message);
res.json({ response: result.result });
res.json({ response: result.data });
} catch (error) {
res.status(500).json({ error: error.message });
}
@@ -124,7 +124,7 @@ export async function POST(request: NextRequest) {
});
const result = await myAgent.run(message);
return NextResponse.json({ response: result.result });
return NextResponse.json({ response: result.data });
} catch (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
@@ -173,7 +173,7 @@ export const handler: APIGatewayProxyHandler = async (event, context) => {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({ response: result.result }),
body: JSON.stringify({ response: result.data }),
};
} catch (error) {
return {
@@ -222,7 +222,7 @@ export const handler: Handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ response: result.result }),
body: JSON.stringify({ response: result.data }),
};
} catch (error) {
return {