Skip to main content

Quickstart: LangChainGo with Ollama

Get started with running your first program using LangChainGo and Ollama. Ollama provides the most straightforward method for local LLM inference across all computer platforms.

Pre-requisites

  1. Ollama: Download and install Ollama.
  2. Go: Download and install Go.

Steps

  1. Initialize Ollama: In your terminal, execute the command $ ollama run llama2. The first run might take some time as the model needs to be fetched to your computer.
  2. Run the Example: Enter the command:
    go run github.com/tmc/langchaingo/examples/ollama-completion-example@main

You should receive (something like) the following output:

The first human to set foot on the moon was Neil Armstrong, an American astronaut, who stepped onto the lunar surface during the Apollo 11 mission on July 20, 1969.

Congratulations! You've successfully built and executed your first open-source LLM-based program using local inference.

Here is the entire program (from ollama-completion-example).

package main

import (
"context"
"fmt"
"log"

"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/ollama"
)

func main() {
llm, err := ollama.New(ollama.WithModel("llama2"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
completion, err := llm.Call(ctx, "Human: Who was the first man to walk on the moon?\nAssistant:",
llms.WithTemperature(0.8),
llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
fmt.Print(string(chunk))
return nil
}),
)
if err != nil {
log.Fatal(err)
}

_ = completion
}