Skip to content

Commit

Permalink
fix(claude): remove vercel ai sdk
Browse files Browse the repository at this point in the history
see #40
  • Loading branch information
UNICKCHENG committed Aug 10, 2023
1 parent bc872dd commit a7ee0cd
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions app/api/claude/append_message/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextRequest } from 'next/server'
import { cookies } from 'next/headers'
import { AnthropicStream, StreamingTextResponse } from 'ai'

/**
* 发送消息
Expand All @@ -19,6 +18,37 @@ export async function POST(request: NextRequest) {
body: JSON.stringify(await request.json()),
}
const response = await fetch(base_url, init);
const stream = AnthropicStream(response)
return new StreamingTextResponse(stream);
const stream = await claudeWebApiStream(response);
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache, no-transform',
},
});
}

async function claudeWebApiStream(response: any) {
const encoder = new TextEncoder();
const decoder = new TextDecoder("utf-8");
const reader = response.body?.getReader();

return new ReadableStream({
async pull(controller) {
const { value, done } = await reader?.read();
if (done) {
controller.close();
} else {
let content: string = '';
const lines = decoder.decode(value).split("\n");
lines.map((line) => line.replace(/^data: /, "").trim())
.filter((line) => line !== "")
.map((line) => JSON.parse(line))
.forEach((line) => {
content += line.completion ? line.completion as string : '';
});
controller.enqueue(encoder.encode(content));
}
},
})
}

0 comments on commit a7ee0cd

Please sign in to comment.