Skip to content

Commit

Permalink
fix:fix top await in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixingshi committed Feb 7, 2025
1 parent be88077 commit 61ef551
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
16 changes: 10 additions & 6 deletions examples/fill-in-middle/fill.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import ollama from 'ollama'

const response = await ollama.generate({
model: 'deepseek-coder-v2',
prompt: `def add(`,
suffix: `return c`,
})
console.log(response.response)
async function main() {
const response = await ollama.generate({
model: 'deepseek-coder-v2',
prompt: `def add(`,
suffix: `return c`,
})
console.log(response.response)
}

main().catch(console.error)
22 changes: 13 additions & 9 deletions examples/multimodal/multimodal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import ollama from 'ollama'

const imagePath = './examples/multimodal/cat.jpg'
const response = await ollama.generate({
model: 'llava',
prompt: 'describe this image:',
images: [imagePath],
stream: true,
})
for await (const part of response) {
process.stdout.write(part.response)
async function main() {
const imagePath = './examples/multimodal/cat.jpg'
const response = await ollama.generate({
model: 'llava',
prompt: 'describe this image:',
images: [imagePath],
stream: true,
})
for await (const part of response) {
process.stdout.write(part.response)
}
}

main().catch(console.error)
42 changes: 23 additions & 19 deletions examples/pull-progress/pull.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import ollama from 'ollama'

const model = 'llama3.1'
console.log(`downloading ${model}...`)
let currentDigestDone = false
const stream = await ollama.pull({ model: model, stream: true })
for await (const part of stream) {
if (part.digest) {
let percent = 0
if (part.completed && part.total) {
percent = Math.round((part.completed / part.total) * 100)
}
process.stdout.clearLine(0) // Clear the current line
process.stdout.cursorTo(0) // Move cursor to the beginning of the line
process.stdout.write(`${part.status} ${percent}%...`) // Write the new text
if (percent === 100 && !currentDigestDone) {
console.log() // Output to a new line
currentDigestDone = true
async function main() {
const model = 'llama3.1'
console.log(`downloading ${model}...`)
let currentDigestDone = false
const stream = await ollama.pull({ model: model, stream: true })
for await (const part of stream) {
if (part.digest) {
let percent = 0
if (part.completed && part.total) {
percent = Math.round((part.completed / part.total) * 100)
}
process.stdout.clearLine(0) // Clear the current line
process.stdout.cursorTo(0) // Move cursor to the beginning of the line
process.stdout.write(`${part.status} ${percent}%...`) // Write the new text
if (percent === 100 && !currentDigestDone) {
console.log() // Output to a new line
currentDigestDone = true
} else {
currentDigestDone = false
}
} else {
currentDigestDone = false
console.log(part.status)
}
} else {
console.log(part.status)
}
}

main().catch(console.error)

0 comments on commit 61ef551

Please sign in to comment.