Skip to content

Commit

Permalink
add: stream response
Browse files Browse the repository at this point in the history
  • Loading branch information
mannjaro committed Sep 21, 2024
1 parent 7f9bde9 commit 7ac5a1c
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 14 deletions.
Binary file modified bun.lockb
Binary file not shown.
Binary file modified lambda/bedrock-proxy/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions lambda/bedrock-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@aws-sdk/client-bedrock-runtime": "^3.642.0",
"@hono/zod-validator": "^0.2.2",
"@types/node": "^22.5.4",
"dotenv": "^16.4.5",
"hono": "^4.5.10",
"openai": "^4.57.0",
"zod": "^3.23.8"
Expand Down
8 changes: 3 additions & 5 deletions lambda/bedrock-proxy/src/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ chat.post(
async (c) => {
const chatRequest = await c.req.valid("json");
const model = (() => {
if (chatRequest.model.startsWith("gpt")) {
return new OpenAIModel();
} else {
return new BedrockModel();
}
return chatRequest.model.startsWith("gpt")
? new OpenAIModel()
: new BedrockModel();
})();
if (chatRequest.stream) {
return streamText(c, async (stream) => {
Expand Down
Empty file.
21 changes: 14 additions & 7 deletions lambda/bedrock-proxy/src/model/openai/chat.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { BaseModel } from "../base";
import OpenAI from "openai";
import type { ChatRequest } from "../../schema/request/chat";
import type { StreamingApi } from "hono/utils/stream";

export class OpenAIModel extends BaseModel {
async _invokeOpenAI(chatRequest: ChatRequest) {
const client = new OpenAI();
return client.chat.completions.create(chatRequest);
}
async chat(chatRequest: ChatRequest) {
const response = await this._invokeOpenAI(chatRequest);
const client = new OpenAI();
const response = await client.chat.completions.create(chatRequest);
return response;
}
async chatStream(chatRequest: ChatRequest) {
const response = await this._invokeOpenAI(chatRequest);
async chatStream(chatRequest: ChatRequest, stream: StreamingApi) {
const client = new OpenAI();
const response = await client.chat.completions.create({
...chatRequest,
stream: true,
});
const encoder = new TextEncoder();
for await (const chunk of response) {
console.log(chunk);
await stream.write(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
return response;
}
}
8 changes: 7 additions & 1 deletion lib/cloudfront-lambda-stack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import * as cdk from "aws-cdk-lib";
import * as dotenv from "dotenv";

dotenv.config();

import type { Construct } from "constructs";
import { ProxyLambda } from "./construct/lambda";

export class CloudfrontLambdaStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new ProxyLambda(this, "BedrockProxy");
new ProxyLambda(this, "BedrockProxy", {
OPENAI_API_KEY: process.env.OPENAI_API_KEY ?? "",
});
}
}
9 changes: 8 additions & 1 deletion lib/construct/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import * as lambda from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";

export interface ProxyLambdaProps extends cdk.StackProps {
OPENAI_API_KEY: string;
}

export class ProxyLambda extends Construct {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
constructor(scope: Construct, id: string, props: ProxyLambdaProps) {
super(scope, id);
const role = new iam.Role(this, "Role", {
assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
Expand Down Expand Up @@ -33,6 +37,9 @@ export class ProxyLambda extends Construct {
timeout: cdk.Duration.seconds(30),
architecture: lambda.Architecture.ARM_64,
layers: [paramStoreLayer],
environment: {
OPENAI_API_KEY: props.OPENAI_API_KEY,
},
});
fn.addFunctionUrl({
authType: lambda.FunctionUrlAuthType.NONE,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@hono/zod-validator": "^0.2.2",
"aws-cdk-lib": "^2.158.0",
"constructs": "^10.0.0",
"dotenv": "^16.4.5",
"hono": "^4.5.11",
"openai": "^4.61.0",
"source-map-support": "^0.5.21",
Expand Down

0 comments on commit 7ac5a1c

Please sign in to comment.