Skip to content

Call a language model

Use llm.Model directly in a Go application or call it inside a graph node. A provider connects the model to a particular service. The message package carries the same conversation types across providers.

Send your first message

This example uses OpenAI. Set OPENAI_API_KEY, choose a model available to your account, and install the imports with go mod tidy after saving the program as main.go in a Go module. For another backend, follow Choose a provider.

package main
import (
"context"
"fmt"
"log"
"github.com/masterkeysrd/loom/llm"
"github.com/masterkeysrd/loom/message"
loomopenai "github.com/masterkeysrd/loom/providers/openai"
)
func main() {
provider, err := loomopenai.NewDefaultProvider()
if err != nil {
log.Fatal(err)
}
model, err := llm.NewModel(provider, "gpt-4o", nil)
if err != nil {
log.Fatal(err)
}
messages := []message.Message{message.NewUserText("Say hello in Spanish.")}
reply, err := model.Invoke(context.Background(), messages)
if err != nil {
log.Fatal(err)
}
fmt.Println(reply.GetContent().Text())
}

Run go run . to print the model’s reply. To continue the conversation, append the assistant reply and the next user message to your history. See Conversations and messages for roles and multimodal input.

Explore the model API