@forwardimpact/svcembedding 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Zachary Rice
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.js ADDED
@@ -0,0 +1,54 @@
1
+ import { services } from "@forwardimpact/librpc";
2
+
3
+ const { EmbeddingBase } = services;
4
+
5
+ /**
6
+ * gRPC service that produces text embeddings by delegating to a
7
+ * Text Embeddings Inference (TEI) HTTP backend over its OpenAI-compatible
8
+ * `/v1/embeddings` endpoint. The TEI process is spawned by `server.js`; this
9
+ * class is a thin adapter that translates the proto request/response shapes
10
+ * to and from TEI's HTTP payload.
11
+ *
12
+ * Implements the `Embedding.CreateEmbeddings` RPC defined in
13
+ * `proto/embedding.proto`; see `generated/services/embedding/service.js` for
14
+ * the `EmbeddingBase` it extends.
15
+ */
16
+ export class EmbeddingService extends EmbeddingBase {
17
+ #backendUrl;
18
+
19
+ /**
20
+ * @param {import("@forwardimpact/libconfig").ServiceConfig} config -
21
+ * Service configuration from `createServiceConfig("embedding")`.
22
+ * @param {string} backendUrl - Base URL of the TEI HTTP backend
23
+ * (e.g. `http://127.0.0.1:8090`), without a trailing slash. Required;
24
+ * construction throws if empty.
25
+ */
26
+ constructor(config, backendUrl) {
27
+ super(config);
28
+ if (!backendUrl) throw new Error("backendUrl is required");
29
+ this.#backendUrl = backendUrl;
30
+ }
31
+
32
+ /**
33
+ * Embed one or more input strings via the TEI backend.
34
+ *
35
+ * @param {{input: string[]}} req - Proto-decoded `EmbeddingsRequest`.
36
+ * `input` is the list of text strings to embed; order is preserved in
37
+ * the response.
38
+ * @returns {Promise<{data: Array<{values: number[]}>}>} A proto-shaped
39
+ * `EmbeddingsResponse` with one `EmbeddingVector` per input, in the same
40
+ * order. `values` is the dense embedding from the configured TEI model.
41
+ * @throws {Error} If the TEI backend returns a non-2xx status. The error
42
+ * message includes the HTTP status code.
43
+ */
44
+ async CreateEmbeddings(req) {
45
+ const res = await fetch(`${this.#backendUrl}/v1/embeddings`, {
46
+ method: "POST",
47
+ headers: { "Content-Type": "application/json" },
48
+ body: JSON.stringify({ input: req.input, model: "default" }),
49
+ });
50
+ if (!res.ok) throw new Error(`TEI request failed: ${res.status}`);
51
+ const body = await res.json();
52
+ return { data: body.data.map((d) => ({ values: d.embedding })) };
53
+ }
54
+ }
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@forwardimpact/svcembedding",
3
+ "version": "0.1.1",
4
+ "description": "Text embeddings over gRPC — semantic representation without each product running its own inference.",
5
+ "keywords": [
6
+ "embedding",
7
+ "vector",
8
+ "inference",
9
+ "grpc",
10
+ "agent"
11
+ ],
12
+ "homepage": "https://www.forwardimpact.team",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/forwardimpact/monorepo.git",
16
+ "directory": "services/embedding"
17
+ },
18
+ "license": "Apache-2.0",
19
+ "author": "D. Olsson <hi@senzilla.io>",
20
+ "jobs": [
21
+ {
22
+ "user": "Platform Builders",
23
+ "goal": "Ground Agents in Context",
24
+ "trigger": "An agent needs to answer relationship questions, search by meaning, or read activity data, and the only alternative is direct database access and per-product plumbing.",
25
+ "bigHire": "give agents graph queries, semantic search, embeddings, and activity data through shared services that never leak schema or credentials.",
26
+ "littleHire": "call one gRPC method instead of wiring HTTP, auth, and retries per product.",
27
+ "competesWith": "direct database access from agents; per-product retrieval endpoints; inline fetch calls; external search infrastructure; skipping semantic search entirely"
28
+ }
29
+ ],
30
+ "type": "module",
31
+ "main": "index.js",
32
+ "bin": {
33
+ "fit-svcembedding": "./server.js"
34
+ },
35
+ "files": [
36
+ "proto/",
37
+ "server.js"
38
+ ],
39
+ "scripts": {
40
+ "dev": "node --watch server.js",
41
+ "start": "bun server.js",
42
+ "test": "bun test test/*.test.js"
43
+ },
44
+ "dependencies": {
45
+ "@forwardimpact/libcli": "^0.1.14",
46
+ "@forwardimpact/libconfig": "^0.1.58",
47
+ "@forwardimpact/libpreflight": "^0.1.0",
48
+ "@forwardimpact/librpc": "^0.1.77",
49
+ "@forwardimpact/libtelemetry": "^0.1.41",
50
+ "@forwardimpact/libutil": "^0.1.85"
51
+ },
52
+ "devDependencies": {
53
+ "@forwardimpact/libmock": "^0.1.0"
54
+ },
55
+ "engines": {
56
+ "bun": ">=1.2.0",
57
+ "node": ">=22.0.0"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public"
61
+ }
62
+ }
@@ -0,0 +1,22 @@
1
+ syntax = "proto3";
2
+
3
+ package embedding;
4
+
5
+ service Embedding {
6
+ rpc CreateEmbeddings(EmbeddingsRequest) returns (EmbeddingsResponse);
7
+ }
8
+
9
+ message EmbeddingsRequest {
10
+ // Text strings to embed
11
+ repeated string input = 1;
12
+ }
13
+
14
+ message EmbeddingVector {
15
+ // Embedding values for a single input
16
+ repeated float values = 1;
17
+ }
18
+
19
+ message EmbeddingsResponse {
20
+ // One vector per input string, in order
21
+ repeated EmbeddingVector data = 1;
22
+ }
package/server.js ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ import "@forwardimpact/libpreflight/node22";
3
+
4
+ import { serverFlagsShortCircuit } from "@forwardimpact/libcli/server-flags";
5
+ import { spawn } from "node:child_process";
6
+ import { Server } from "@forwardimpact/librpc";
7
+ import { createServiceConfig } from "@forwardimpact/libconfig";
8
+ import { createTracer } from "@forwardimpact/librpc";
9
+ import { createLogger } from "@forwardimpact/libtelemetry";
10
+ import { createDefaultRuntime } from "@forwardimpact/libutil/runtime";
11
+
12
+ import { EmbeddingService } from "./index.js";
13
+
14
+ const handled = serverFlagsShortCircuit({
15
+ name: "fit-svcembedding",
16
+ description: "Text embeddings gRPC service",
17
+ packageJsonUrl: new URL("./package.json", import.meta.url),
18
+ argv: process.argv.slice(2),
19
+ });
20
+
21
+ if (!handled) {
22
+ const config = await createServiceConfig("embedding", {
23
+ port: 3015,
24
+ backend_port: 8090,
25
+ model: "BAAI/bge-small-en-v1.5",
26
+ });
27
+
28
+ const runtime = createDefaultRuntime();
29
+ const logger = createLogger("embedding", runtime);
30
+ const tracer = await createTracer("embedding");
31
+
32
+ const backend_port = config.backend_port;
33
+ const model = config.model;
34
+ const backendUrl = `http://127.0.0.1:${backend_port}`;
35
+
36
+ const tei = spawn(
37
+ "text-embeddings-router",
38
+ ["--model-id", model, "--port", String(backend_port), "--json-output"],
39
+ { stdio: "inherit" },
40
+ );
41
+
42
+ for (const sig of ["SIGTERM", "SIGINT"]) {
43
+ process.on(sig, () => {
44
+ tei.kill(sig);
45
+ });
46
+ }
47
+
48
+ tei.on("exit", (code, signal) => {
49
+ process.exit(signal ? 1 : (code ?? 1));
50
+ });
51
+
52
+ const service = new EmbeddingService(config, backendUrl);
53
+ const server = new Server(service, config, { logger, tracer, runtime });
54
+
55
+ await server.start();
56
+ }