Skip to content

Instantly share code, notes, and snippets.

@halr9000
Last active September 24, 2024 21:46
Show Gist options
  • Save halr9000/e95618baaee2ee25c5e1ffbc66dba98d to your computer and use it in GitHub Desktop.
Save halr9000/e95618baaee2ee25c5e1ffbc66dba98d to your computer and use it in GitHub Desktop.
Ollama AI / LLM Utility Functions for PowerShell

Utility functions for use with Ollama in PowerShell

Want to run AI Large Language Models (LLM) locally? One great way is with Ollama, which can host many state of the art LLMs. Ollama is a command-line utility (CLI) that can be used to download and manage the model files (which are often multiple GB), perform the actual LLM inference, and provide a REST API to expose the LLMs to other applications on your system. Ollama is quick to install.

This repo contains functions for PowerShell. You can dot source this file to make the functions available in your current PowerShell session, or add it to your profile to make them available for every session. This was developed on Windows, but should work as-is on a Linux or MacOS system with pwsh installed.

Setup (Windows)

Install Ollama

Use Winget or UniGetUI to iunstall Ollama.

winget install --id=ollama.ollama

Prepare Ollama and download models

Once setup has completed, you should have an Ollama shortcut in your start menu. You can use that if you would like for Ollama to run as a service, which makes its API available to other applications besides these scripts. (Recommended.) Follow the Ollama Quickstart page to "pull" (download) a model. The functions specify Llama 3.1 model, but there are many others in their library. The model can be easily changed by modifying the functions. Here is an example command to donwload Llama 3.1:

ollama pull llama3.1

Make functions available to PowerShell

. .\ollama-helpers.ps1

You can use Get-Command to ensure that the functions are available:

PS> Get-Command ask

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        ask

Usage

Simply type ask or New-ImagePrompt, followed by instructions for the LLM, enclosed in single or double quotes.

PS C:\Users\hal> ask 'what is your favorite color?'
I don't have a personal preference for colors, nor do I have personal experiences or emotions. I'm designed to provide
information and assist with tasks, but I don't have subjective experiences like humans do.

That being said, I can tell you about different colors, their meanings, and associations in various cultures if that's helpful!
Would you like to know something specific about a particular color?

PS C:\Users\hal> New-ImagePrompt "simple triangle"
A delicate, hand-drawn simple triangle in the foreground, slightly elevated on a worn, wooden pedestal, about 1/3 from the
bottom of the frame and 2/5 from the left edge, with its base aligned almost perfectly parallel to the horizon line. In the
background, a blurred image of a bustling cityscape is visible, perhaps a faded photograph or a watercolor painting, featuring
a mix of modern skyscrapers and historic buildings about 3/4 from the top and 1/2 from the right edge. A few subtle hints of
natural light creep through the city's architecture, illuminating the triangle with a warm, golden glow. The pedestal itself is
weathered to a soft grayish-brown color, while the triangle's lines are rendered in a bold, expressive black ink, contrasting
against a soft blue-white background that gradually fades into the subtle colors of the cityscape. The overall style is
reminiscent of late 19th-century Art Nouveau, with its emphasis on sinuous curves and fluid line work. The palette is
predominantly cool-toned, with touches of warmth from the golden light and the wood's patina, evoking a sense of contemplation
and quiet reflection, set against the vibrant, dynamic energy of the city behind.
function ask {
param(
[Parameter(ValueFromPipeline = $true)]
[string[]]$Prompt
)
if ($null -eq $Prompt -or $Prompt.Length -eq 0) {
throw "Prompt cannot be null or empty."
}
ollama run llama3.1 $Prompt
}
function New-ImagePrompt {
param(
[Parameter(ValueFromPipeline = $true)]
[string[]]$Prompt
)
# Base prompt to guide image creation
$basePrompt = "You are a cinemetographer, photographer, and art critic. Create an image prompt for the subject which follows these instructions. Add details to the prompt to describe the foreground, as well as items in the background. Use relative terms for location of these elements. Begin with the subject, and end with the art style or period, media, color palette, mood, lighting. Include time or season for outdoors images. Do not use very flowery or overly verbose language, but do expand on the subject to produce a unique, compelling, creative, and surprising image. Use comma separated phrases and concepts, rather than complete sentences. Do not output any prefix or acknowlegements, do not include section headers. Only output the image prompt in one paragraph. The subject is: "
if ($null -eq $Prompt -or $Prompt.Length -eq 0) {
throw "Prompt cannot be null or empty."
}
# Generating the prompt using ollama run
ollama run llama3.1 ($basePrompt + $Prompt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment