Bruce MacDonald 1 vuosi sitten
vanhempi
commit
de7fda01de

+ 82 - 77
examples/async-chat-stream/chat.ts

@@ -1,97 +1,102 @@
-import { spawn, ChildProcessWithoutNullStreams, execSync } from 'child_process';
-import readline, { Interface } from 'readline';
-import ollama from '../../src/index';
-import { Message } from '../../src/interfaces';
+import { spawn, ChildProcessWithoutNullStreams, execSync } from 'child_process'
+import readline, { Interface } from 'readline'
+import ollama, { Message } from 'ollama'
 
 
 interface CommandLineArguments {
 interface CommandLineArguments {
-    speak?: boolean;
+  speak?: boolean
 }
 }
 
 
 async function speak(speaker: string | null, content: string): Promise<void> {
 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()));
-    }
+  if (speaker) {
+    const process: ChildProcessWithoutNullStreams = spawn(speaker, [content])
+    await new Promise<void>((resolve) => process.on('close', () => resolve()))
+  }
 }
 }
 
 
 function parseCommandLineArguments(): CommandLineArguments {
 function parseCommandLineArguments(): CommandLineArguments {
-    const args: CommandLineArguments = {};
-    process.argv.slice(2).forEach((arg) => {
-        if (arg.startsWith('--')) {
-            const key = arg.replace('--', '');
-            args[key] = true;
-        }
-    });
-    return args;
+  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> {
 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 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: '>>> ',
+  })
 
 
-    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();
+  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) => {
 main().catch((error) => {
-    console.error(error);
-    process.exit(1);
-});
+  console.error(error)
+  process.exit(1)
+})
 
 
 process.on('SIGINT', () => {
 process.on('SIGINT', () => {
-    process.exit(0);
-});
+  process.exit(0)
+})

+ 7 - 10
examples/chat-stream/chat.ts

@@ -1,14 +1,11 @@
-import ollama from '../../src/index';
-import { Message } from '../../src/interfaces';
+import ollama, { Message } from 'ollama'
 
 
 async function main(): Promise<void> {
 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);
-    }
+  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();
+await main()

+ 5 - 8
examples/chat/chat.ts

@@ -1,12 +1,9 @@
-import ollama from '../../src/index';
-import { Message } from '../../src/interfaces';
+import ollama, { Message } from 'ollama'
 
 
 async function main(): Promise<void> {
 async function main(): Promise<void> {
-    const messages: Message[] = [
-        { role: "user", content: "Why is the sky blue?" },
-    ];
-    const response = await ollama.chat({model: "mistral", messages});
-    console.log(response.message.content);
+  const messages: Message[] = [{ role: 'user', content: 'Why is the sky blue?' }]
+  const response = await ollama.chat({ model: 'mistral', messages })
+  console.log(response.message.content)
 }
 }
 
 
-await main();
+await main()

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

@@ -1,15 +1,24 @@
-import { Ollama } from '../../src/index';
+import ollama, { Message } from 'ollama'
 
 
 async function main(): Promise<void> {
 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
 return result
-`;
-    const client: Ollama = new Ollama();
-    const respose = await client.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);
+`
+  const respose = 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();
+await main()

+ 10 - 6
examples/generate-stream/generate.ts

@@ -1,10 +1,14 @@
-import ollama from '../../src/index';
+import ollama from 'ollama'
 
 
 async function main(): Promise<void> {
 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);
-    }
+  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();
+await main()

+ 7 - 4
examples/generate/generate.ts

@@ -1,8 +1,11 @@
-import ollama from '../../src/index';
+import ollama from 'ollama'
 
 
 async function main(): Promise<void> {
 async function main(): Promise<void> {
-    const respose = await ollama.generate({model: "mistral", prompt: "Why is the sky blue?"});
-    console.log(respose.response);
+  const respose = await ollama.generate({
+    model: 'mistral',
+    prompt: 'Why is the sky blue?',
+  })
+  console.log(respose.response)
 }
 }
 
 
-await main();
+await main()

+ 31 - 26
examples/multimodal/multimodal.ts

@@ -1,39 +1,44 @@
-import https from 'https';
-import ollama from '../../src/index';
+import https from 'https'
+import ollama from 'ollama'
 
 
 interface XKCDResponse {
 interface XKCDResponse {
-    num: number;
-    alt: string;
-    img: string;
+  num: number
+  alt: string
+  img: string
 }
 }
 
 
 async function fetchImageAsBuffer(url: string): Promise<Buffer> {
 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);
-        });
-    });
+  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)
+    })
+  })
 }
 }
 
 
 async function main(): Promise<void> {
 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();
+await main()

+ 24 - 24
examples/pull-progress/pull.ts

@@ -1,29 +1,29 @@
-import ollama from '../../src/index';
+import ollama from 'ollama'
 
 
 async function main(): Promise<void> {
 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;
-            }
-        } else {
-            console.log(part.status)
-        }
+  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
+      }
+    } else {
+      console.log(part.status)
     }
     }
+  }
 }
 }
 
 
-await main();
+await main()