Browse Source

xkcd explainer

Bruce MacDonald 1 year ago
parent
commit
4c77c1b5a2

+ 1 - 1
README.md

@@ -26,7 +26,7 @@ console.log(response.message.content)
 import ollama from 'ollama'
 
 const message = { role: 'user', content: 'Why is the sky blue?' }
-const { response } = await ollama.chat({ model: 'llama2', messages: [message], stream: true })
+const response = await ollama.chat({ model: 'llama2', messages: [message], stream: true })
 for await (const part of response) {
   process.stdout.write(part.message.content)
 }

+ 2 - 3
examples/async-chat-stream/chat.ts

@@ -1,6 +1,6 @@
 import { spawn, ChildProcessWithoutNullStreams, execSync } from 'child_process';
 import readline, { Interface } from 'readline';
-import { Ollama } from '../../src/index';
+import ollama from '../../src/index';
 import { Message } from '../../src/interfaces';
 
 interface CommandLineArguments {
@@ -46,7 +46,6 @@ async function main(): Promise<void> {
         }
     }
 
-    const client: Ollama = new Ollama();
     const messages: Message[] = [];
     const rl: Interface = readline.createInterface({
         input: process.stdin,
@@ -62,7 +61,7 @@ async function main(): Promise<void> {
 
             let contentOut: string = '';
             let message = { role: 'assistant', content: '' };
-            for await (const response of await client.chat({ model: 'mistral', messages: messages, stream: true })) {
+            for await (const response of await ollama.chat({ model: 'mistral', messages: messages, stream: true })) {
                 if (response.done) {
                     messages.push(message);
                 }

+ 2 - 3
examples/chat-stream/chat.ts

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

+ 2 - 3
examples/chat/chat.ts

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

+ 2 - 3
examples/generate-stream/generate.ts

@@ -1,8 +1,7 @@
-import { Ollama } from '../../src/index';
+import ollama from '../../src/index';
 
 async function main(): Promise<void> {
-    const client: Ollama = new Ollama();
-    const stream = await client.generate({model: "mistral", prompt: "Why is the sky blue?", stream: true});
+    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);
     }

+ 2 - 3
examples/generate/generate.ts

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

+ 39 - 0
examples/multimodal/multimodal.ts

@@ -0,0 +1,39 @@
+import https from 'https';
+import ollama from '../../src/index';
+
+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);
+        });
+    });
+}
+
+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();
+
+        // 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);
+    }
+}
+
+await main();