Go Semantic Router is a superfast decision-making layer for your LLMs and agents written in pure Go .
Rather than waiting for slow LLM generations to make tool-use decisions, use the magic of semantic vector space to make those decisions — routing requests using configurable semantic meaning.
A pure-go package for abstractly computing similarity scores between a query vector embedding and a set of vector embeddings.
go get github.com/conneroisu/go-semantic-router
import "github.com/conneroisu/go-semantic-router/encoders/openai"
import "github.com/conneroisu/go-semantic-router/encoders/google"
import "github.com/conneroisu/go-semantic-router/encoders/ollama"
// Package main shows how to use the semantic router to find the best route for a given utterance
// in the context of a chat bot or other conversational application.
package main
import (
"context"
"fmt"
"os"
semantic_router "github.com/conneroisu/go-semantic-router"
"github.com/conneroisu/go-semantic-router/domain"
encoders "github.com/conneroisu/go-semantic-router/encoders/openai"
"github.com/conneroisu/go-semantic-router/stores/memory"
"github.com/sashabaranov/go-openai"
)
// PoliticsRoutes represents a set of routes that are noteworthy.
var PoliticsRoutes = semantic_router.Route{
Name: "politics",
Utterances: []domain.Utterance{
{Utterance: "isn't politics the best thing ever"},
{Utterance: "why don't you tell me about your political opinions"},
{Utterance: "don't you just love the president"},
{Utterance: "they're going to destroy this country!"},
{Utterance: "they will save the country!"},
},
}
// ChitchatRoutes represents a set of routes that are noteworthy.
var ChitchatRoutes = semantic_router.Route{
Name: "chitchat",
Utterances: []domain.Utterance{
{Utterance: "how's the weather today?"},
{Utterance: "how are things going?"},
{Utterance: "lovely weather today"},
{Utterance: "the weather is horrendous"},
{Utterance: "let's go to the chippy"},
},
}
// main runs the example.
func main() {
if err := run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// run runs the example.
func run() error {
ctx := context.Background()
store := memory.NewStore()
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
router, err := semantic_router.NewRouter(
[]semantic_router.Route{
PoliticsRoutes,
ChitchatRoutes,
},
encoders.Encoder{
Client: client,
},
store,
)
if err != nil {
return fmt.Errorf("error creating router: %w", err)
}
finding, p, err := router.Match(ctx, "how's the weather today?")
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("p:", p)
fmt.Println("Found:", finding)
return nil
}
Output:
Found: chitchat
The following example shows how to use the semantic router to find the best route for a given utterance in the context of a veterinarian appointment.
The goal of the example is to decide whether spoken utterances are relevant to a noteworthy conversation or a chitchat conversation.
// Package main shows how to use the semantic router to find the best route for a given utterance
// in the context of a veterinarian appointment.
package main
import (
"context"
"fmt"
"os"
semantic_router "github.com/conneroisu/go-semantic-router"
"github.com/conneroisu/go-semantic-router/domain"
"github.com/conneroisu/go-semantic-router/encoders/ollama"
"github.com/conneroisu/go-semantic-router/stores/memory"
"github.com/ollama/ollama/api"
)
// NoteworthyRoutes represents a set of routes that are noteworthy.
// noteworthy here means that the routes are likely to be relevant to a noteworthy conversation in a veterinarian appointment.
var NoteworthyRoutes = semantic_router.Route{
Name: "noteworthy",
Utterances: []domain.Utterance{
{Utterance: "what is the best way to treat a dog with a cold?"},
{Utterance: "my cat has been limping, what should I do?"},
},
}
// ChitchatRoutes represents a set of routes that are chitchat.
// chitchat here means that the routes are likely to be relevant to a chitchat conversation in a veterinarian appointment.
var ChitchatRoutes = semantic_router.Route{
Name: "chitchat",
Utterances: []domain.Utterance{
{Utterance: "what is your favorite color?"},
{Utterance: "what is your favorite animal?"},
},
}
// main runs the example.
func main() {
if err := run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// run runs the example.
func run() error {
ctx := context.Background()
cli, err := api.ClientFromEnvironment()
if err != nil {
return fmt.Errorf("error creating client: %w", err)
}
router, err := semantic_router.NewRouter(
[]semantic_router.Route{NoteworthyRoutes, ChitchatRoutes},
&ollama.Encoder{
Client: cli,
Model: "mxbai-embed-large",
},
memory.NewStore(),
)
if err != nil {
return fmt.Errorf("error creating router: %w", err)
}
finding, p, err := router.Match(ctx, "how's the weather today?")
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println("Found:", finding)
fmt.Println("p:", p)
return nil
}
The output of the veterinarian example is:
Found: chitchat
p: 0.4656368810166642