llmjs2 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/README.md +1 -0
- package/llmjs.js +61 -0
- package/package.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
llmjs
|
package/llmjs.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// llmjs.js
|
|
2
|
+
export async function completion({
|
|
3
|
+
model,
|
|
4
|
+
apiKey,
|
|
5
|
+
userPrompt,
|
|
6
|
+
systemPrompt = "You are a helpful assistant."
|
|
7
|
+
}) {
|
|
8
|
+
const [provider, modelName] = model.split('/');
|
|
9
|
+
|
|
10
|
+
const providers = {
|
|
11
|
+
openai: {
|
|
12
|
+
url: 'https://api.openai.com/v1/chat/completions',
|
|
13
|
+
headers: {
|
|
14
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
15
|
+
'Content-Type': 'application/json'
|
|
16
|
+
},
|
|
17
|
+
body: (m, p) => ({
|
|
18
|
+
model: m,
|
|
19
|
+
messages: [{ role: "system", content: systemPrompt }, { role: "user", content: p }]
|
|
20
|
+
}),
|
|
21
|
+
transform: (res) => res.choices[0].message.content
|
|
22
|
+
},
|
|
23
|
+
ollama: {
|
|
24
|
+
// Defaulting to the Ollama Cloud API (2026 standard)
|
|
25
|
+
url: 'https://ollama.com/api/chat',
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'application/json',
|
|
28
|
+
// Cloud access requires the Bearer token
|
|
29
|
+
...(apiKey ? { 'Authorization': `Bearer ${apiKey}` } : {})
|
|
30
|
+
},
|
|
31
|
+
body: (m, p) => ({
|
|
32
|
+
model: m,
|
|
33
|
+
messages: [{ role: "user", content: p }],
|
|
34
|
+
stream: false
|
|
35
|
+
}),
|
|
36
|
+
transform: (res) => res.message.content
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const config = providers[provider];
|
|
41
|
+
if (!config) throw new Error(`Provider "${provider}" is not supported.`);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const response = await fetch(config.url, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: config.headers,
|
|
47
|
+
body: JSON.stringify(config.body(modelName, userPrompt))
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
const errorText = await response.text();
|
|
52
|
+
throw new Error(`API Error (${response.status}): ${errorText}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const data = await response.json();
|
|
56
|
+
return config.transform(data);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(`LLMJS Error [${model}]:`, error.message);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "llmjs2",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A very lightweight llm library for building small or personal AI",
|
|
5
|
+
"main": "llmjs.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"llmjs",
|
|
9
|
+
"llm"
|
|
10
|
+
],
|
|
11
|
+
"author": "littlellmjs",
|
|
12
|
+
"files": [
|
|
13
|
+
"llmjs.js",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=16.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|