This commit is contained in:
Andrea Bianchi
2026-05-26 08:43:10 +09:00
parent 70649dff69
commit f80186aacd
6 changed files with 121 additions and 0 deletions

9
w14_olama/README.md Normal file
View File

@@ -0,0 +1,9 @@
# Expose the server
```sh
OLLAMA_HOST=0.0.0.0:11434 ollama serve
or
OLLAMA_HOST=127.0.0.1:8080 ollama serve
```

16
w14_olama/basics.js Normal file
View File

@@ -0,0 +1,16 @@
import delay from 'delay';
fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'qwen2.5-coder:1.5b', // replace with your model name
prompt: 'Tell me a joke!',
stream: false,
think: false,
}),
})
.then((res) => res.json())
.then((data) => data.response)
.then(console.log)
.catch(console.error);

18
w14_olama/ollama.js Normal file
View File

@@ -0,0 +1,18 @@
// https://github.com/ollama/ollama-js
import ollama from 'ollama';
// Generate
const response = await ollama.generate({
model: 'deepseek-r1:latest',
prompt: 'tell a joke',
think: false,
});
console.log('Generate:', response);
// Chat
const responseChat = await ollama.chat({
model: 'deepseek-r1:latest',
messages: [{ role: 'user', content: 'tell a joke' }],
think: false,
});
console.log('Chat:', responseChat);

40
w14_olama/package-lock.json generated Normal file
View File

@@ -0,0 +1,40 @@
{
"name": "olama",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"delay": "^6.0.0",
"ollama": "^0.6.3"
}
},
"node_modules/delay": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/delay/-/delay-6.0.0.tgz",
"integrity": "sha512-2NJozoOHQ4NuZuVIr5CWd0iiLVIRSDepakaovIN+9eIDHEhdCAEvSy2cuf1DCrPPQLvHmbqTHODlhHg8UCy4zw==",
"license": "MIT",
"engines": {
"node": ">=16"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/ollama": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/ollama/-/ollama-0.6.3.tgz",
"integrity": "sha512-KEWEhIqE5wtfzEIZbDCLH51VFZ6Z3ZSa6sIOg/E/tBV8S51flyqBOXi+bRxlOYKDf8i327zG9eSTb8IJxvm3Zg==",
"license": "MIT",
"dependencies": {
"whatwg-fetch": "^3.6.20"
}
},
"node_modules/whatwg-fetch": {
"version": "3.6.20",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz",
"integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==",
"license": "MIT"
}
}
}

7
w14_olama/package.json Normal file
View File

@@ -0,0 +1,7 @@
{
"dependencies": {
"delay": "^6.0.0",
"ollama": "^0.6.3"
},
"type": "module"
}

31
w14_olama/stream.js Normal file
View File

@@ -0,0 +1,31 @@
import delay from 'delay';
fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'deepseek-r1:latest',
prompt: 'Hello! Tell me a long long joke.',
stream: true,
think: false,
}),
})
.then((res) => {
const reader = res.body.getReader();
const decoder = new TextDecoder();
reader.read().then(async ({ done, value }) => {
while (!done) {
// Convert chunk to string
const chunk = decoder.decode(value, { stream: true });
// Optionally, print each chunk as it arrives
const line = JSON.parse(chunk);
process.stdout.write(line.response ?? '');
// Read the next chunk
({ done, value } = await reader.read());
}
});
})
.catch(console.error);