@animalabs/membrane 0.2.0 → 0.2.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.
|
@@ -22,10 +22,17 @@ 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
|
+
* Additional stop sequences beyond auto-generated participant-based ones.
|
|
32
|
+
* By default, stop sequences are generated from participant names in the
|
|
33
|
+
* conversation (e.g., "\n\nAlice:", "\nBob:").
|
|
34
|
+
*/
|
|
35
|
+
extraStopSequences?: string[];
|
|
29
36
|
/**
|
|
30
37
|
* Whether to warn when images are stripped from context (default: true)
|
|
31
38
|
*/
|
|
@@ -37,18 +44,27 @@ export declare class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
37
44
|
private apiKey;
|
|
38
45
|
private defaultMaxTokens;
|
|
39
46
|
private extraHeaders;
|
|
40
|
-
private
|
|
47
|
+
private assistantName;
|
|
48
|
+
private extraStopSequences;
|
|
41
49
|
private warnOnImageStrip;
|
|
42
50
|
constructor(config: OpenAICompletionsAdapterConfig);
|
|
43
51
|
supportsModel(_modelId: string): boolean;
|
|
44
52
|
complete(request: ProviderRequest, options?: ProviderRequestOptions): Promise<ProviderResponse>;
|
|
45
53
|
stream(request: ProviderRequest, callbacks: StreamCallbacks, options?: ProviderRequestOptions): Promise<ProviderResponse>;
|
|
46
54
|
/**
|
|
47
|
-
* Serialize messages to
|
|
55
|
+
* Serialize messages to "Participant: content" format for base models.
|
|
56
|
+
* Uses actual participant names from messages.
|
|
48
57
|
* Images are stripped from content.
|
|
49
58
|
*/
|
|
50
|
-
serializeToPrompt(messages: any[]):
|
|
51
|
-
|
|
59
|
+
serializeToPrompt(messages: any[]): {
|
|
60
|
+
prompt: string;
|
|
61
|
+
participants: Set<string>;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Generate stop sequences from participant names.
|
|
65
|
+
* Prevents the model from generating turns for other participants.
|
|
66
|
+
*/
|
|
67
|
+
private generateStopSequences;
|
|
52
68
|
private extractTextContent;
|
|
53
69
|
private getHeaders;
|
|
54
70
|
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.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,MAA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,OAAO,CAAC,YAAY;YAgCN,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,8 @@ export class OpenAICompletionsAdapter {
|
|
|
19
19
|
apiKey;
|
|
20
20
|
defaultMaxTokens;
|
|
21
21
|
extraHeaders;
|
|
22
|
-
|
|
22
|
+
assistantName;
|
|
23
|
+
extraStopSequences;
|
|
23
24
|
warnOnImageStrip;
|
|
24
25
|
constructor(config) {
|
|
25
26
|
if (!config.baseURL) {
|
|
@@ -30,7 +31,8 @@ export class OpenAICompletionsAdapter {
|
|
|
30
31
|
this.apiKey = config.apiKey ?? '';
|
|
31
32
|
this.defaultMaxTokens = config.defaultMaxTokens ?? 4096;
|
|
32
33
|
this.extraHeaders = config.extraHeaders ?? {};
|
|
33
|
-
this.
|
|
34
|
+
this.assistantName = config.assistantName ?? 'Assistant';
|
|
35
|
+
this.extraStopSequences = config.extraStopSequences ?? [];
|
|
34
36
|
this.warnOnImageStrip = config.warnOnImageStrip ?? true;
|
|
35
37
|
}
|
|
36
38
|
supportsModel(_modelId) {
|
|
@@ -103,36 +105,52 @@ export class OpenAICompletionsAdapter {
|
|
|
103
105
|
// Prompt Serialization
|
|
104
106
|
// ============================================================================
|
|
105
107
|
/**
|
|
106
|
-
* Serialize messages to
|
|
108
|
+
* Serialize messages to "Participant: content" format for base models.
|
|
109
|
+
* Uses actual participant names from messages.
|
|
107
110
|
* Images are stripped from content.
|
|
108
111
|
*/
|
|
109
112
|
serializeToPrompt(messages) {
|
|
110
113
|
const parts = [];
|
|
114
|
+
const participants = new Set();
|
|
111
115
|
let hasStrippedImages = false;
|
|
112
116
|
for (const msg of messages) {
|
|
113
|
-
|
|
114
|
-
const
|
|
117
|
+
// Get participant name (supports both 'participant' and 'role' fields)
|
|
118
|
+
const participant = msg.participant || msg.role || 'Unknown';
|
|
119
|
+
participants.add(participant);
|
|
115
120
|
// Extract text content, strip images
|
|
116
121
|
const textContent = this.extractTextContent(msg.content);
|
|
117
122
|
if (textContent.hadImages) {
|
|
118
123
|
hasStrippedImages = true;
|
|
119
124
|
}
|
|
120
125
|
if (textContent.text) {
|
|
121
|
-
parts.push(`${
|
|
126
|
+
parts.push(`${participant}: ${textContent.text}`);
|
|
122
127
|
}
|
|
123
128
|
}
|
|
124
129
|
if (hasStrippedImages && this.warnOnImageStrip) {
|
|
125
130
|
console.warn('[OpenAICompletionsAdapter] Images were stripped from context (not supported in completions mode)');
|
|
126
131
|
}
|
|
127
|
-
// Add final
|
|
128
|
-
parts.push(
|
|
129
|
-
return
|
|
132
|
+
// Add final assistant prefix to prompt completion
|
|
133
|
+
parts.push(`${this.assistantName}:`);
|
|
134
|
+
return {
|
|
135
|
+
prompt: parts.join('\n\n'),
|
|
136
|
+
participants,
|
|
137
|
+
};
|
|
130
138
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Generate stop sequences from participant names.
|
|
141
|
+
* Prevents the model from generating turns for other participants.
|
|
142
|
+
*/
|
|
143
|
+
generateStopSequences(participants) {
|
|
144
|
+
const stops = [];
|
|
145
|
+
for (const participant of participants) {
|
|
146
|
+
// Skip the assistant - we don't want to stop on its own name
|
|
147
|
+
if (participant === this.assistantName)
|
|
148
|
+
continue;
|
|
149
|
+
// Add both "\n\nName:" and "\nName:" variants
|
|
150
|
+
stops.push(`\n\n${participant}:`);
|
|
151
|
+
stops.push(`\n${participant}:`);
|
|
134
152
|
}
|
|
135
|
-
return
|
|
153
|
+
return stops;
|
|
136
154
|
}
|
|
137
155
|
extractTextContent(content) {
|
|
138
156
|
if (typeof content === 'string') {
|
|
@@ -168,7 +186,7 @@ export class OpenAICompletionsAdapter {
|
|
|
168
186
|
return headers;
|
|
169
187
|
}
|
|
170
188
|
buildRequest(request) {
|
|
171
|
-
const prompt = this.serializeToPrompt(request.messages);
|
|
189
|
+
const { prompt, participants } = this.serializeToPrompt(request.messages);
|
|
172
190
|
const params = {
|
|
173
191
|
model: request.model,
|
|
174
192
|
prompt,
|
|
@@ -177,9 +195,10 @@ export class OpenAICompletionsAdapter {
|
|
|
177
195
|
if (request.temperature !== undefined) {
|
|
178
196
|
params.temperature = request.temperature;
|
|
179
197
|
}
|
|
180
|
-
//
|
|
198
|
+
// Generate stop sequences from participant names + any extras
|
|
181
199
|
const stopSequences = [
|
|
182
|
-
...this.
|
|
200
|
+
...this.generateStopSequences(participants),
|
|
201
|
+
...this.extraStopSequences,
|
|
183
202
|
...(request.stopSequences || []),
|
|
184
203
|
];
|
|
185
204
|
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,YAAY,GACb,MAAM,mBAAmB,CAAC;
|
|
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,YAAY,GACb,MAAM,mBAAmB,CAAC;AAsE3B,+EAA+E;AAC/E,6BAA6B;AAC7B,+EAA+E;AAE/E,MAAM,OAAO,wBAAwB;IAC1B,IAAI,CAAS;IACd,OAAO,CAAS;IAChB,MAAM,CAAS;IACf,gBAAgB,CAAS;IACzB,YAAY,CAAyB;IACrC,aAAa,CAAS;IACtB,kBAAkB,CAAW;IAC7B,gBAAgB,CAAU;IAElC,YAAY,MAAsC;QAChD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM,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AAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;gBAClF,OAAO,WAAW,CAAC,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;YAC5D,CAAC;YAED,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAChC,OAAO,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,18 @@ 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
|
+
* Additional stop sequences beyond auto-generated participant-based ones.
|
|
88
|
+
* By default, stop sequences are generated from participant names in the
|
|
89
|
+
* conversation (e.g., "\n\nAlice:", "\nBob:").
|
|
90
|
+
*/
|
|
91
|
+
extraStopSequences?: string[];
|
|
84
92
|
|
|
85
93
|
/**
|
|
86
94
|
* Whether to warn when images are stripped from context (default: true)
|
|
@@ -98,7 +106,8 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
98
106
|
private apiKey: string;
|
|
99
107
|
private defaultMaxTokens: number;
|
|
100
108
|
private extraHeaders: Record<string, string>;
|
|
101
|
-
private
|
|
109
|
+
private assistantName: string;
|
|
110
|
+
private extraStopSequences: string[];
|
|
102
111
|
private warnOnImageStrip: boolean;
|
|
103
112
|
|
|
104
113
|
constructor(config: OpenAICompletionsAdapterConfig) {
|
|
@@ -111,7 +120,8 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
111
120
|
this.apiKey = config.apiKey ?? '';
|
|
112
121
|
this.defaultMaxTokens = config.defaultMaxTokens ?? 4096;
|
|
113
122
|
this.extraHeaders = config.extraHeaders ?? {};
|
|
114
|
-
this.
|
|
123
|
+
this.assistantName = config.assistantName ?? 'Assistant';
|
|
124
|
+
this.extraStopSequences = config.extraStopSequences ?? [];
|
|
115
125
|
this.warnOnImageStrip = config.warnOnImageStrip ?? true;
|
|
116
126
|
}
|
|
117
127
|
|
|
@@ -204,16 +214,19 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
204
214
|
// ============================================================================
|
|
205
215
|
|
|
206
216
|
/**
|
|
207
|
-
* Serialize messages to
|
|
217
|
+
* Serialize messages to "Participant: content" format for base models.
|
|
218
|
+
* Uses actual participant names from messages.
|
|
208
219
|
* Images are stripped from content.
|
|
209
220
|
*/
|
|
210
|
-
serializeToPrompt(messages: any[]): string {
|
|
221
|
+
serializeToPrompt(messages: any[]): { prompt: string; participants: Set<string> } {
|
|
211
222
|
const parts: string[] = [];
|
|
223
|
+
const participants = new Set<string>();
|
|
212
224
|
let hasStrippedImages = false;
|
|
213
225
|
|
|
214
226
|
for (const msg of messages) {
|
|
215
|
-
|
|
216
|
-
const
|
|
227
|
+
// Get participant name (supports both 'participant' and 'role' fields)
|
|
228
|
+
const participant = msg.participant || msg.role || 'Unknown';
|
|
229
|
+
participants.add(participant);
|
|
217
230
|
|
|
218
231
|
// Extract text content, strip images
|
|
219
232
|
const textContent = this.extractTextContent(msg.content);
|
|
@@ -222,7 +235,7 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
222
235
|
}
|
|
223
236
|
|
|
224
237
|
if (textContent.text) {
|
|
225
|
-
parts.push(`${
|
|
238
|
+
parts.push(`${participant}: ${textContent.text}`);
|
|
226
239
|
}
|
|
227
240
|
}
|
|
228
241
|
|
|
@@ -230,17 +243,32 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
230
243
|
console.warn('[OpenAICompletionsAdapter] Images were stripped from context (not supported in completions mode)');
|
|
231
244
|
}
|
|
232
245
|
|
|
233
|
-
// Add final
|
|
234
|
-
parts.push(
|
|
246
|
+
// Add final assistant prefix to prompt completion
|
|
247
|
+
parts.push(`${this.assistantName}:`);
|
|
235
248
|
|
|
236
|
-
return
|
|
249
|
+
return {
|
|
250
|
+
prompt: parts.join('\n\n'),
|
|
251
|
+
participants,
|
|
252
|
+
};
|
|
237
253
|
}
|
|
238
254
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
255
|
+
/**
|
|
256
|
+
* Generate stop sequences from participant names.
|
|
257
|
+
* Prevents the model from generating turns for other participants.
|
|
258
|
+
*/
|
|
259
|
+
private generateStopSequences(participants: Set<string>): string[] {
|
|
260
|
+
const stops: string[] = [];
|
|
261
|
+
|
|
262
|
+
for (const participant of participants) {
|
|
263
|
+
// Skip the assistant - we don't want to stop on its own name
|
|
264
|
+
if (participant === this.assistantName) continue;
|
|
265
|
+
|
|
266
|
+
// Add both "\n\nName:" and "\nName:" variants
|
|
267
|
+
stops.push(`\n\n${participant}:`);
|
|
268
|
+
stops.push(`\n${participant}:`);
|
|
242
269
|
}
|
|
243
|
-
|
|
270
|
+
|
|
271
|
+
return stops;
|
|
244
272
|
}
|
|
245
273
|
|
|
246
274
|
private extractTextContent(content: any): { text: string; hadImages: boolean } {
|
|
@@ -285,7 +313,7 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
285
313
|
}
|
|
286
314
|
|
|
287
315
|
private buildRequest(request: ProviderRequest): CompletionsRequest {
|
|
288
|
-
const prompt = this.serializeToPrompt(request.messages as any[]);
|
|
316
|
+
const { prompt, participants } = this.serializeToPrompt(request.messages as any[]);
|
|
289
317
|
|
|
290
318
|
const params: CompletionsRequest = {
|
|
291
319
|
model: request.model,
|
|
@@ -297,9 +325,10 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
|
|
|
297
325
|
params.temperature = request.temperature;
|
|
298
326
|
}
|
|
299
327
|
|
|
300
|
-
//
|
|
328
|
+
// Generate stop sequences from participant names + any extras
|
|
301
329
|
const stopSequences = [
|
|
302
|
-
...this.
|
|
330
|
+
...this.generateStopSequences(participants),
|
|
331
|
+
...this.extraStopSequences,
|
|
303
332
|
...(request.stopSequences || []),
|
|
304
333
|
];
|
|
305
334
|
if (stopSequences.length > 0) {
|