From 135802ab62984d1e233d95b3755eef1808994a82 Mon Sep 17 00:00:00 2001 From: Kevin Cheung Date: Fri, 12 Jul 2024 15:42:34 -0700 Subject: [PATCH] Ollama Go plugin docs (#599) --- docs-go/Makefile | 3 +- docs-go/plugins/ollama.md | 70 ++++++++++++++++++++++++++++++ docs-go/plugins/ollama.src | 44 +++++++++++++++++++ go/internal/doc-snippets/ollama.go | 60 +++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 docs-go/plugins/ollama.md create mode 100644 docs-go/plugins/ollama.src create mode 100644 go/internal/doc-snippets/ollama.go diff --git a/docs-go/Makefile b/docs-go/Makefile index 78b828244..664d86515 100644 --- a/docs-go/Makefile +++ b/docs-go/Makefile @@ -1,7 +1,8 @@ WEAVE=$(HOME)/go/bin/weave all: $(WEAVE) get-started-go.md flows.md models.md prompts.md dotprompt.md pgvector.md \ - cloud-run.md rag.md + cloud-run.md rag.md \ + plugins/ollama.md $(WEAVE): ../go/internal/cmd/weave/*.go go -C ../go install ./internal/cmd/weave diff --git a/docs-go/plugins/ollama.md b/docs-go/plugins/ollama.md new file mode 100644 index 000000000..97683c9c9 --- /dev/null +++ b/docs-go/plugins/ollama.md @@ -0,0 +1,70 @@ + + +# Ollama plugin + +The Ollama plugin provides interfaces to any of the local LLMs supported by +[Ollama](https://ollama.com/). + +## Prerequisites + +This plugin requires that you first install and run the Ollama server. You can +follow the instructions on the [Download Ollama](https://ollama.com/download) +page. + +Use the Ollama CLI to download the models you are interested in. For example: + +```posix-terminal +ollama pull gemma2 +``` + +For development, you can run Ollama on your development machine. Deployed apps +usually run Ollama on a different, GPU-accelerated, machine from the app backend +that runs Genkit. + +## Configuration + +To use this plugin, call `ollama.Init()`, specifying the address of your Ollama +server: + +```go +import "github.com/firebase/genkit/go/plugins/ollama" +``` + +```go +// Init with Ollama's default local address. +if err := ollama.Init(ctx, "http://127.0.0.1:11434"); err != nil { + return err +} +``` + +## Usage + +To generate content, you first need to create a model definition based on the +model you installed and want to use. For example, if you installed Gemma 2: + +```go +model := ollama.DefineModel( + ollama.ModelDefinition{ + Name: "gemma2", + Type: "chat", // "chat" or "generate" + }, + &ai.ModelCapabilities{ + Multiturn: true, + SystemRole: true, + Tools: false, + Media: false, + }, +) +``` + +Then, you can use the model reference to send requests to your Ollama server: + +```go +genRes, err := model.Generate(ctx, ai.NewGenerateRequest( + nil, ai.NewUserTextMessage("Tell me a joke.")), nil) +if err != nil { + return err +} +``` + +See [Generating content](models.md) for more information. diff --git a/docs-go/plugins/ollama.src b/docs-go/plugins/ollama.src new file mode 100644 index 000000000..00770d1a7 --- /dev/null +++ b/docs-go/plugins/ollama.src @@ -0,0 +1,44 @@ +# Ollama plugin + +The Ollama plugin provides interfaces to any of the local LLMs supported by +[Ollama](https://ollama.com/). + +## Prerequisites + +This plugin requires that you first install and run the Ollama server. You can +follow the instructions on the [Download Ollama](https://ollama.com/download) +page. + +Use the Ollama CLI to download the models you are interested in. For example: + +```posix-terminal +ollama pull gemma2 +``` + +For development, you can run Ollama on your development machine. Deployed apps +usually run Ollama on a different, GPU-accelerated, machine from the app backend +that runs Genkit. + +## Configuration + +To use this plugin, call `ollama.Init()`, specifying the address of your Ollama +server: + +```go +import "github.com/firebase/genkit/go/plugins/ollama" +``` + +%include ../go/internal/doc-snippets/ollama.go init + +## Usage + +To generate content, you first need to create a model definition based on the +model you installed and want to use. For example, if you installed Gemma 2: + +%include ../go/internal/doc-snippets/ollama.go definemodel + +Then, you can use the model reference to send requests to your Ollama server: + +%include ../go/internal/doc-snippets/ollama.go gen + +See [Generating content](models.md) for more information. diff --git a/go/internal/doc-snippets/ollama.go b/go/internal/doc-snippets/ollama.go new file mode 100644 index 000000000..8cbaef3fe --- /dev/null +++ b/go/internal/doc-snippets/ollama.go @@ -0,0 +1,60 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package snippets + +import ( + "context" + + "github.com/firebase/genkit/go/ai" + "github.com/firebase/genkit/go/plugins/ollama" +) + +func ollamaEx(ctx context.Context) error { + var err error + + //!+init + // Init with Ollama's default local address. + if err := ollama.Init(ctx, "http://127.0.0.1:11434"); err != nil { + return err + } + //!-init + + //!+definemodel + model := ollama.DefineModel( + ollama.ModelDefinition{ + Name: "gemma2", + Type: "chat", // "chat" or "generate" + }, + &ai.ModelCapabilities{ + Multiturn: true, + SystemRole: true, + Tools: false, + Media: false, + }, + ) + //!-definemodel + + //!+gen + genRes, err := model.Generate(ctx, ai.NewGenerateRequest( + nil, ai.NewUserTextMessage("Tell me a joke.")), nil) + if err != nil { + return err + } + //!-gen + + _ = genRes + + return nil +}