@pixygon/chatbot-server 0.1.1 → 0.2.0

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.
Files changed (2) hide show
  1. package/dist/index.js +33 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -134,12 +134,41 @@ function createAiClient(cfg) {
134
134
  return { content, model: `${model}/${version}`, tokensInput, tokensOutput, costUsd };
135
135
  },
136
136
  async embed(text, opts = {}) {
137
+ const model = opts.model || "text-embedding-3-small";
137
138
  if (!cfg.openaiApiKey) {
138
- const err = new Error("OPENAI_API_KEY not set");
139
- err.code = "OPENAI_UNCONFIGURED";
140
- throw err;
139
+ if (!cfg.pixygonApiKey) {
140
+ const err = new Error("PIXYGON_API_KEY (or OPENAI_API_KEY) required for embeddings");
141
+ err.code = "EMBED_UNCONFIGURED";
142
+ throw err;
143
+ }
144
+ const formData = new FormData();
145
+ formData.append("type", "embedding");
146
+ formData.append("model", "openai");
147
+ formData.append("version", model);
148
+ formData.append("prompt", text);
149
+ const res2 = await fetch(PIXYGON_AI_URL, {
150
+ method: "POST",
151
+ headers: { "x-api-key": cfg.pixygonApiKey },
152
+ body: formData
153
+ });
154
+ if (!res2.ok) {
155
+ const body = await res2.text().catch(() => "");
156
+ const err = new Error(`Pixygon embed failed: ${res2.status} \u2014 ${body.slice(0, 300)}`);
157
+ err.code = "PIXYGON_EMBED_FAILED";
158
+ err.status = res2.status;
159
+ throw err;
160
+ }
161
+ const payload2 = await res2.json();
162
+ const vector = payload2?.embedding;
163
+ if (!Array.isArray(vector) || vector.length === 0) {
164
+ const err = new Error("Pixygon embed response had no vector");
165
+ err.code = "PIXYGON_EMBED_EMPTY";
166
+ throw err;
167
+ }
168
+ const tokens2 = Number(payload2?.tokens ?? approxTokens(text));
169
+ const costUsd2 = tokens2 / 1e3 * EMBED_RATE;
170
+ return { embedding: vector, tokens: tokens2, costUsd: costUsd2, dimensions: vector.length };
141
171
  }
142
- const model = opts.model || "text-embedding-3-small";
143
172
  const res = await fetch(`${OPENAI_API_URL}/embeddings`, {
144
173
  method: "POST",
145
174
  headers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixygon/chatbot-server",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "RAG chatbot + analytics for Node + Mongoose + Express hosts.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",