Browse Source

update examples

- remove chat example
- add cat image for multimodal example
Bruce MacDonald 1 year ago
parent
commit
979ed02c64

+ 0 - 3
examples/async-chat-stream/README.md

@@ -1,3 +0,0 @@
-# async-chat-stream
-
-This example demonstrates how to create a conversation history using an streaming Ollama client and the chat endpoint. The streaming response is outputted to `stdout` as well as a TTS if enabled with `--speak` and available. Supported TTS are `say` on macOS and `espeak` on Linux.

+ 0 - 102
examples/async-chat-stream/chat.ts

@@ -1,102 +0,0 @@
-import { spawn, ChildProcessWithoutNullStreams, execSync } from 'child_process'
-import readline, { Interface } from 'readline'
-import ollama, { Message } from 'ollama'
-
-interface CommandLineArguments {
-  speak?: boolean
-}
-
-async function speak(speaker: string | null, content: string): Promise<void> {
-  if (speaker) {
-    const process: ChildProcessWithoutNullStreams = spawn(speaker, [content])
-    await new Promise<void>((resolve) => process.on('close', () => resolve()))
-  }
-}
-
-function parseCommandLineArguments(): CommandLineArguments {
-  const args: CommandLineArguments = {}
-  process.argv.slice(2).forEach((arg) => {
-    if (arg.startsWith('--')) {
-      const key = arg.replace('--', '')
-      args[key] = true
-    }
-  })
-  return args
-}
-
-async function main(): Promise<void> {
-  const args: CommandLineArguments = parseCommandLineArguments()
-
-  let speaker: string | null = null
-  if (args.speak) {
-    if (process.platform === 'darwin') {
-      speaker = 'say'
-    } else {
-      try {
-        // Try to find 'espeak' or 'espeak-ng'
-        const espeakPath = execSync('which espeak', { stdio: 'pipe' }).toString().trim()
-        if (espeakPath) speaker = 'espeak'
-
-        const espeakNgPath = execSync('which espeak-ng', { stdio: 'pipe' })
-          .toString()
-          .trim()
-        if (espeakNgPath) speaker = 'espeak-ng'
-      } catch (error) {
-        console.warn('No speaker found')
-      }
-    }
-  }
-
-  const messages: Message[] = []
-  const rl: Interface = readline.createInterface({
-    input: process.stdin,
-    output: process.stdout,
-    prompt: '>>> ',
-  })
-
-  rl.prompt()
-
-  for await (const line of rl) {
-    if (line) {
-      messages.push({ role: 'user', content: line })
-
-      let contentOut: string = ''
-      let message = { role: 'assistant', content: '' }
-      for await (const response of await ollama.chat({
-        model: 'mistral',
-        messages: messages,
-        stream: true,
-      })) {
-        if (response.done) {
-          messages.push(message)
-        }
-
-        const content: string = response.message.content
-        process.stdout.write(content, 'utf-8')
-
-        contentOut += content
-        if (['.', '!', '?', '\n'].includes(content)) {
-          await speak(speaker, contentOut)
-          contentOut = ''
-        }
-
-        message.content += content
-      }
-
-      if (contentOut) {
-        await speak(speaker, contentOut)
-      }
-      console.log()
-      rl.prompt()
-    }
-  }
-}
-
-main().catch((error) => {
-  console.error(error)
-  process.exit(1)
-})
-
-process.on('SIGINT', () => {
-  process.exit(0)
-})

+ 1 - 1
examples/fill-in-middle/fill.ts

@@ -1,4 +1,4 @@
-import ollama, { Message } from 'ollama'
+import ollama from 'ollama'
 
 const prefix = `def remove_non_ascii(s: str) -> str:
 """

BIN
examples/multimodal/cat.jpg


+ 9 - 36
examples/multimodal/multimodal.ts

@@ -1,40 +1,13 @@
 import https from 'https'
 import ollama from 'ollama'
 
-interface XKCDResponse {
-  num: number
-  alt: string
-  img: string
-}
-
-async function fetchImageAsBuffer(url: string): Promise<Buffer> {
-  return new Promise<Buffer>((resolve, reject) => {
-    https.get(url, (res) => {
-      const chunks: Buffer[] = []
-      res.on('data', (chunk: Buffer) => chunks.push(chunk))
-      res.on('end', () => resolve(Buffer.concat(chunks)))
-      res.on('error', reject)
-    })
-  })
-}
-
-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)
-
-  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 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)
 }