ollama-js icon indicating copy to clipboard operation
ollama-js copied to clipboard

Error while using Custom client

Open mallowhjs opened this issue 3 months ago • 5 comments

TypeError: itr.getReader is not a function node_modules/ollama/dist/utils.js:120:28)

My code : const ollama = new Ollama({ host: 'https://...' }) const ollamaResponse = await ollama.chat(apiMessage);

Called from NodeJs, but it looks like the code is using browser libraries. Do I miss something ? Any idea to solve that issue ?

mallowhjs avatar Mar 02 '24 05:03 mallowhjs

Try this code:

import ollama from 'ollama';
const message = {
  role: 'user',
  content: `Tell me a joke`,
};

const response = await ollama.chat({
  model: 'mistral:latest',
  messages: [message],
  stream: true,
});
for await (const part of response) {
  process.stdout.write(part.message.content);
}

I use this in nodeJS, it works. Your package.json file must contain this also "type": "module"

Spankyzone avatar Mar 06 '24 09:03 Spankyzone

I've installed Ollama on my little server and I'm running Mistral. Everything works fine, I can chat via cmdline interface, and also API requests works ok, except streaming response. I try to get streaming response working by using your library, but I got stuck using it.

I have this simple script to talk to my local Ollama instance:

import {Ollama} from 'ollama'

async function run() {
    const ollama = new Ollama({host: 'http://xxxx.duckdns.org:11434'})
    
    const response = await ollama.generate({
        model: 'mistral',
        prompt: "Write a C code that parses a string into words that are divided by spaces.",
        stream: true
    });
    
    console.log(response);

    for await(const part of response) {
        process.stdout.write(part.message.content);
    }
}

run();

I get the following error:

{
  next: [Function (anonymous)],
  throw: [Function (anonymous)],
  return: [Function (anonymous)],
  [Symbol(Symbol.asyncIterator)]: [Function (anonymous)]
}
file:///Users/nordin/development/nodejs/ollama-req/main.js:15
        process.stdout.write(part.message.content);
                                          ^

TypeError: Cannot read properties of undefined (reading 'content')
    at run (file:///Users/nordin/development/nodejs/ollama-req/main.js:15:43)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I hope you can pinpoint the problem, as it's relatively simple piece of script.

bouchtaoui-dev avatar Mar 06 '24 17:03 bouchtaoui-dev

I have the same problem when I used ollama in the browser. I think you need to call next() func in returned object. Try this:

const responseNext = await response.next()
for await(const part of responseNext) {
    process.stdout.write(part.message.content);
}

Spankyzone avatar Mar 06 '24 21:03 Spankyzone

You helped me to take a look at the iterator I wasn't familiar with. The following snippet should give you a working result:

    for await (const part of response) {
        console.log(part);
    }

From here on you're good to go 😉

bouchtaoui-dev avatar Mar 09 '24 18:03 bouchtaoui-dev

太优秀了

chergn avatar Apr 19 '24 10:04 chergn