@yapyak/ollama 0.0.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.
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +90 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joakim Uhrwing
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @yapyak/ollama
|
|
2
|
+
|
|
3
|
+
> Node-only
|
|
4
|
+
|
|
5
|
+
Ollama translator for yapyak.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @yapyak/ollama
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @yapyak/ollama
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Documentation
|
|
16
|
+
|
|
17
|
+
[yapyak.dev/reference/ollama](https://yapyak.dev/reference/ollama)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ContextLevel, Translator } from "yapyak/translator";
|
|
2
|
+
//#region src/ollama.d.ts
|
|
3
|
+
/** Options for {@link ollama}. */
|
|
4
|
+
type OllamaOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* The maximum items per API call.
|
|
7
|
+
*
|
|
8
|
+
* @defaultValue `8`
|
|
9
|
+
*/
|
|
10
|
+
batchSize?: number;
|
|
11
|
+
/**
|
|
12
|
+
* The maximum parallel API calls.
|
|
13
|
+
*
|
|
14
|
+
* @defaultValue `5`
|
|
15
|
+
*/
|
|
16
|
+
concurrency?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The call-site context level.
|
|
19
|
+
*
|
|
20
|
+
* @defaultValue `'minimal'`
|
|
21
|
+
*/
|
|
22
|
+
context?: ContextLevel;
|
|
23
|
+
/**
|
|
24
|
+
* The API endpoint.
|
|
25
|
+
*
|
|
26
|
+
* @defaultValue `'http://localhost:11434/api/generate'`
|
|
27
|
+
*/
|
|
28
|
+
endpoint?: string;
|
|
29
|
+
/** The translation glossary. */
|
|
30
|
+
glossary?: Record<string, Record<string, string>>;
|
|
31
|
+
/** The extra request headers. */
|
|
32
|
+
headers?: Record<string, string>;
|
|
33
|
+
/**
|
|
34
|
+
* The maximum retry attempts.
|
|
35
|
+
*
|
|
36
|
+
* @defaultValue `1`
|
|
37
|
+
*/
|
|
38
|
+
maxRetries?: number;
|
|
39
|
+
/** The output-token cap. */
|
|
40
|
+
maxTokens?: number;
|
|
41
|
+
/**
|
|
42
|
+
* The model name.
|
|
43
|
+
*
|
|
44
|
+
* @defaultValue `'llama3.1'`
|
|
45
|
+
*/
|
|
46
|
+
model?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The sampling temperature.
|
|
49
|
+
*
|
|
50
|
+
* @defaultValue `0.2`
|
|
51
|
+
*/
|
|
52
|
+
temperature?: number;
|
|
53
|
+
/**
|
|
54
|
+
* The request timeout in milliseconds.
|
|
55
|
+
*
|
|
56
|
+
* @defaultValue `120_000`
|
|
57
|
+
*/
|
|
58
|
+
timeout?: number;
|
|
59
|
+
/** The voice and tone guidance. */
|
|
60
|
+
voice?: string;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Creates an Ollama translator.
|
|
64
|
+
*
|
|
65
|
+
* @param options - The options.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts [yapyak.config.ts]
|
|
69
|
+
* import { defineConfig } from 'yapyak/config';
|
|
70
|
+
* import { ollama } from '@yapyak/ollama';
|
|
71
|
+
*
|
|
72
|
+
* export default defineConfig({
|
|
73
|
+
* translator: ollama({ model: 'llama3.1' })
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function ollama(options?: OllamaOptions): Translator;
|
|
78
|
+
//#endregion
|
|
79
|
+
export { type OllamaOptions, ollama };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { TranslatorInvalidResponseError, TranslatorTruncatedError, createTranslator } from "yapyak/translator";
|
|
2
|
+
import { buildSystem, causeToError, fetchWithRetry, parseResponseBody, parseTranslationsBatch, resolveMaxTokens, responseToError } from "yapyak/translator/internal";
|
|
3
|
+
//#region src/ollama.ts
|
|
4
|
+
const DEFAULT_MODEL = "llama3.1";
|
|
5
|
+
const DEFAULT_ENDPOINT = "http://localhost:11434/api/generate";
|
|
6
|
+
const DEFAULT_TEMPERATURE = .2;
|
|
7
|
+
const DEFAULT_TIMEOUT = 12e4;
|
|
8
|
+
const DEFAULT_MAX_RETRIES = 1;
|
|
9
|
+
const DEFAULT_BATCH_SIZE = 8;
|
|
10
|
+
const MAX_TOKENS_CAP = 4e3;
|
|
11
|
+
const MAX_TOKENS_FLOOR = 1024;
|
|
12
|
+
const MAX_TOKENS_PER_ITEM = 96;
|
|
13
|
+
/**
|
|
14
|
+
* Creates an Ollama translator.
|
|
15
|
+
*
|
|
16
|
+
* @param options - The options.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts [yapyak.config.ts]
|
|
20
|
+
* import { defineConfig } from 'yapyak/config';
|
|
21
|
+
* import { ollama } from '@yapyak/ollama';
|
|
22
|
+
*
|
|
23
|
+
* export default defineConfig({
|
|
24
|
+
* translator: ollama({ model: 'llama3.1' })
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function ollama(options = {}) {
|
|
29
|
+
const { batchSize = DEFAULT_BATCH_SIZE, concurrency, context, endpoint = DEFAULT_ENDPOINT, glossary, headers: customHeaders, maxRetries = DEFAULT_MAX_RETRIES, maxTokens, model = DEFAULT_MODEL, temperature = DEFAULT_TEMPERATURE, timeout = DEFAULT_TIMEOUT, voice } = options;
|
|
30
|
+
return createTranslator({
|
|
31
|
+
batchSize,
|
|
32
|
+
concurrency,
|
|
33
|
+
context,
|
|
34
|
+
id: "ollama",
|
|
35
|
+
translate: async (request) => {
|
|
36
|
+
const { items, signal, sourceLocale, targetLocales } = request;
|
|
37
|
+
const resolvedMaxTokens = resolveMaxTokens({
|
|
38
|
+
cap: MAX_TOKENS_CAP,
|
|
39
|
+
floor: MAX_TOKENS_FLOOR,
|
|
40
|
+
itemCount: items.length,
|
|
41
|
+
localeCount: targetLocales.length,
|
|
42
|
+
override: maxTokens,
|
|
43
|
+
perItem: MAX_TOKENS_PER_ITEM
|
|
44
|
+
});
|
|
45
|
+
const init = {
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
format: "json",
|
|
48
|
+
model,
|
|
49
|
+
options: {
|
|
50
|
+
num_predict: resolvedMaxTokens,
|
|
51
|
+
temperature
|
|
52
|
+
},
|
|
53
|
+
prompt: JSON.stringify(items),
|
|
54
|
+
stream: false,
|
|
55
|
+
system: buildSystem(sourceLocale, targetLocales, {
|
|
56
|
+
glossary,
|
|
57
|
+
voice
|
|
58
|
+
})
|
|
59
|
+
}),
|
|
60
|
+
headers: {
|
|
61
|
+
"content-type": "application/json",
|
|
62
|
+
...customHeaders
|
|
63
|
+
},
|
|
64
|
+
method: "POST"
|
|
65
|
+
};
|
|
66
|
+
const fetchOptions = {
|
|
67
|
+
maxRetries,
|
|
68
|
+
timeout
|
|
69
|
+
};
|
|
70
|
+
if (signal) fetchOptions.signal = signal;
|
|
71
|
+
let response;
|
|
72
|
+
try {
|
|
73
|
+
response = await fetchWithRetry(endpoint, init, fetchOptions);
|
|
74
|
+
} catch (cause) {
|
|
75
|
+
throw causeToError(cause, "ollama");
|
|
76
|
+
}
|
|
77
|
+
if (!response.ok) throw await responseToError(response, "ollama");
|
|
78
|
+
const responseBody = await parseResponseBody(response, "ollama", signal);
|
|
79
|
+
validateResponse(responseBody);
|
|
80
|
+
const text = responseBody.response;
|
|
81
|
+
if (typeof text !== "string") throw new TranslatorInvalidResponseError("yapyak ollama: response did not contain a response field.", { vendor: "ollama" });
|
|
82
|
+
return parseTranslationsBatch(text, "ollama");
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function validateResponse(body) {
|
|
87
|
+
if (body.done_reason === "length") throw new TranslatorTruncatedError("yapyak ollama: response truncated by token limit (done_reason='length'). Lower batchSize or raise `maxTokens` in the translator options.", { vendor: "ollama" });
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { ollama };
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yapyak/ollama",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Ollama translator for yapyak.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"i18n",
|
|
7
|
+
"ollama",
|
|
8
|
+
"translation",
|
|
9
|
+
"translator",
|
|
10
|
+
"yapyak"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://yapyak.dev",
|
|
13
|
+
"bugs": "https://github.com/yapyak/yapyak/issues",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/yapyak/yapyak.git",
|
|
17
|
+
"directory": "packages/ollama"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Joakim Uhrwing",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsdown": "0.22.12",
|
|
34
|
+
"typescript": "6.0.3",
|
|
35
|
+
"vitest": "4.1.10",
|
|
36
|
+
"@yapyak/tsdown-config": "0.0.0",
|
|
37
|
+
"@yapyak/typescript-config": "0.0.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"yapyak": "0.0.1"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22.22"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsdown",
|
|
50
|
+
"test": "pnpm -w vitest --run --project=@yapyak/ollama",
|
|
51
|
+
"typecheck": "tsc --noEmit"
|
|
52
|
+
}
|
|
53
|
+
}
|