Create a useAi hook for Ollama 🤖

June 19, 2024

Ollama photo

We just added a new starter for using AI directly in your actions!

In this guide, we’ll walk through installing and configuring the OpenAI library, followed by interacting with it.

To use AI as a writing assistant for your notes, you can refer to this video to help you set up Ollama with Znote: 🎥 Ollama Znote integration


Install OLLAMA

Ollama download

ollama pull llama2

Install OpenAI NPM

This library can be used both for OpenAI and local LLMs

npm i -S openai@4.24.5

The useAI hook

Copy this function into the f(x) function editor then you will be ready to call the function anywhere in your notes.

async function useAI(prompt, data) {
  const {OpenAI} = require("openai/index.js");

const configuration = {
  baseURL: 'http://localhost:11434/v1', // ollama endpoint - comment for OpenAI
  apiKey: 'not-needed',
const openai = new OpenAI(configuration);

  const response = await{
  model: "llama2", //"gpt-3.5-turbo" or "gpt-4o" for OpenAI
  stream: true,
  messages: [
    // system, user, assistant, or function
    //{role: "system", content: prompt}, 
    {role: "user", content: `${prompt}: + ${data}`},

  // stream response
  for await (const chunk of response) {
    if (chunk.object && chunk.object.startsWith('chat.completion')) {
      if (chunk.choices.length > 0) {
        const text = chunk.choices[0]?.delta?.content || '';

Use it

Now we can test our new hook.

useAI("Makes a changelog for the given tasks" +
    " starting with a short summary before listing" +
    " the issues addressed", tasks);

Simple isn't it 😊? You can use the same code if you prefer to use OpenAI.