@animalabs/membrane 0.2.0 → 0.2.2
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.
|
@@ -22,10 +22,27 @@ export interface OpenAICompletionsAdapterConfig {
|
|
|
22
22
|
/** Additional headers to include with requests */
|
|
23
23
|
extraHeaders?: Record<string, string>;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
25
|
+
* Name of the assistant participant (default: 'Assistant')
|
|
26
|
+
* Used to identify which messages are from the assistant and to
|
|
27
|
+
* add the final prompt prefix for completion.
|
|
27
28
|
*/
|
|
28
|
-
|
|
29
|
+
assistantName?: string;
|
|
30
|
+
/**
|
|
31
|
+
* End-of-turn token to append after each message (default: '<|eot|>')
|
|
32
|
+
* Set to empty string or null to disable.
|
|
33
|
+
*
|
|
34
|
+
* Example output with default:
|
|
35
|
+
* Alice: Hello<|eot|>
|
|
36
|
+
*
|
|
37
|
+
* Claude: Hi there!<|eot|>
|
|
38
|
+
*/
|
|
39
|
+
eotToken?: string | null;
|
|
40
|
+
/**
|
|
41
|
+
* Additional stop sequences beyond auto-generated participant-based ones.
|
|
42
|
+
* By default, stop sequences are generated from participant names in the
|
|
43
|
+
* conversation (e.g., "\n\nAlice:", "\nBob:").
|
|
44
|
+
*/
|
|
45
|
+
extraStopSequences?: string[];
|
|
29
46
|
/**
|
|
30
47
|
* Whether to warn when images are stripped from context (default: true)
|
|
31
48
|
*/
|
|
@@ -37,18 +54,28 @@ export declare class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
37
54
|
private apiKey;
|
|
38
55
|
private defaultMaxTokens;
|
|
39
56
|
private extraHeaders;
|
|
40
|
-
private
|
|
57
|
+
private assistantName;
|
|
58
|
+
private eotToken;
|
|
59
|
+
private extraStopSequences;
|
|
41
60
|
private warnOnImageStrip;
|
|
42
61
|
constructor(config: OpenAICompletionsAdapterConfig);
|
|
43
62
|
supportsModel(_modelId: string): boolean;
|
|
44
63
|
complete(request: ProviderRequest, options?: ProviderRequestOptions): Promise<ProviderResponse>;
|
|
45
64
|
stream(request: ProviderRequest, callbacks: StreamCallbacks, options?: ProviderRequestOptions): Promise<ProviderResponse>;
|
|
46
65
|
/**
|
|
47
|
-
* Serialize messages to
|
|
66
|
+
* Serialize messages to "Participant: content<eot>" format for base models.
|
|
67
|
+
* Uses actual participant names from messages.
|
|
48
68
|
* Images are stripped from content.
|
|
49
69
|
*/
|
|
50
|
-
serializeToPrompt(messages: any[]):
|
|
51
|
-
|
|
70
|
+
serializeToPrompt(messages: any[]): {
|
|
71
|
+
prompt: string;
|
|
72
|
+
participants: Set<string>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Generate stop sequences from participant names.
|
|
76
|
+
* Prevents the model from generating turns for other participants.
|
|
77
|
+
*/
|
|
78
|
+
private generateStopSequences;
|
|
52
79
|
private extractTextContent;
|
|
53
80
|
private getHeaders;
|
|
54
81
|
private buildRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-completions.d.ts","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EAEhB,MAAM,mBAAmB,CAAC;AA2C3B,MAAM,WAAW,8BAA8B;IAC7C,wEAAwE;IACxE,OAAO,EAAE,MAAM,CAAC;IAEhB,2CAA2C;IAC3C,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,+EAA+E;IAC/E,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,yBAAyB;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B,kDAAkD;IAClD,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEtC
|
|
1
|
+
{"version":3,"file":"openai-completions.d.ts","sourceRoot":"","sources":["../../src/providers/openai-completions.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,CACV,OAAO,EAAE,eAAe,EACxB,SAAS,EAAE,eAAe,EAC1B,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,gBAAgB,CAAC;IAkE5B;;;;OAIG;IACH,iBAAiB,CAAC,QAAQ,EAAE,GAAG,EAAE,GAAG;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;KAAE;IAoCjF;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAe7B,OAAO,CAAC,kBAAkB;IA4B1B,OAAO,CAAC,UAAU;IAalB,OAAO,CAAC,YAAY;YAiCN,WAAW;IAgBzB,OAAO,CAAC,aAAa;IAkBrB,OAAO,CAAC,qBAAqB;IAoB7B,OAAO,CAAC,aAAa;IASrB,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,WAAW;CAqCpB"}
|
|
@@ -19,7 +19,9 @@ export class OpenAICompletionsAdapter {
|
|
|
19
19
|
apiKey;
|
|
20
20
|
defaultMaxTokens;
|
|
21
21
|
extraHeaders;
|
|
22
|
-
|
|
22
|
+
assistantName;
|
|
23
|
+
eotToken;
|
|
24
|
+
extraStopSequences;
|
|
23
25
|
warnOnImageStrip;
|
|
24
26
|
constructor(config) {
|
|
25
27
|
if (!config.baseURL) {
|
|
@@ -30,7 +32,9 @@ export class OpenAICompletionsAdapter {
|
|
|
30
32
|
this.apiKey = config.apiKey ?? '';
|
|
31
33
|
this.defaultMaxTokens = config.defaultMaxTokens ?? 4096;
|
|
32
34
|
this.extraHeaders = config.extraHeaders ?? {};
|
|
33
|
-
this.
|
|
35
|
+
this.assistantName = config.assistantName ?? 'Assistant';
|
|
36
|
+
this.eotToken = config.eotToken === null ? '' : (config.eotToken ?? '<|eot|>');
|
|
37
|
+
this.extraStopSequences = config.extraStopSequences ?? [];
|
|
34
38
|
this.warnOnImageStrip = config.warnOnImageStrip ?? true;
|
|
35
39
|
}
|
|
36
40
|
supportsModel(_modelId) {
|
|
@@ -103,36 +107,54 @@ export class OpenAICompletionsAdapter {
|
|
|
103
107
|
// Prompt Serialization
|
|
104
108
|
// ============================================================================
|
|
105
109
|
/**
|
|
106
|
-
* Serialize messages to
|
|
110
|
+
* Serialize messages to "Participant: content<eot>" format for base models.
|
|
111
|
+
* Uses actual participant names from messages.
|
|
107
112
|
* Images are stripped from content.
|
|
108
113
|
*/
|
|
109
114
|
serializeToPrompt(messages) {
|
|
110
115
|
const parts = [];
|
|
116
|
+
const participants = new Set();
|
|
111
117
|
let hasStrippedImages = false;
|
|
112
118
|
for (const msg of messages) {
|
|
113
|
-
|
|
114
|
-
const
|
|
119
|
+
// Get participant name (supports both 'participant' and 'role' fields)
|
|
120
|
+
const participant = msg.participant || msg.role || 'Unknown';
|
|
121
|
+
participants.add(participant);
|
|
115
122
|
// Extract text content, strip images
|
|
116
123
|
const textContent = this.extractTextContent(msg.content);
|
|
117
124
|
if (textContent.hadImages) {
|
|
118
125
|
hasStrippedImages = true;
|
|
119
126
|
}
|
|
120
127
|
if (textContent.text) {
|
|
121
|
-
|
|
128
|
+
// Append EOT token after content (on same line)
|
|
129
|
+
const eot = this.eotToken;
|
|
130
|
+
parts.push(`${participant}: ${textContent.text}${eot}`);
|
|
122
131
|
}
|
|
123
132
|
}
|
|
124
133
|
if (hasStrippedImages && this.warnOnImageStrip) {
|
|
125
134
|
console.warn('[OpenAICompletionsAdapter] Images were stripped from context (not supported in completions mode)');
|
|
126
135
|
}
|
|
127
|
-
// Add final
|
|
128
|
-
parts.push(
|
|
129
|
-
return
|
|
136
|
+
// Add final assistant prefix to prompt completion (no EOT - model generates this)
|
|
137
|
+
parts.push(`${this.assistantName}:`);
|
|
138
|
+
return {
|
|
139
|
+
prompt: parts.join('\n\n'),
|
|
140
|
+
participants,
|
|
141
|
+
};
|
|
130
142
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Generate stop sequences from participant names.
|
|
145
|
+
* Prevents the model from generating turns for other participants.
|
|
146
|
+
*/
|
|
147
|
+
generateStopSequences(participants) {
|
|
148
|
+
const stops = [];
|
|
149
|
+
for (const participant of participants) {
|
|
150
|
+
// Skip the assistant - we don't want to stop on its own name
|
|
151
|
+
if (participant === this.assistantName)
|
|
152
|
+
continue;
|
|
153
|
+
// Add both "\n\nName:" and "\nName:" variants
|
|
154
|
+
stops.push(`\n\n${participant}:`);
|
|
155
|
+
stops.push(`\n${participant}:`);
|
|
134
156
|
}
|
|
135
|
-
return
|
|
157
|
+
return stops;
|
|
136
158
|
}
|
|
137
159
|
extractTextContent(content) {
|
|
138
160
|
if (typeof content === 'string') {
|
|
@@ -168,7 +190,7 @@ export class OpenAICompletionsAdapter {
|
|
|
168
190
|
return headers;
|
|
169
191
|
}
|
|
170
192
|
buildRequest(request) {
|
|
171
|
-
const prompt = this.serializeToPrompt(request.messages);
|
|
193
|
+
const { prompt, participants } = this.serializeToPrompt(request.messages);
|
|
172
194
|
const params = {
|
|
173
195
|
model: request.model,
|
|
174
196
|
prompt,
|
|
@@ -177,9 +199,11 @@ export class OpenAICompletionsAdapter {
|
|
|
177
199
|
if (request.temperature !== undefined) {
|
|
178
200
|
params.temperature = request.temperature;
|
|
179
201
|
}
|
|
180
|
-
//
|
|
202
|
+
// Generate stop sequences from participant names + EOT token + any extras
|
|
181
203
|
const stopSequences = [
|
|
182
|
-
...this.
|
|
204
|
+
...this.generateStopSequences(participants),
|
|
205
|
+
...(this.eotToken ? [this.eotToken] : []),
|
|
206
|
+
...this.extraStopSequences,
|
|
183
207
|
...(request.stopSequences || []),
|
|
184
208
|
];
|
|
185
209
|
if (stopSequences.length > 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-completions.js","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAUH,OAAO,EACL,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,SAAS,EACT,WAAW,EACX,UAAU,EACV,Y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,UAAU,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;YAC3C,CAAC;YAED,IAAI,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;gBACjG,OAAO,YAAY,CAAC,OAAO,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;YAClD,CAAC;QACH,CAAC;QAED,OAAO,IAAI,aAAa,CAAC;YACvB,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;YAC/D,SAAS,EAAE,KAAK;YAChB,QAAQ,EAAE,KAAK;YACf,UAAU;SACX,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -77,10 +77,29 @@ export interface OpenAICompletionsAdapterConfig {
|
|
|
77
77
|
extraHeaders?: Record<string, string>;
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
80
|
+
* Name of the assistant participant (default: 'Assistant')
|
|
81
|
+
* Used to identify which messages are from the assistant and to
|
|
82
|
+
* add the final prompt prefix for completion.
|
|
82
83
|
*/
|
|
83
|
-
|
|
84
|
+
assistantName?: string;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* End-of-turn token to append after each message (default: '<|eot|>')
|
|
88
|
+
* Set to empty string or null to disable.
|
|
89
|
+
*
|
|
90
|
+
* Example output with default:
|
|
91
|
+
* Alice: Hello<|eot|>
|
|
92
|
+
*
|
|
93
|
+
* Claude: Hi there!<|eot|>
|
|
94
|
+
*/
|
|
95
|
+
eotToken?: string | null;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Additional stop sequences beyond auto-generated participant-based ones.
|
|
99
|
+
* By default, stop sequences are generated from participant names in the
|
|
100
|
+
* conversation (e.g., "\n\nAlice:", "\nBob:").
|
|
101
|
+
*/
|
|
102
|
+
extraStopSequences?: string[];
|
|
84
103
|
|
|
85
104
|
/**
|
|
86
105
|
* Whether to warn when images are stripped from context (default: true)
|
|
@@ -98,7 +117,9 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
98
117
|
private apiKey: string;
|
|
99
118
|
private defaultMaxTokens: number;
|
|
100
119
|
private extraHeaders: Record<string, string>;
|
|
101
|
-
private
|
|
120
|
+
private assistantName: string;
|
|
121
|
+
private eotToken: string;
|
|
122
|
+
private extraStopSequences: string[];
|
|
102
123
|
private warnOnImageStrip: boolean;
|
|
103
124
|
|
|
104
125
|
constructor(config: OpenAICompletionsAdapterConfig) {
|
|
@@ -111,7 +132,9 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
111
132
|
this.apiKey = config.apiKey ?? '';
|
|
112
133
|
this.defaultMaxTokens = config.defaultMaxTokens ?? 4096;
|
|
113
134
|
this.extraHeaders = config.extraHeaders ?? {};
|
|
114
|
-
this.
|
|
135
|
+
this.assistantName = config.assistantName ?? 'Assistant';
|
|
136
|
+
this.eotToken = config.eotToken === null ? '' : (config.eotToken ?? '<|eot|>');
|
|
137
|
+
this.extraStopSequences = config.extraStopSequences ?? [];
|
|
115
138
|
this.warnOnImageStrip = config.warnOnImageStrip ?? true;
|
|
116
139
|
}
|
|
117
140
|
|
|
@@ -204,16 +227,19 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
204
227
|
// ============================================================================
|
|
205
228
|
|
|
206
229
|
/**
|
|
207
|
-
* Serialize messages to
|
|
230
|
+
* Serialize messages to "Participant: content<eot>" format for base models.
|
|
231
|
+
* Uses actual participant names from messages.
|
|
208
232
|
* Images are stripped from content.
|
|
209
233
|
*/
|
|
210
|
-
serializeToPrompt(messages: any[]): string {
|
|
234
|
+
serializeToPrompt(messages: any[]): { prompt: string; participants: Set<string> } {
|
|
211
235
|
const parts: string[] = [];
|
|
236
|
+
const participants = new Set<string>();
|
|
212
237
|
let hasStrippedImages = false;
|
|
213
238
|
|
|
214
239
|
for (const msg of messages) {
|
|
215
|
-
|
|
216
|
-
const
|
|
240
|
+
// Get participant name (supports both 'participant' and 'role' fields)
|
|
241
|
+
const participant = msg.participant || msg.role || 'Unknown';
|
|
242
|
+
participants.add(participant);
|
|
217
243
|
|
|
218
244
|
// Extract text content, strip images
|
|
219
245
|
const textContent = this.extractTextContent(msg.content);
|
|
@@ -222,7 +248,9 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
222
248
|
}
|
|
223
249
|
|
|
224
250
|
if (textContent.text) {
|
|
225
|
-
|
|
251
|
+
// Append EOT token after content (on same line)
|
|
252
|
+
const eot = this.eotToken;
|
|
253
|
+
parts.push(`${participant}: ${textContent.text}${eot}`);
|
|
226
254
|
}
|
|
227
255
|
}
|
|
228
256
|
|
|
@@ -230,17 +258,32 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
230
258
|
console.warn('[OpenAICompletionsAdapter] Images were stripped from context (not supported in completions mode)');
|
|
231
259
|
}
|
|
232
260
|
|
|
233
|
-
// Add final
|
|
234
|
-
parts.push(
|
|
261
|
+
// Add final assistant prefix to prompt completion (no EOT - model generates this)
|
|
262
|
+
parts.push(`${this.assistantName}:`);
|
|
235
263
|
|
|
236
|
-
return
|
|
264
|
+
return {
|
|
265
|
+
prompt: parts.join('\n\n'),
|
|
266
|
+
participants,
|
|
267
|
+
};
|
|
237
268
|
}
|
|
238
269
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Generate stop sequences from participant names.
|
|
272
|
+
* Prevents the model from generating turns for other participants.
|
|
273
|
+
*/
|
|
274
|
+
private generateStopSequences(participants: Set<string>): string[] {
|
|
275
|
+
const stops: string[] = [];
|
|
276
|
+
|
|
277
|
+
for (const participant of participants) {
|
|
278
|
+
// Skip the assistant - we don't want to stop on its own name
|
|
279
|
+
if (participant === this.assistantName) continue;
|
|
280
|
+
|
|
281
|
+
// Add both "\n\nName:" and "\nName:" variants
|
|
282
|
+
stops.push(`\n\n${participant}:`);
|
|
283
|
+
stops.push(`\n${participant}:`);
|
|
242
284
|
}
|
|
243
|
-
|
|
285
|
+
|
|
286
|
+
return stops;
|
|
244
287
|
}
|
|
245
288
|
|
|
246
289
|
private extractTextContent(content: any): { text: string; hadImages: boolean } {
|
|
@@ -285,7 +328,7 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
285
328
|
}
|
|
286
329
|
|
|
287
330
|
private buildRequest(request: ProviderRequest): CompletionsRequest {
|
|
288
|
-
const prompt = this.serializeToPrompt(request.messages as any[]);
|
|
331
|
+
const { prompt, participants } = this.serializeToPrompt(request.messages as any[]);
|
|
289
332
|
|
|
290
333
|
const params: CompletionsRequest = {
|
|
291
334
|
model: request.model,
|
|
@@ -297,9 +340,11 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
297
340
|
params.temperature = request.temperature;
|
|
298
341
|
}
|
|
299
342
|
|
|
300
|
-
//
|
|
343
|
+
// Generate stop sequences from participant names + EOT token + any extras
|
|
301
344
|
const stopSequences = [
|
|
302
|
-
...this.
|
|
345
|
+
...this.generateStopSequences(participants),
|
|
346
|
+
...(this.eotToken ? [this.eotToken] : []),
|
|
347
|
+
...this.extraStopSequences,
|
|
303
348
|
...(request.stopSequences || []),
|
|
304
349
|
];
|
|
305
350
|
if (stopSequences.length > 0) {
|