@modular-prompt/driver 0.14.0 → 0.16.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.
- package/README.md +90 -5
- package/dist/driver-registry/ai-service.d.ts +23 -1
- package/dist/driver-registry/ai-service.d.ts.map +1 -1
- package/dist/driver-registry/ai-service.js +44 -10
- package/dist/driver-registry/ai-service.js.map +1 -1
- package/dist/driver-registry/config-based-factory.d.ts.map +1 -1
- package/dist/driver-registry/config-based-factory.js +20 -3
- package/dist/driver-registry/config-based-factory.js.map +1 -1
- package/dist/driver-registry/factory-helper.d.ts +2 -0
- package/dist/driver-registry/factory-helper.d.ts.map +1 -1
- package/dist/driver-registry/factory-helper.js +22 -3
- package/dist/driver-registry/factory-helper.js.map +1 -1
- package/dist/driver-registry/index.d.ts +2 -2
- package/dist/driver-registry/index.d.ts.map +1 -1
- package/dist/driver-registry/index.js +1 -1
- package/dist/driver-registry/index.js.map +1 -1
- package/dist/driver-registry/registry.d.ts.map +1 -1
- package/dist/driver-registry/registry.js +3 -1
- package/dist/driver-registry/registry.js.map +1 -1
- package/dist/driver-registry/types.d.ts +11 -2
- package/dist/driver-registry/types.d.ts.map +1 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/local-inference/adapters.d.ts +66 -0
- package/dist/local-inference/adapters.d.ts.map +1 -0
- package/dist/local-inference/adapters.js +2 -0
- package/dist/local-inference/adapters.js.map +1 -0
- package/dist/local-inference/driver.d.ts +51 -0
- package/dist/local-inference/driver.d.ts.map +1 -0
- package/dist/local-inference/driver.js +309 -0
- package/dist/local-inference/driver.js.map +1 -0
- package/dist/local-inference/index.d.ts +22 -0
- package/dist/local-inference/index.d.ts.map +1 -0
- package/dist/local-inference/index.js +17 -0
- package/dist/local-inference/index.js.map +1 -0
- package/dist/local-inference/process-client.d.ts +50 -0
- package/dist/local-inference/process-client.d.ts.map +1 -0
- package/dist/local-inference/process-client.js +92 -0
- package/dist/local-inference/process-client.js.map +1 -0
- package/dist/local-inference/process-communication.d.ts +41 -0
- package/dist/local-inference/process-communication.d.ts.map +1 -0
- package/dist/{mlx-ml/process → local-inference}/process-communication.js +25 -61
- package/dist/local-inference/process-communication.js.map +1 -0
- package/dist/local-inference/process-port.d.ts +12 -0
- package/dist/local-inference/process-port.d.ts.map +1 -0
- package/dist/local-inference/process-port.js +2 -0
- package/dist/local-inference/process-port.js.map +1 -0
- package/dist/local-inference/prompt-utils.d.ts +6 -0
- package/dist/local-inference/prompt-utils.d.ts.map +1 -0
- package/dist/local-inference/prompt-utils.js +17 -0
- package/dist/local-inference/prompt-utils.js.map +1 -0
- package/dist/local-inference/protocol.d.ts +192 -0
- package/dist/local-inference/protocol.d.ts.map +1 -0
- package/dist/local-inference/protocol.js +2 -0
- package/dist/local-inference/protocol.js.map +1 -0
- package/dist/local-inference/queue-types.d.ts +54 -0
- package/dist/local-inference/queue-types.d.ts.map +1 -0
- package/dist/local-inference/queue-types.js +2 -0
- package/dist/local-inference/queue-types.js.map +1 -0
- package/dist/local-inference/request-queue.d.ts +36 -0
- package/dist/local-inference/request-queue.d.ts.map +1 -0
- package/dist/{mlx-ml/process/queue.js → local-inference/request-queue.js} +83 -56
- package/dist/local-inference/request-queue.js.map +1 -0
- package/dist/local-inference/stream-utils.d.ts +19 -0
- package/dist/local-inference/stream-utils.d.ts.map +1 -0
- package/dist/local-inference/stream-utils.js +76 -0
- package/dist/local-inference/stream-utils.js.map +1 -0
- package/dist/mlx-ml/mlx-cache-controller.d.ts.map +1 -1
- package/dist/mlx-ml/mlx-cache-controller.js +2 -1
- package/dist/mlx-ml/mlx-cache-controller.js.map +1 -1
- package/dist/mlx-ml/mlx-cache-support.d.ts +23 -0
- package/dist/mlx-ml/mlx-cache-support.d.ts.map +1 -0
- package/dist/mlx-ml/mlx-cache-support.js +45 -0
- package/dist/mlx-ml/mlx-cache-support.js.map +1 -0
- package/dist/mlx-ml/mlx-driver.d.ts +20 -59
- package/dist/mlx-ml/mlx-driver.d.ts.map +1 -1
- package/dist/mlx-ml/mlx-driver.js +86 -460
- package/dist/mlx-ml/mlx-driver.js.map +1 -1
- package/dist/mlx-ml/mlx-local-inference-adapters.d.ts +3 -0
- package/dist/mlx-ml/mlx-local-inference-adapters.d.ts.map +1 -0
- package/dist/mlx-ml/mlx-local-inference-adapters.js +19 -0
- package/dist/mlx-ml/mlx-local-inference-adapters.js.map +1 -0
- package/dist/mlx-ml/mlx-options.d.ts +19 -0
- package/dist/mlx-ml/mlx-options.d.ts.map +1 -0
- package/dist/mlx-ml/mlx-options.js +30 -0
- package/dist/mlx-ml/mlx-options.js.map +1 -0
- package/dist/mlx-ml/process/index.d.ts +10 -8
- package/dist/mlx-ml/process/index.d.ts.map +1 -1
- package/dist/mlx-ml/process/index.js +73 -54
- package/dist/mlx-ml/process/index.js.map +1 -1
- package/dist/mlx-ml/process/model-specific.d.ts +2 -1
- package/dist/mlx-ml/process/model-specific.d.ts.map +1 -1
- package/dist/mlx-ml/process/model-specific.js.map +1 -1
- package/dist/mlx-ml/process/prompt-builder.d.ts +11 -0
- package/dist/mlx-ml/process/prompt-builder.d.ts.map +1 -0
- package/dist/mlx-ml/process/prompt-builder.js +51 -0
- package/dist/mlx-ml/process/prompt-builder.js.map +1 -0
- package/dist/mlx-ml/process/types.d.ts +15 -183
- package/dist/mlx-ml/process/types.d.ts.map +1 -1
- package/dist/mlx-ml/types.d.ts +2 -45
- package/dist/mlx-ml/types.d.ts.map +1 -1
- package/dist/models-config/index.d.ts +8 -0
- package/dist/models-config/index.d.ts.map +1 -0
- package/dist/models-config/index.js +7 -0
- package/dist/models-config/index.js.map +1 -0
- package/dist/models-config/loader.d.ts +20 -0
- package/dist/models-config/loader.d.ts.map +1 -0
- package/dist/models-config/loader.js +85 -0
- package/dist/models-config/loader.js.map +1 -0
- package/dist/models-config/paths.d.ts +15 -0
- package/dist/models-config/paths.d.ts.map +1 -0
- package/dist/models-config/paths.js +26 -0
- package/dist/models-config/paths.js.map +1 -0
- package/dist/models-config/resolve.d.ts +57 -0
- package/dist/models-config/resolve.d.ts.map +1 -0
- package/dist/models-config/resolve.js +208 -0
- package/dist/models-config/resolve.js.map +1 -0
- package/dist/models-config/types.d.ts +62 -0
- package/dist/models-config/types.d.ts.map +1 -0
- package/dist/models-config/types.js +5 -0
- package/dist/models-config/types.js.map +1 -0
- package/dist/pytorch/process/index.d.ts +35 -0
- package/dist/pytorch/process/index.d.ts.map +1 -0
- package/dist/pytorch/process/index.js +69 -0
- package/dist/pytorch/process/index.js.map +1 -0
- package/dist/pytorch/pytorch-driver.d.ts +35 -0
- package/dist/pytorch/pytorch-driver.d.ts.map +1 -0
- package/dist/pytorch/pytorch-driver.js +48 -0
- package/dist/pytorch/pytorch-driver.js.map +1 -0
- package/dist/pytorch/pytorch-local-inference-adapters.d.ts +3 -0
- package/dist/pytorch/pytorch-local-inference-adapters.d.ts.map +1 -0
- package/dist/pytorch/pytorch-local-inference-adapters.js +19 -0
- package/dist/pytorch/pytorch-local-inference-adapters.js.map +1 -0
- package/dist/pytorch/pytorch-options.d.ts +8 -0
- package/dist/pytorch/pytorch-options.d.ts.map +1 -0
- package/dist/pytorch/pytorch-options.js +21 -0
- package/dist/pytorch/pytorch-options.js.map +1 -0
- package/dist/query-logger.js +1 -1
- package/dist/query-logger.js.map +1 -1
- package/dist/runtime/check.d.ts +10 -0
- package/dist/runtime/check.d.ts.map +1 -0
- package/dist/runtime/check.js +25 -0
- package/dist/runtime/check.js.map +1 -0
- package/dist/runtime/index.d.ts +5 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +5 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/manifest-core.d.mts +27 -0
- package/dist/runtime/manifest-core.d.mts.map +1 -0
- package/dist/runtime/manifest-core.mjs +68 -0
- package/dist/runtime/manifest-core.mjs.map +1 -0
- package/dist/runtime/manifest.d.ts +18 -0
- package/dist/runtime/manifest.d.ts.map +1 -0
- package/dist/runtime/manifest.js +9 -0
- package/dist/runtime/manifest.js.map +1 -0
- package/dist/runtime/paths-core.d.mts +17 -0
- package/dist/runtime/paths-core.d.mts.map +1 -0
- package/dist/runtime/paths-core.mjs +67 -0
- package/dist/runtime/paths-core.mjs.map +1 -0
- package/dist/runtime/paths.d.ts +12 -0
- package/dist/runtime/paths.d.ts.map +1 -0
- package/dist/runtime/paths.js +17 -0
- package/dist/runtime/paths.js.map +1 -0
- package/dist/runtime/setup-commands-core.d.mts +13 -0
- package/dist/runtime/setup-commands-core.d.mts.map +1 -0
- package/dist/runtime/setup-commands-core.mjs +14 -0
- package/dist/runtime/setup-commands-core.mjs.map +1 -0
- package/dist/runtime/setup-commands.d.ts +2 -0
- package/dist/runtime/setup-commands.d.ts.map +1 -0
- package/dist/runtime/setup-commands.js +2 -0
- package/dist/runtime/setup-commands.js.map +1 -0
- package/dist/types.d.ts +11 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +13 -7
- package/scripts/download-model.js +26 -9
- package/scripts/runtime-cli.js +320 -0
- package/src/mlx-ml/python/__main__.py +43 -4
- package/src/mlx-ml/python/backends/cache_archive.py +41 -0
- package/src/mlx-ml/python/backends/mlx_lm.py +17 -1
- package/src/mlx-ml/python/handlers/__init__.py +2 -1
- package/src/mlx-ml/python/handlers/completion.py +3 -27
- package/src/mlx-ml/python/handlers/{chat.py → generate.py} +33 -106
- package/src/mlx-ml/python/handlers/render.py +40 -0
- package/src/mlx-ml/python/pyproject.toml +11 -4
- package/src/mlx-ml/python/server.py +28 -8
- package/src/mlx-ml/python/utils/template_render.py +80 -0
- package/src/mlx-ml/python/utils/token_utils.py +2 -2
- package/src/mlx-ml/python/uv.lock +544 -689
- package/src/pytorch/python/__main__.py +19 -0
- package/src/pytorch/python/backends/__init__.py +3 -0
- package/src/pytorch/python/backends/base.py +84 -0
- package/src/pytorch/python/backends/transformers_lm.py +127 -0
- package/src/pytorch/python/handlers/__init__.py +6 -0
- package/src/pytorch/python/handlers/cancel.py +53 -0
- package/src/pytorch/python/handlers/capabilities.py +6 -0
- package/src/pytorch/python/handlers/completion.py +15 -0
- package/src/pytorch/python/handlers/format_test.py +70 -0
- package/src/pytorch/python/handlers/generate.py +68 -0
- package/src/pytorch/python/handlers/render.py +40 -0
- package/src/pytorch/python/handlers/tokenize.py +63 -0
- package/src/pytorch/python/pyproject.toml +36 -0
- package/src/pytorch/python/server.py +140 -0
- package/src/pytorch/python/utils/__init__.py +0 -0
- package/src/pytorch/python/utils/chat_template_constraints.py +164 -0
- package/src/pytorch/python/utils/prompt_builder.py +54 -0
- package/src/pytorch/python/utils/template_render.py +80 -0
- package/src/pytorch/python/utils/token_utils.py +376 -0
- package/src/pytorch/python/uv.lock +694 -0
- package/dist/mlx-ml/process/process-communication.d.ts +0 -45
- package/dist/mlx-ml/process/process-communication.d.ts.map +0 -1
- package/dist/mlx-ml/process/process-communication.js.map +0 -1
- package/dist/mlx-ml/process/queue.d.ts +0 -35
- package/dist/mlx-ml/process/queue.d.ts.map +0 -1
- package/dist/mlx-ml/process/queue.js.map +0 -1
- package/scripts/setup-mlx.js +0 -53
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local Inference Protocol プロセスクライアント
|
|
3
|
+
*
|
|
4
|
+
* stdio JSON-RPC 通信とリクエストキューを統合する。
|
|
5
|
+
*/
|
|
6
|
+
import { Readable } from 'stream';
|
|
7
|
+
import type { RuntimeProfile } from '../runtime/index.js';
|
|
8
|
+
import type { InferenceCapabilities, InferenceFormatTestResult, InferenceRenderResult, InferenceTokenizeResult, InferenceCachePrefillResult, InferenceMessage, InferenceToolDefinition, InferenceSamplingOptions } from './protocol.js';
|
|
9
|
+
import { type SamplingOptionsMapper } from './request-queue.js';
|
|
10
|
+
export interface InferenceProcessClientConfig {
|
|
11
|
+
modelName: string;
|
|
12
|
+
/** uv --project に渡す Python プロジェクトディレクトリ */
|
|
13
|
+
pythonProjectDir: string;
|
|
14
|
+
/** UV_PROJECT_ENVIRONMENT に設定する venv パス */
|
|
15
|
+
venvPath: string;
|
|
16
|
+
/** 指定時は起動前に runtime venv の存在を確認する */
|
|
17
|
+
runtimeProfile?: RuntimeProfile;
|
|
18
|
+
extraSpawnArgs?: string[];
|
|
19
|
+
extraEnv?: Record<string, string>;
|
|
20
|
+
loggerPrefix?: string;
|
|
21
|
+
mapSamplingOptions?: SamplingOptionsMapper;
|
|
22
|
+
processExitErrorMessage?: (code: number | null, signal: string | null) => string;
|
|
23
|
+
}
|
|
24
|
+
export declare class InferenceProcessClient {
|
|
25
|
+
readonly modelName: string;
|
|
26
|
+
private requestQueue;
|
|
27
|
+
private processComm;
|
|
28
|
+
constructor(config: InferenceProcessClientConfig);
|
|
29
|
+
ensureInitialized(): Promise<void>;
|
|
30
|
+
getCapabilities(): Promise<InferenceCapabilities>;
|
|
31
|
+
formatTest(messages: InferenceMessage[], options?: {
|
|
32
|
+
primer?: string;
|
|
33
|
+
}): Promise<InferenceFormatTestResult>;
|
|
34
|
+
render(messages: InferenceMessage[], options?: InferenceSamplingOptions & {
|
|
35
|
+
primer?: string;
|
|
36
|
+
}, tools?: InferenceToolDefinition[], reasoningEffort?: 'low' | 'medium' | 'high'): Promise<InferenceRenderResult>;
|
|
37
|
+
tokenize(messages: InferenceMessage[], tools?: InferenceToolDefinition[], reasoningEffort?: 'low' | 'medium' | 'high'): Promise<InferenceTokenizeResult>;
|
|
38
|
+
cachePrefill(cachePath: string, messages: InferenceMessage[], baseCachePath?: string, trimToTokens?: number, prefixOffsets?: number[], prefixHashes?: string[], tools?: InferenceToolDefinition[], reasoningEffort?: 'low' | 'medium' | 'high'): Promise<InferenceCachePrefillResult>;
|
|
39
|
+
completion(prompt: string, options?: unknown, images?: string[], maxImageSize?: number): Promise<Readable>;
|
|
40
|
+
generate(prompt: string | number[], options?: unknown, images?: string[], maxImageSize?: number, cachePath?: string, cacheTrimTokens?: number, primer?: string): Promise<Readable>;
|
|
41
|
+
exit(): Promise<void>;
|
|
42
|
+
cancelActiveRequest(): void;
|
|
43
|
+
getStatus(): {
|
|
44
|
+
modelName: string;
|
|
45
|
+
queueLength: number;
|
|
46
|
+
isStreamingActive: boolean;
|
|
47
|
+
isJsonBuffering: boolean;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=process-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-client.d.ts","sourceRoot":"","sources":["../../src/local-inference/process-client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,KAAK,EACV,qBAAqB,EACrB,yBAAyB,EACzB,qBAAqB,EACrB,uBAAuB,EACvB,2BAA2B,EAC3B,gBAAgB,EAChB,uBAAuB,EACvB,wBAAwB,EACzB,MAAM,eAAe,CAAC;AAKvB,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,oBAAoB,CAAC;AAE5B,MAAM,WAAW,4BAA4B;IAC3C,SAAS,EAAE,MAAM,CAAC;IAClB,2CAA2C;IAC3C,gBAAgB,EAAE,MAAM,CAAC;IACzB,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,qCAAqC;IACrC,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kBAAkB,CAAC,EAAE,qBAAqB,CAAC;IAC3C,uBAAuB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,KAAK,MAAM,CAAC;CAClF;AAED,qBAA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,EAAE,MAAM,EAAE,EACjB,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,QAAQ,CAAC;IAId,QAAQ,CACZ,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,EACzB,OAAO,CAAC,EAAE,OAAO,EACjB,MAAM,CAAC,EAAE,MAAM,EAAE,EACjB,YAAY,CAAC,EAAE,MAAM,EACrB,SAAS,CAAC,EAAE,MAAM,EAClB,eAAe,CAAC,EAAE,MAAM,EACxB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,QAAQ,CAAC;IAYd,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,mBAAmB,IAAI,IAAI;IAI3B,SAAS;;;;;;CAQV"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local Inference Protocol プロセスクライアント
|
|
3
|
+
*
|
|
4
|
+
* stdio JSON-RPC 通信とリクエストキューを統合する。
|
|
5
|
+
*/
|
|
6
|
+
import { Logger } from '@modular-prompt/utils';
|
|
7
|
+
import { assertRuntimeReady } from '../runtime/index.js';
|
|
8
|
+
import { ProcessCommunication, } from './process-communication.js';
|
|
9
|
+
import { InferenceRequestQueue, } from './request-queue.js';
|
|
10
|
+
export class InferenceProcessClient {
|
|
11
|
+
modelName;
|
|
12
|
+
requestQueue;
|
|
13
|
+
processComm;
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.modelName = config.modelName;
|
|
16
|
+
if (config.runtimeProfile) {
|
|
17
|
+
assertRuntimeReady(config.runtimeProfile);
|
|
18
|
+
}
|
|
19
|
+
const logger = new Logger({
|
|
20
|
+
prefix: config.loggerPrefix ?? 'LIP',
|
|
21
|
+
context: 'process',
|
|
22
|
+
});
|
|
23
|
+
// processCallbacks は this.requestQueue 代入前に定義するが、
|
|
24
|
+
// コールバックは子プロセスの stdout/exit など非同期イベント時のみ呼ばれるため安全。
|
|
25
|
+
const processCallbacks = {
|
|
26
|
+
onJsonResponse: (jsonData) => this.requestQueue.handleJsonResponse(jsonData),
|
|
27
|
+
onRequestCompleted: () => this.requestQueue.onRequestCompleted(),
|
|
28
|
+
onProcessExit: (code, signal) => {
|
|
29
|
+
if (code !== 0) {
|
|
30
|
+
const message = config.processExitErrorMessage?.(code, signal) ??
|
|
31
|
+
`Inference process exited unexpectedly (code=${code}, signal=${signal})`;
|
|
32
|
+
logger.error(message);
|
|
33
|
+
this.requestQueue.rejectAll(new Error(message));
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const queueCallbacks = {
|
|
38
|
+
sendToProcess: (data) => this.processComm.sendToProcess(data),
|
|
39
|
+
createNewStream: () => this.processComm.createNewStream(),
|
|
40
|
+
cancelActiveStream: () => this.processComm.cancelActiveStream(),
|
|
41
|
+
};
|
|
42
|
+
this.processComm = new ProcessCommunication({
|
|
43
|
+
pythonProjectDir: config.pythonProjectDir,
|
|
44
|
+
venvPath: config.venvPath,
|
|
45
|
+
modelName: config.modelName,
|
|
46
|
+
extraArgs: config.extraSpawnArgs,
|
|
47
|
+
extraEnv: config.extraEnv,
|
|
48
|
+
loggerPrefix: config.loggerPrefix,
|
|
49
|
+
processExitErrorMessage: config.processExitErrorMessage,
|
|
50
|
+
}, processCallbacks);
|
|
51
|
+
this.requestQueue = new InferenceRequestQueue(queueCallbacks, config.mapSamplingOptions);
|
|
52
|
+
}
|
|
53
|
+
async ensureInitialized() {
|
|
54
|
+
// 互換性のため維持
|
|
55
|
+
}
|
|
56
|
+
async getCapabilities() {
|
|
57
|
+
return this.requestQueue.addCapabilitiesRequest();
|
|
58
|
+
}
|
|
59
|
+
async formatTest(messages, options) {
|
|
60
|
+
return this.requestQueue.addFormatTestRequest(messages, options);
|
|
61
|
+
}
|
|
62
|
+
async render(messages, options, tools, reasoningEffort) {
|
|
63
|
+
return this.requestQueue.addRenderRequest(messages, options, tools, reasoningEffort);
|
|
64
|
+
}
|
|
65
|
+
async tokenize(messages, tools, reasoningEffort) {
|
|
66
|
+
return this.requestQueue.addTokenizeRequest(messages, tools, reasoningEffort);
|
|
67
|
+
}
|
|
68
|
+
async cachePrefill(cachePath, messages, baseCachePath, trimToTokens, prefixOffsets, prefixHashes, tools, reasoningEffort) {
|
|
69
|
+
return this.requestQueue.addCachePrefillRequest(cachePath, messages, baseCachePath, trimToTokens, prefixOffsets, prefixHashes, tools, reasoningEffort);
|
|
70
|
+
}
|
|
71
|
+
async completion(prompt, options, images, maxImageSize) {
|
|
72
|
+
return this.requestQueue.addCompletionRequest(prompt, options, images, maxImageSize);
|
|
73
|
+
}
|
|
74
|
+
async generate(prompt, options, images, maxImageSize, cachePath, cacheTrimTokens, primer) {
|
|
75
|
+
return this.requestQueue.addGenerateRequest(prompt, options, images, maxImageSize, cachePath, cacheTrimTokens, primer);
|
|
76
|
+
}
|
|
77
|
+
async exit() {
|
|
78
|
+
await this.processComm.exit();
|
|
79
|
+
}
|
|
80
|
+
cancelActiveRequest() {
|
|
81
|
+
this.requestQueue.cancelActiveRequest();
|
|
82
|
+
}
|
|
83
|
+
getStatus() {
|
|
84
|
+
return {
|
|
85
|
+
modelName: this.modelName,
|
|
86
|
+
queueLength: this.requestQueue.length,
|
|
87
|
+
isStreamingActive: this.processComm.isStreamingActive(),
|
|
88
|
+
isJsonBuffering: this.processComm.isJsonBuffering(),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=process-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-client.js","sourceRoot":"","sources":["../../src/local-inference/process-client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAWzD,OAAO,EACL,oBAAoB,GAErB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,qBAAqB,GAGtB,MAAM,oBAAoB,CAAC;AAiB5B,MAAM,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5
|
+
export interface ProcessCommunicationCallbacks {
|
|
6
|
+
onJsonResponse: (jsonData: string) => void;
|
|
7
|
+
onRequestCompleted: () => void;
|
|
8
|
+
onProcessExit: (code: number | null, signal: string | null) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface ProcessCommunicationConfig {
|
|
11
|
+
/** uv --project に渡す Python プロジェクトディレクトリ */
|
|
12
|
+
pythonProjectDir: string;
|
|
13
|
+
/** UV_PROJECT_ENVIRONMENT に設定する venv パス */
|
|
14
|
+
venvPath: string;
|
|
15
|
+
modelName: string;
|
|
16
|
+
/** `__main__.py` と modelName の後に付与する追加引数 */
|
|
17
|
+
extraArgs?: string[];
|
|
18
|
+
/** 子プロセス spawn 時にマージする追加環境変数 */
|
|
19
|
+
extraEnv?: Record<string, string>;
|
|
20
|
+
loggerPrefix?: string;
|
|
21
|
+
loggerContext?: string;
|
|
22
|
+
processExitErrorMessage?: (code: number | null, signal: string | null) => string;
|
|
23
|
+
}
|
|
24
|
+
export declare class ProcessCommunication {
|
|
25
|
+
private process;
|
|
26
|
+
private decoder;
|
|
27
|
+
private currentStream;
|
|
28
|
+
private jsonBuffer;
|
|
29
|
+
private draining;
|
|
30
|
+
private callbacks;
|
|
31
|
+
private readonly exitErrorMessage;
|
|
32
|
+
constructor(config: ProcessCommunicationConfig, callbacks: ProcessCommunicationCallbacks);
|
|
33
|
+
private processData;
|
|
34
|
+
createNewStream(): Readable;
|
|
35
|
+
cancelActiveStream(): void;
|
|
36
|
+
sendToProcess(data: string): void;
|
|
37
|
+
isStreamingActive(): boolean;
|
|
38
|
+
isJsonBuffering(): boolean;
|
|
39
|
+
exit(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=process-communication.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-communication.d.ts","sourceRoot":"","sources":["../../src/local-inference/process-communication.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,QAAQ,EAAE,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e,IAAI,OAAO;IAIpB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAe5B"}
|
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Pythonプロセスとの通信、データの送受信、ストリーミング処理を管理
|
|
2
|
+
* Thin Python 推論プロセスとの stdio 通信
|
|
5
3
|
*/
|
|
6
4
|
import { spawn } from 'child_process';
|
|
7
5
|
import { Readable } from 'stream';
|
|
8
6
|
import { StringDecoder } from 'string_decoder';
|
|
9
|
-
import path from "path";
|
|
10
|
-
import { fileURLToPath } from "url";
|
|
11
7
|
import { Logger } from '@modular-prompt/utils';
|
|
12
|
-
const logger = new Logger({ prefix: 'MLX', context: 'process' });
|
|
13
|
-
// Get the mlx-ml/python directory
|
|
14
|
-
// From dist/mlx-ml/process/ -> go up 3 levels to package root, then to src/mlx-ml/python
|
|
15
|
-
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..' // dist/mlx-ml/process -> dist/mlx-ml -> dist -> package root
|
|
16
|
-
);
|
|
17
|
-
const mlxDriverDir = path.join(packageRoot, 'src', 'mlx-ml', 'python');
|
|
18
8
|
export class ProcessCommunication {
|
|
19
9
|
process;
|
|
20
10
|
decoder;
|
|
@@ -22,32 +12,34 @@ export class ProcessCommunication {
|
|
|
22
12
|
jsonBuffer = '';
|
|
23
13
|
draining = false;
|
|
24
14
|
callbacks;
|
|
25
|
-
|
|
15
|
+
exitErrorMessage;
|
|
16
|
+
constructor(config, callbacks) {
|
|
26
17
|
this.callbacks = callbacks;
|
|
27
18
|
this.decoder = new StringDecoder('utf8');
|
|
19
|
+
this.exitErrorMessage =
|
|
20
|
+
config.processExitErrorMessage ??
|
|
21
|
+
((code, signal) => `Inference process exited unexpectedly (code=${code}, signal=${signal})`);
|
|
22
|
+
const logger = new Logger({
|
|
23
|
+
prefix: config.loggerPrefix ?? 'LIP',
|
|
24
|
+
context: config.loggerContext ?? 'process',
|
|
25
|
+
});
|
|
28
26
|
const args = [
|
|
29
27
|
'--project',
|
|
30
|
-
|
|
28
|
+
config.pythonProjectDir,
|
|
31
29
|
'run',
|
|
32
30
|
'python',
|
|
33
31
|
'__main__.py',
|
|
34
|
-
modelName
|
|
32
|
+
config.modelName,
|
|
33
|
+
...(config.extraArgs ?? []),
|
|
35
34
|
];
|
|
36
|
-
if (options?.textOnly) {
|
|
37
|
-
args.push('--text-only');
|
|
38
|
-
}
|
|
39
|
-
if (options?.drafterModel) {
|
|
40
|
-
args.push('--drafter', options.drafterModel);
|
|
41
|
-
}
|
|
42
|
-
if (options?.draftBlockSize !== undefined) {
|
|
43
|
-
args.push('--draft-block-size', options.draftBlockSize.toString());
|
|
44
|
-
}
|
|
45
35
|
this.process = spawn('uv', args, {
|
|
46
|
-
cwd:
|
|
36
|
+
cwd: config.pythonProjectDir,
|
|
37
|
+
env: {
|
|
38
|
+
...process.env,
|
|
39
|
+
UV_PROJECT_ENVIRONMENT: config.venvPath,
|
|
40
|
+
...config.extraEnv,
|
|
41
|
+
},
|
|
47
42
|
});
|
|
48
|
-
this.setupProcessHandlers();
|
|
49
|
-
}
|
|
50
|
-
setupProcessHandlers() {
|
|
51
43
|
this.process.stderr.on('data', (data) => {
|
|
52
44
|
logger.debug(data.toString());
|
|
53
45
|
});
|
|
@@ -59,36 +51,25 @@ export class ProcessCommunication {
|
|
|
59
51
|
});
|
|
60
52
|
this.process.on('exit', (code, signal) => {
|
|
61
53
|
if (this.currentStream) {
|
|
62
|
-
this.currentStream.destroy(new Error(
|
|
54
|
+
this.currentStream.destroy(new Error(this.exitErrorMessage(code, signal)));
|
|
63
55
|
this.currentStream = null;
|
|
64
56
|
}
|
|
65
57
|
this.callbacks.onProcessExit(code, signal);
|
|
66
58
|
});
|
|
67
59
|
}
|
|
68
|
-
/**
|
|
69
|
-
* stdoutから受信したデータを処理する
|
|
70
|
-
* null文字をレスポンス区切りとして解釈し、
|
|
71
|
-
* 1つのdataチャンクに複数のレスポンスが含まれる場合も正しく処理する
|
|
72
|
-
*/
|
|
73
60
|
processData(data) {
|
|
74
61
|
let remaining = data;
|
|
75
62
|
while (remaining.length > 0) {
|
|
76
63
|
const nullIndex = remaining.indexOf('\0');
|
|
77
64
|
if (nullIndex !== -1) {
|
|
78
|
-
// null文字が見つかった場合、レスポンス終了
|
|
79
65
|
const chunk = this.decoder.write(remaining.slice(0, nullIndex));
|
|
80
66
|
this.decoder = new StringDecoder('utf8');
|
|
81
67
|
if (this.currentStream) {
|
|
82
|
-
// ストリーミングレスポンスの場合
|
|
83
68
|
this.currentStream.push(chunk);
|
|
84
|
-
this.currentStream.push(null);
|
|
69
|
+
this.currentStream.push(null);
|
|
85
70
|
this.currentStream = null;
|
|
86
71
|
}
|
|
87
|
-
else if (this.draining) {
|
|
88
|
-
// キャンセル後のドレイン: データを破棄
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
// JSONレスポンスの場合
|
|
72
|
+
else if (!this.draining) {
|
|
92
73
|
this.jsonBuffer += chunk;
|
|
93
74
|
this.callbacks.onJsonResponse(this.jsonBuffer);
|
|
94
75
|
this.jsonBuffer = '';
|
|
@@ -97,21 +78,14 @@ export class ProcessCommunication {
|
|
|
97
78
|
if (this.draining) {
|
|
98
79
|
this.draining = false;
|
|
99
80
|
}
|
|
100
|
-
// null文字以降の残りデータを続けて処理
|
|
101
81
|
remaining = remaining.slice(nullIndex + 1);
|
|
102
82
|
}
|
|
103
83
|
else {
|
|
104
|
-
// null文字がない場合、データを蓄積
|
|
105
84
|
const chunk = this.decoder.write(remaining);
|
|
106
85
|
if (this.currentStream) {
|
|
107
|
-
// ストリーミング中
|
|
108
86
|
this.currentStream.push(chunk);
|
|
109
87
|
}
|
|
110
|
-
else if (this.draining) {
|
|
111
|
-
// キャンセル後のドレイン: データを破棄
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
// JSONレスポンス蓄積中
|
|
88
|
+
else if (!this.draining) {
|
|
115
89
|
this.jsonBuffer += chunk;
|
|
116
90
|
}
|
|
117
91
|
break;
|
|
@@ -120,15 +94,10 @@ export class ProcessCommunication {
|
|
|
120
94
|
}
|
|
121
95
|
createNewStream() {
|
|
122
96
|
this.currentStream = new Readable({
|
|
123
|
-
read() { }
|
|
97
|
+
read() { },
|
|
124
98
|
});
|
|
125
99
|
return this.currentStream;
|
|
126
100
|
}
|
|
127
|
-
/**
|
|
128
|
-
* Cancel the active streaming request.
|
|
129
|
-
* Destroys the current stream, drains stdout until the response terminator,
|
|
130
|
-
* and sends a cancel command to the Python process.
|
|
131
|
-
*/
|
|
132
101
|
cancelActiveStream() {
|
|
133
102
|
if (!this.currentStream && !this.draining) {
|
|
134
103
|
return;
|
|
@@ -140,9 +109,6 @@ export class ProcessCommunication {
|
|
|
140
109
|
this.draining = true;
|
|
141
110
|
this.sendToProcess(JSON.stringify({ method: 'cancel' }) + '\n');
|
|
142
111
|
}
|
|
143
|
-
isDraining() {
|
|
144
|
-
return this.draining;
|
|
145
|
-
}
|
|
146
112
|
sendToProcess(data) {
|
|
147
113
|
this.process.stdin.write(data);
|
|
148
114
|
}
|
|
@@ -155,15 +121,13 @@ export class ProcessCommunication {
|
|
|
155
121
|
async exit() {
|
|
156
122
|
return new Promise((resolve) => {
|
|
157
123
|
const timeout = setTimeout(() => {
|
|
158
|
-
// タイムアウト後は強制終了
|
|
159
124
|
this.process.kill('SIGTERM');
|
|
160
125
|
resolve();
|
|
161
|
-
}, 5000);
|
|
126
|
+
}, 5000);
|
|
162
127
|
this.process.once('exit', () => {
|
|
163
128
|
clearTimeout(timeout);
|
|
164
129
|
resolve();
|
|
165
130
|
});
|
|
166
|
-
// stdinを閉じてプロセスに終了を通知
|
|
167
131
|
this.process.stdin.end();
|
|
168
132
|
});
|
|
169
133
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-communication.js","sourceRoot":"","sources":["../../src/local-inference/process-communication.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,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ACZ,CAAC,CAAC,CAAC;YAEH,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Readable } from 'stream';
|
|
2
|
+
import type { InferenceCapabilities, InferenceMessage, InferenceRenderResult, InferenceToolDefinition } from './protocol.js';
|
|
3
|
+
/** LIP プロセスへの最小ポート(MlxProcess / 将来 PyTorchProcess が実装) */
|
|
4
|
+
export interface InferenceProcessPort {
|
|
5
|
+
ensureInitialized(): Promise<void>;
|
|
6
|
+
getCapabilities(): Promise<InferenceCapabilities>;
|
|
7
|
+
render(messages: InferenceMessage[], options?: Record<string, unknown>, tools?: InferenceToolDefinition[], reasoningEffort?: 'low' | 'medium' | 'high'): Promise<InferenceRenderResult>;
|
|
8
|
+
generate(prompt: string | number[], options?: Record<string, unknown>, images?: string[], maxImageSize?: number, cachePath?: string, cacheTrimTokens?: number, primer?: string): Promise<Readable>;
|
|
9
|
+
cancelActiveRequest(): void;
|
|
10
|
+
exit(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=process-port.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-port.d.ts","sourceRoot":"","sources":["../../src/local-inference/process-port.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,qBAAqB,EACrB,uBAAuB,EACxB,MAAM,eAAe,CAAC;AAEvB,0DAA0D;AAC1D,MAAM,WAAW,oBAAoB;IACnC,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC,eAAe,IAAI,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAClD,MAAM,CACJ,QAAQ,EAAE,gBAAgB,EAAE,EAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,KAAK,CAAC,EAAE,uBAAuB,EAAE,EACjC,eAAe,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAC1C,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAClC,QAAQ,CACN,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,EACzB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,MAAM,CAAC,EAAE,MAAM,EAAE,EACjB,YAAY,CAAC,EAAE,MAAM,EACrB,SAAS,CAAC,EAAE,MAAM,EAClB,eAAe,CAAC,EAAE,MAAM,EACxB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,QAAQ,CAAC,CAAC;IACrB,mBAAmB,IAAI,IAAI,CAAC;IAC5B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-port.js","sourceRoot":"","sources":["../../src/local-inference/process-port.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-utils.d.ts","sourceRoot":"","sources":["../../src/local-inference/prompt-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAcjE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the prompt contains MessageElement
|
|
3
|
+
*/
|
|
4
|
+
export function hasMessageElement(prompt) {
|
|
5
|
+
const checkElements = (elements) => {
|
|
6
|
+
if (!elements)
|
|
7
|
+
return false;
|
|
8
|
+
return elements.some((element) => {
|
|
9
|
+
const el = element;
|
|
10
|
+
return el?.type === 'message';
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
return (checkElements(prompt.instructions) ||
|
|
14
|
+
checkElements(prompt.data) ||
|
|
15
|
+
checkElements(prompt.output));
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=prompt-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-utils.js","sourceRoot":"","sources":["../../src/local-inference/prompt-utils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAsB;IACtD,MAAM,aAAa,GAAG,CAAC,QAAmB,EAAW,EAAE;QACrD,IAAI,CAAC,QAAQ;YAAE,OAAO,KAAK,CAAC;QAC5B,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAC/B,MAAM,EAAE,GAAG,OAA4B,CAAC;YACxC,OAAO,EAAE,EAAE,IAAI,KAAK,SAAS,CAAC;QAChC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,CACL,aAAa,CAAC,MAAM,CAAC,YAAY,CAAC;QAClC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC;QAC1B,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAC7B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local Inference Protocol (LIP) 型定義
|
|
3
|
+
*
|
|
4
|
+
* Thin Python 推論型ドライバ(MLX / PyTorch 等)が共有する stdio JSON-RPC プロトコル。
|
|
5
|
+
* Python プロセスからの応答は snake_case のまま受け取る。
|
|
6
|
+
*/
|
|
7
|
+
import type { SpecialToken, SpecialTokenPair } from '../formatter/types.js';
|
|
8
|
+
/** LIP で利用可能な JSON-RPC メソッド */
|
|
9
|
+
export type InferenceMethod = 'capabilities' | 'format_test' | 'render' | 'chat' | 'completion' | 'generate' | 'cache_prefill' | 'tokenize';
|
|
10
|
+
/** VLM content part for structured message content */
|
|
11
|
+
export type InferenceContentPart = {
|
|
12
|
+
type: 'text';
|
|
13
|
+
text: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'image';
|
|
16
|
+
};
|
|
17
|
+
/** 標準メッセージ(system / user / assistant) */
|
|
18
|
+
export interface InferenceStandardMessage {
|
|
19
|
+
role: 'user' | 'assistant' | 'system';
|
|
20
|
+
content: string | InferenceContentPart[];
|
|
21
|
+
}
|
|
22
|
+
/** tool_calls 付き assistant メッセージ(HuggingFace 互換形式) */
|
|
23
|
+
export interface InferenceAssistantToolCallMessage {
|
|
24
|
+
role: 'assistant';
|
|
25
|
+
content: string;
|
|
26
|
+
tool_calls: Array<{
|
|
27
|
+
id: string;
|
|
28
|
+
type: 'function';
|
|
29
|
+
function: {
|
|
30
|
+
name: string;
|
|
31
|
+
arguments: string;
|
|
32
|
+
};
|
|
33
|
+
}>;
|
|
34
|
+
}
|
|
35
|
+
/** tool result メッセージ(HuggingFace 互換形式) */
|
|
36
|
+
export interface InferenceToolResultMessage {
|
|
37
|
+
role: 'tool';
|
|
38
|
+
content: string;
|
|
39
|
+
tool_call_id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
}
|
|
42
|
+
export type InferenceMessage = InferenceStandardMessage | InferenceAssistantToolCallMessage | InferenceToolResultMessage;
|
|
43
|
+
/** HuggingFace apply_chat_template 互換の tool 定義 */
|
|
44
|
+
export interface InferenceToolDefinition {
|
|
45
|
+
type: 'function';
|
|
46
|
+
function: {
|
|
47
|
+
name: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
parameters?: Record<string, unknown>;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** サンプリング等の推論オプション(バックエンド共通の最小集合) */
|
|
53
|
+
export interface InferenceSamplingOptions {
|
|
54
|
+
max_tokens?: number;
|
|
55
|
+
temperature?: number;
|
|
56
|
+
top_p?: number;
|
|
57
|
+
top_k?: number;
|
|
58
|
+
repetition_penalty?: number;
|
|
59
|
+
repetition_context_size?: number;
|
|
60
|
+
trust_remote_code?: boolean;
|
|
61
|
+
}
|
|
62
|
+
export interface InferenceBaseRequest {
|
|
63
|
+
method: InferenceMethod;
|
|
64
|
+
}
|
|
65
|
+
export interface InferenceCapabilitiesRequest extends InferenceBaseRequest {
|
|
66
|
+
method: 'capabilities';
|
|
67
|
+
}
|
|
68
|
+
export interface InferenceFormatTestRequest extends InferenceBaseRequest {
|
|
69
|
+
method: 'format_test';
|
|
70
|
+
messages: InferenceMessage[];
|
|
71
|
+
options?: {
|
|
72
|
+
primer?: string;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export interface InferenceTokenizeRequest extends InferenceBaseRequest {
|
|
76
|
+
method: 'tokenize';
|
|
77
|
+
messages: InferenceMessage[];
|
|
78
|
+
tools?: InferenceToolDefinition[];
|
|
79
|
+
reasoning_effort?: 'low' | 'medium' | 'high';
|
|
80
|
+
}
|
|
81
|
+
export interface InferenceRenderRequest extends InferenceBaseRequest {
|
|
82
|
+
method: 'render';
|
|
83
|
+
messages: InferenceMessage[];
|
|
84
|
+
options?: InferenceSamplingOptions & {
|
|
85
|
+
primer?: string;
|
|
86
|
+
};
|
|
87
|
+
tools?: InferenceToolDefinition[];
|
|
88
|
+
reasoning_effort?: 'low' | 'medium' | 'high';
|
|
89
|
+
}
|
|
90
|
+
/** @deprecated Python chat ハンドラ廃止。render + generate を使用すること。 */
|
|
91
|
+
export interface InferenceChatRequest extends InferenceBaseRequest {
|
|
92
|
+
method: 'chat';
|
|
93
|
+
messages: InferenceMessage[];
|
|
94
|
+
primer?: string;
|
|
95
|
+
tools?: InferenceToolDefinition[];
|
|
96
|
+
options?: InferenceSamplingOptions;
|
|
97
|
+
images?: string[];
|
|
98
|
+
maxImageSize?: number;
|
|
99
|
+
reasoning_effort?: 'low' | 'medium' | 'high';
|
|
100
|
+
cache_path?: string;
|
|
101
|
+
cache_trim_tokens?: number;
|
|
102
|
+
}
|
|
103
|
+
export interface InferenceRenderResult {
|
|
104
|
+
formatted_prompt: string | null;
|
|
105
|
+
error: string | null;
|
|
106
|
+
}
|
|
107
|
+
export interface InferenceCompletionRequest extends InferenceBaseRequest {
|
|
108
|
+
method: 'completion';
|
|
109
|
+
prompt: string;
|
|
110
|
+
options?: InferenceSamplingOptions;
|
|
111
|
+
images?: string[];
|
|
112
|
+
maxImageSize?: number;
|
|
113
|
+
}
|
|
114
|
+
/** 整形済み prompt(文字列 or トークン ID)のストリーム推論 */
|
|
115
|
+
export interface InferenceGenerateRequest extends InferenceBaseRequest {
|
|
116
|
+
method: 'generate';
|
|
117
|
+
prompt: string | number[];
|
|
118
|
+
primer?: string;
|
|
119
|
+
options?: InferenceSamplingOptions;
|
|
120
|
+
images?: string[];
|
|
121
|
+
maxImageSize?: number;
|
|
122
|
+
cache_path?: string;
|
|
123
|
+
cache_trim_tokens?: number;
|
|
124
|
+
}
|
|
125
|
+
export interface InferenceCachePrefillRequest extends InferenceBaseRequest {
|
|
126
|
+
method: 'cache_prefill';
|
|
127
|
+
cache_path: string;
|
|
128
|
+
messages: InferenceMessage[];
|
|
129
|
+
base_cache_path?: string;
|
|
130
|
+
trim_to_tokens?: number;
|
|
131
|
+
prefix_offsets?: number[];
|
|
132
|
+
prefix_hashes?: string[];
|
|
133
|
+
tools?: InferenceToolDefinition[];
|
|
134
|
+
reasoning_effort?: 'low' | 'medium' | 'high';
|
|
135
|
+
}
|
|
136
|
+
export interface InferenceCachePrefillResult {
|
|
137
|
+
cache_path: string;
|
|
138
|
+
token_count?: number;
|
|
139
|
+
prefix_offsets?: number[];
|
|
140
|
+
prefix_hashes?: string[];
|
|
141
|
+
}
|
|
142
|
+
export type InferenceRequest = InferenceCapabilitiesRequest | InferenceFormatTestRequest | InferenceRenderRequest | InferenceTokenizeRequest | InferenceChatRequest | InferenceCompletionRequest | InferenceGenerateRequest | InferenceCachePrefillRequest;
|
|
143
|
+
/** MLX-LM が認識する tool_parser_type(capabilities 経由で参照)。
|
|
144
|
+
* 現時点では MLX 固有の知識。PyTorch バックエンド追加時(#302 Phase 6)に
|
|
145
|
+
* バックエンド固有モジュールへ移動・一般化する可能性あり。 */
|
|
146
|
+
export type KnownToolParserType = 'json_tools' | 'pythonic' | 'function_gemma' | 'mistral' | 'kimi_k2' | 'longcat' | 'glm47' | 'qwen3_coder' | 'minimax_m2' | 'gemma4' | 'harmony';
|
|
147
|
+
export interface ToolCallFormat {
|
|
148
|
+
tool_parser_type?: string;
|
|
149
|
+
call_start?: string;
|
|
150
|
+
call_end?: string;
|
|
151
|
+
response_start?: string;
|
|
152
|
+
response_end?: string;
|
|
153
|
+
}
|
|
154
|
+
export interface ChatTemplateInfo {
|
|
155
|
+
supported_roles: string[];
|
|
156
|
+
preview?: string;
|
|
157
|
+
constraints: Record<string, unknown>;
|
|
158
|
+
tool_call_format?: ToolCallFormat;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Python プロセスから取得するランタイム能力情報(snake_case)
|
|
162
|
+
*/
|
|
163
|
+
export interface InferenceCapabilities {
|
|
164
|
+
methods: string[];
|
|
165
|
+
special_tokens: Record<string, SpecialToken | SpecialTokenPair>;
|
|
166
|
+
features: {
|
|
167
|
+
apply_chat_template: boolean;
|
|
168
|
+
vocab_size?: number;
|
|
169
|
+
model_max_length?: number;
|
|
170
|
+
chat_template?: ChatTemplateInfo;
|
|
171
|
+
};
|
|
172
|
+
chat_restrictions?: {
|
|
173
|
+
single_system_at_start?: boolean;
|
|
174
|
+
max_system_messages?: number;
|
|
175
|
+
alternating_turns?: boolean;
|
|
176
|
+
requires_user_last?: boolean;
|
|
177
|
+
allow_empty_messages?: boolean;
|
|
178
|
+
};
|
|
179
|
+
model_kind?: 'lm' | 'vlm';
|
|
180
|
+
}
|
|
181
|
+
export interface InferenceFormatTestResult {
|
|
182
|
+
formatted_prompt: string | null;
|
|
183
|
+
template_applied: boolean;
|
|
184
|
+
model_specific_processing: InferenceMessage[] | null;
|
|
185
|
+
error: string | null;
|
|
186
|
+
}
|
|
187
|
+
export interface InferenceTokenizeResult {
|
|
188
|
+
token_ids: number[] | null;
|
|
189
|
+
token_count: number;
|
|
190
|
+
error: string | null;
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=protocol.d.ts.map
|