-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mannjaro/feature/embedding_model
add: schema
- Loading branch information
Showing
13 changed files
with
248 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Code quality | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
|
||
jobs: | ||
quality: | ||
permissions: | ||
pull-requests: write | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup Biome | ||
uses: biomejs/setup-biome@v2 | ||
with: | ||
version: latest | ||
- name: Run biome check | ||
run: biome check **/*.ts --write | ||
- name: Commit and push | ||
continue-on-error: true | ||
run: | | ||
git config user.email 41898282+github-actions[bot]@users.noreply.github.com | ||
git config user.name github-actions[bot] | ||
git add . | ||
git commit -m "format by bot" | ||
git push | ||
- name: Run Biome | ||
run: biome ci **/*.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"vcs": { | ||
"enabled": true, | ||
"clientKind": "git", | ||
"useIgnoreFile": true, | ||
"defaultBranch": "main" | ||
}, | ||
"files": { | ||
"ignoreUnknown": false, | ||
"ignore": [] | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "space", | ||
"indentWidth": 2 | ||
}, | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true, | ||
"style": { | ||
"useImportType": "info" | ||
} | ||
"recommended": true | ||
} | ||
}, | ||
"formatter": { | ||
"indentStyle": "space", | ||
"indentWidth": 2 | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "double" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { zValidator } from "@hono/zod-validator"; | ||
import { Hono } from "hono"; | ||
import { CohereEmbeddingModel } from "../model/bedrock/embedding"; | ||
|
||
import { EmbeddingRequestSchema } from "../schema/request/embedding"; | ||
|
||
const embedding = new Hono(); | ||
|
||
embedding.post( | ||
"", | ||
zValidator("json", EmbeddingRequestSchema, async (result, c) => { | ||
if (!result.success) { | ||
console.log(await c.req.text()); | ||
return c.json({ message: "Validation failed" }); | ||
} | ||
}), | ||
async (c) => { | ||
const model = new CohereEmbeddingModel(); | ||
const embeddingRequest = await c.req.valid("json"); | ||
const response = await model.embed(embeddingRequest); | ||
return c.json(response); | ||
}, | ||
); | ||
|
||
export { embedding }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { Hono } from "hono"; | ||
|
||
import type { LambdaEvent } from "hono/aws-lambda"; | ||
|
||
import { chat } from "./api/chat"; | ||
import { embedding } from "./api/embedding"; | ||
type Bindings = { | ||
event: LambdaEvent; | ||
}; | ||
|
||
const app = new Hono(); | ||
const app = new Hono<{ Bindings: Bindings }>(); | ||
|
||
app.get("/", (c) => { | ||
return c.text("ok"); | ||
}); | ||
|
||
app.route("/chat", chat); | ||
|
||
app.route("/embeddings", embedding); | ||
export default app; |
27 changes: 13 additions & 14 deletions
27
lambda/bedrock-proxy/src/model/bedrock.ts → ...a/bedrock-proxy/src/model/bedrock/chat.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
BedrockRuntime, | ||
type InvokeModelCommandInput, | ||
ValidationException, | ||
} from "@aws-sdk/client-bedrock-runtime"; | ||
import type { EmbeddingRequest } from "../../schema/request/embedding"; | ||
import type { | ||
Embedding, | ||
EmbeddingResponse, | ||
} from "../../schema/response/embedding"; | ||
|
||
interface InvokeModelArgs { | ||
texts: Array<string>; | ||
input_type: "search_document"; | ||
truncate: "END"; | ||
} | ||
|
||
class BedrockEmbeddingModel { | ||
_invokeModel(args: InvokeModelArgs, modelId: string) { | ||
const bedrockRuntime = new BedrockRuntime({ region: "us-east-1" }); | ||
const input: InvokeModelCommandInput = { | ||
modelId: modelId, | ||
body: JSON.stringify(args), | ||
accept: "application/json", | ||
contentType: "application/json", | ||
}; | ||
try { | ||
const response = bedrockRuntime.invokeModel(input); | ||
return response; | ||
} catch (error) { | ||
if (error instanceof ValidationException) { | ||
throw new Error(`HTTPException: ${error.message}`); | ||
} | ||
throw error; | ||
} | ||
} | ||
_createEmbeddingResponse( | ||
embeddings: Array<number>, | ||
model: string, | ||
inputTokens: number, | ||
outputTokens: number, | ||
encodingFormat: "float" | "base64", | ||
): EmbeddingResponse { | ||
const data: Array<Embedding> = []; | ||
data.push({ | ||
object: "embedding", | ||
index: 0, | ||
embedding: embeddings, | ||
}); | ||
return { | ||
object: "list", | ||
data: data, | ||
model: model, | ||
usage: { | ||
prompt_tokens: inputTokens, | ||
completion_tokens: outputTokens, | ||
total_tokens: inputTokens + outputTokens, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
class CohereEmbeddingModel extends BedrockEmbeddingModel { | ||
_parseRequest(embeddingRequest: EmbeddingRequest): InvokeModelArgs { | ||
const input = embeddingRequest.input; | ||
const texts = (): Array<string> => { | ||
if (typeof input === "string") { | ||
return [input]; | ||
} | ||
if (Array.isArray(input)) { | ||
return input; | ||
} | ||
throw new Error("Invalid input type"); | ||
}; | ||
return { | ||
texts: texts(), | ||
input_type: "search_document", | ||
truncate: "END", | ||
}; | ||
} | ||
async embed(embeddingRequest: EmbeddingRequest): Promise<EmbeddingResponse> { | ||
const response = await this._invokeModel( | ||
this._parseRequest(embeddingRequest), | ||
embeddingRequest.model, | ||
); | ||
const body = JSON.parse(new TextDecoder().decode(response.body)); | ||
console.log(body); | ||
return this._createEmbeddingResponse( | ||
body.embeddings, | ||
embeddingRequest.model, | ||
0, | ||
0, | ||
"float", | ||
); | ||
} | ||
} | ||
|
||
export { CohereEmbeddingModel }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from "zod"; | ||
|
||
export const EmbeddingRequestSchema = z.object({ | ||
input: z.union([z.string(), z.array(z.string())]), | ||
model: z.string(), | ||
encoding_format: z.enum(["float", "base64"]).default("float"), | ||
dimensions: z.number().optional(), | ||
user: z.string().optional(), | ||
}); | ||
|
||
export type EmbeddingRequest = z.infer<typeof EmbeddingRequestSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { z } from "zod"; | ||
|
||
const EmbeddingSchema = z.object({ | ||
object: z.literal("embedding"), | ||
embedding: z.array(z.number()), | ||
index: z.number(), | ||
}); | ||
export const EmbeddingResponseSchema = z.object({ | ||
object: z.literal("list"), | ||
data: z.array( | ||
z.object({ | ||
object: z.literal("embedding"), | ||
index: z.number(), | ||
embedding: z.array(z.number()), | ||
}), | ||
), | ||
model: z.string(), | ||
usage: z.object({ | ||
prompt_tokens: z.number(), | ||
completion_tokens: z.number(), | ||
total_tokens: z.number(), | ||
}), | ||
}); | ||
|
||
export type Embedding = z.infer<typeof EmbeddingSchema>; | ||
export type EmbeddingResponse = z.infer<typeof EmbeddingResponseSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters