Ver código fonte

clean up examples

- remove stream examples
- remove await main
Bruce MacDonald 1 ano atrás
pai
commit
324309d25c

+ 0 - 11
examples/chat-stream/chat.ts

@@ -1,11 +0,0 @@
-import ollama, { Message } from 'ollama'
-
-async function main(): Promise<void> {
-  const messages: Message[] = [{ role: 'user', content: 'Why is the sky blue?' }]
-  const stream = await ollama.chat({ model: 'mistral', messages, stream: true })
-  for await (const part of stream) {
-    process.stdout.write(part.message.content)
-  }
-}
-
-await main()

+ 14 - 18
examples/fill-in-middle/fill.ts

@@ -1,24 +1,20 @@
 import ollama, { Message } from 'ollama'
 
-async function main(): Promise<void> {
-  const prefix = `def remove_non_ascii(s: str) -> str:
+const prefix = `def remove_non_ascii(s: str) -> str:
 """
 `
-  const suffix = `
+const suffix = `
 return result
 `
-  const response = await ollama.generate({
-    model: 'codellama:7b-code',
-    prompt: `<PRE> ${prefix} <SUF>${suffix} <MID>`,
-    options: {
-      num_predict: 128,
-      temperature: 0,
-      top_p: 0.9,
-      presence_penalty: 0,
-      stop: ['<EOT>'],
-    },
-  })
-  console.log(respose.response)
-}
-
-await main()
+const response = await ollama.generate({
+  model: 'codellama:7b-code',
+  prompt: `<PRE> ${prefix} <SUF>${suffix} <MID>`,
+  options: {
+    num_predict: 128,
+    temperature: 0,
+    top_p: 0.9,
+    presence_penalty: 0,
+    stop: ['<EOT>'],
+  },
+})
+console.log(response.response)

+ 0 - 14
examples/generate-stream/generate.ts

@@ -1,14 +0,0 @@
-import ollama from 'ollama'
-
-async function main(): Promise<void> {
-  const stream = await ollama.generate({
-    model: 'mistral',
-    prompt: 'Why is the sky blue?',
-    stream: true,
-  })
-  for await (const part of stream) {
-    process.stdout.write(part.response)
-  }
-}
-
-await main()

+ 16 - 20
examples/multimodal/multimodal.ts

@@ -18,27 +18,23 @@ async function fetchImageAsBuffer(url: string): Promise<Buffer> {
   })
 }
 
-async function main(): Promise<void> {
-  try {
-    // Fetch the latest XKCD comic info
-    const latestComicResponse = await fetch('https://xkcd.com/info.0.json')
-    const latestComic: XKCDResponse = await latestComicResponse.json()
+try {
+  // Fetch the latest XKCD comic info
+  const latestComicResponse = await fetch('https://xkcd.com/info.0.json')
+  const latestComic: XKCDResponse = await latestComicResponse.json()
 
-    // Fetch the image data as a Buffer
-    const imageBuffer: Buffer = await fetchImageAsBuffer(latestComic.img)
+  // Fetch the image data as a Buffer
+  const imageBuffer: Buffer = await fetchImageAsBuffer(latestComic.img)
 
-    const response = await ollama.generate({
-      model: 'llava',
-      prompt: 'explain this comic:',
-      images: [imageBuffer],
-      stream: true,
-    })
-    for await (const part of response) {
-      process.stdout.write(part.response)
-    }
-  } catch (error) {
-    console.error(error)
+  const response = await ollama.generate({
+    model: 'llava',
+    prompt: 'explain this comic:',
+    images: [imageBuffer],
+    stream: true,
+  })
+  for await (const part of response) {
+    process.stdout.write(part.response)
   }
+} catch (error) {
+  console.error(error)
 }
-
-await main()

+ 19 - 23
examples/pull-progress/pull.ts

@@ -1,29 +1,25 @@
 import ollama from 'ollama'
 
-async function main(): Promise<void> {
-  const model = 'falcon'
-  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
-      }
+const model = 'falcon'
+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 {
-      console.log(part.status)
+      currentDigestDone = false
     }
+  } else {
+    console.log(part.status)
   }
 }
-
-await main()