From f80186aacd8883e0fb165653653c1983669d3e36 Mon Sep 17 00:00:00 2001 From: Andrea Bianchi Date: Tue, 26 May 2026 08:43:10 +0900 Subject: [PATCH] ollama --- w14_olama/README.md | 9 +++++++++ w14_olama/basics.js | 16 +++++++++++++++ w14_olama/ollama.js | 18 +++++++++++++++++ w14_olama/package-lock.json | 40 +++++++++++++++++++++++++++++++++++++ w14_olama/package.json | 7 +++++++ w14_olama/stream.js | 31 ++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 w14_olama/README.md create mode 100644 w14_olama/basics.js create mode 100644 w14_olama/ollama.js create mode 100644 w14_olama/package-lock.json create mode 100644 w14_olama/package.json create mode 100644 w14_olama/stream.js diff --git a/w14_olama/README.md b/w14_olama/README.md new file mode 100644 index 0000000..1e59736 --- /dev/null +++ b/w14_olama/README.md @@ -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 +``` diff --git a/w14_olama/basics.js b/w14_olama/basics.js new file mode 100644 index 0000000..728ab18 --- /dev/null +++ b/w14_olama/basics.js @@ -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); diff --git a/w14_olama/ollama.js b/w14_olama/ollama.js new file mode 100644 index 0000000..3b6116e --- /dev/null +++ b/w14_olama/ollama.js @@ -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); diff --git a/w14_olama/package-lock.json b/w14_olama/package-lock.json new file mode 100644 index 0000000..6811b2a --- /dev/null +++ b/w14_olama/package-lock.json @@ -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" + } + } +} diff --git a/w14_olama/package.json b/w14_olama/package.json new file mode 100644 index 0000000..e248097 --- /dev/null +++ b/w14_olama/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "delay": "^6.0.0", + "ollama": "^0.6.3" + }, + "type": "module" +} diff --git a/w14_olama/stream.js b/w14_olama/stream.js new file mode 100644 index 0000000..cf85856 --- /dev/null +++ b/w14_olama/stream.js @@ -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);