-
-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathmock-openai-embedding-server.mjs
More file actions
181 lines (160 loc) · 6.21 KB
/
mock-openai-embedding-server.mjs
File metadata and controls
181 lines (160 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import crypto from 'node:crypto';
import http from 'node:http';
const host = process.env.NEO_TEST_EMBEDDING_HOST || '0.0.0.0';
const port = Number(process.env.NEO_TEST_EMBEDDING_PORT || 11434);
const dimensions = Number(process.env.NEO_TEST_EMBEDDING_DIMENSIONS || 4096);
const modelName = process.env.NEO_TEST_EMBEDDING_MODEL || 'text-embedding-qwen3-embedding-8b';
const chatModel = process.env.NEO_TEST_CHAT_MODEL || process.env.NEO_OPENAI_COMPATIBLE_MODEL || 'gemma-4-31b-it';
/**
* @summary Reads a JSON request body.
* Reads a JSON request body.
* @param {http.IncomingMessage} request The incoming HTTP request.
* @returns {Promise<Object>} The parsed JSON payload.
*/
function readJson(request) {
return new Promise((resolve, reject) => {
let body = '';
request.on('data', chunk => body += chunk);
request.on('error', reject);
request.on('end', () => {
try {
resolve(body ? JSON.parse(body) : {});
} catch (error) {
reject(error);
}
});
});
}
/**
* @summary Sends a JSON response.
* Sends a JSON response.
* @param {http.ServerResponse} response The outgoing HTTP response.
* @param {Number} statusCode The HTTP status code.
* @param {Object} payload The JSON payload.
* @returns {void}
*/
function sendJson(response, statusCode, payload) {
response.writeHead(statusCode, {'content-type': 'application/json'});
response.end(JSON.stringify(payload));
}
/**
* @summary Builds a deterministic embedding vector for integration tests.
* Builds a deterministic embedding vector for integration tests.
* @param {String} text The input text.
* @returns {Number[]} A vector matching the configured embedding dimensions.
*/
function buildEmbedding(text) {
const digest = crypto.createHash('sha256').update(String(text)).digest();
return Array.from({length: dimensions}, (_, index) => {
const byte = digest[index % digest.length];
return Number((((byte / 255) * 2 - 1) / Math.sqrt(dimensions)).toFixed(8));
});
}
/**
* @summary Extracts a deterministic chat response from an OpenAI-compatible payload.
* Extracts a deterministic chat response from an OpenAI-compatible payload.
* @param {Object} payload The OpenAI-compatible chat completion payload.
* @returns {String} A deterministic response body.
*/
function buildChatResponse(payload) {
const messages = Array.isArray(payload.messages) ? payload.messages : [];
const lastUser = [...messages].reverse().find(message => message.role === 'user');
const content = lastUser?.content || messages.at(-1)?.content || '';
const fullPrompt = messages.map(message => message.content).join('\n');
if (fullPrompt.includes('session_artifact') && fullPrompt.includes('cloud-readiness-graph-sentinel')) {
return JSON.stringify({
a2a_version: '1.0',
agent_id : 'neo-integration',
session_artifact: {
graph: {
nodes: [{
id : 'CONCEPT:cloud-readiness-graph-sentinel',
type : 'CONCEPT',
name : 'cloud-readiness-graph-sentinel',
description: 'Deterministic provider-readiness node emitted by the mock OpenAI-compatible server.'
}],
edges: []
}
}
});
}
return JSON.stringify({
provider: 'openAiCompatible',
model : payload.model || chatModel,
echo : String(content).slice(0, 240)
});
}
/**
* @summary Sends a streaming OpenAI-compatible chat completion response.
* Sends a streaming OpenAI-compatible chat completion response.
* @param {http.ServerResponse} response The outgoing HTTP response.
* @param {String} content The deterministic completion content.
* @returns {void}
*/
function sendChatStream(response, content) {
response.writeHead(200, {
'content-type' : 'text/event-stream',
'cache-control': 'no-cache',
connection : 'keep-alive'
});
response.write(`data: ${JSON.stringify({choices: [{delta: {content}}]})}\n\n`);
response.write('data: [DONE]\n\n');
response.end();
}
const server = http.createServer(async (request, response) => {
if (request.method === 'GET' && request.url === '/health') {
sendJson(response, 200, {status: 'ok', dimensions, embeddingModel: modelName, chatModel});
return;
}
if (request.method !== 'POST') {
sendJson(response, 404, {error: 'Not found'});
return;
}
try {
const payload = await readJson(request);
if (request.url === '/v1/embeddings') {
const inputs = Array.isArray(payload.input) ? payload.input : [payload.input ?? ''];
sendJson(response, 200, {
object: 'list',
model : payload.model || modelName,
data : inputs.map((input, index) => ({
object : 'embedding',
index,
embedding: buildEmbedding(input)
})),
usage : {
prompt_tokens: 0,
total_tokens : 0
}
});
return;
}
if (request.url === '/v1/chat/completions') {
const content = buildChatResponse(payload);
if (payload.stream !== false) {
sendChatStream(response, content);
return;
}
sendJson(response, 200, {
id : 'chatcmpl-neo-integration',
object : 'chat.completion',
model : payload.model || chatModel,
choices: [{
index : 0,
message: {
role: 'assistant',
content
},
finish_reason: 'stop'
}]
});
return;
}
sendJson(response, 404, {error: 'Not found'});
} catch (error) {
sendJson(response, 400, {error: error.message});
}
});
server.listen(port, host, () => {
console.log(`[mock-openai-compatible-server] listening on ${host}:${port}`);
});