joe, So far this week, we have looked at how to use Ollama from the CLI, how to use Ollama from the web service, and how to use Ollama from a phone or iPad. Today we are going to be using the Ollama JavaScript Library to write an application.
Install the Ollama Library
The first step is to run
npm i ollama
from the terminal.That installs Ollama as a dependency in package.json.
Basic CLI example
At this point, we can start writing code. When we used the web service earlier this week, we used the generate endpoint and provided
model
,prompt
, andstream
as parameters. We set thestream
parameter to false so that it would return a single response object instead of a stream of objects. When using the javascript library, thestream
parameter isn’t necessary because it returns a single response object by default. We still provide it with amodel
and aprompt
, though.If you run it from the terminal, the response will look familiar.
Basic Web Application Example
The output is very similar to the node-fetch example from earlier this week. Last week, when we looked at how to dockerize a node app, we output an array as an unordered list. Let’s see if we can replicate that result using the output from Ollama.
If you
npm install express
to install express, you can host a simple HTTP page at port 8080 and with the magic of JSON.parse() and a for loop, you can build your unordered list.So, what does the output look like?
Every time you load the page, it makes a server-side API call to Ollama, gets a list of large cities in Wisconsin, and displays them on the website. The list is never the same (because of hallucinations) but that is another issue.
Have any questions, comments, etc? Please feel free to drop a comment, below.
https://jws.news/2024/how-to-write-a-javascript-app-that-uses-ollama/
Add comment