gobindings

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 7 Imported by: 0

README

Golang Melody

Parsing

import "github.com/cohere-ai/melody"

textChunks := []string{
    "<|START_THINKING|>", "This", " is", " a", " rainbow", " <", "co", ">", "emoji", ":", " 🌈",
    "</", "co", ":", " ", "0", ":[", "1", "]>", "<|END_THINKING|>", "\n", "<|START_RESPONSE|>",
    "foo", " <", "co", ">", "bar", "</", "co", ":", " ", "0", ":[", "1", ",", "2", "],", "1",
    ":[", "3", ",", "4", "]>", "<|END_RESPONSE|>"
}

// Create a filter with options using the builder pattern
f := melody.NewFilter(melody.HandleMultiHopCmd3(), melody.StreamToolActions())

// Process tokens synchronously
var fullContent, fullReasoning string
var allCitations []melody.FilterCitation
for _, chunk := range textChunks {
    result, err := f.WriteDecoded(chunk)
    if err != nil {
        panic(err)
    }
    if result == nil {
        continue
    }
    if result.Content != nil {
        fullContent += *result.Content
    }
    if result.Reasoning != nil {
        fullReasoning += *result.Reasoning
    }
    allCitations = append(allCitations, result.Citations...)
}

// Flush any remaining partial outputs
flushResult, err := f.FlushPartials()
if err != nil {
    panic(err)
}
if flushResult != nil {
    if flushResult.Content != nil {
        fullContent += *flushResult.Content
    }
    if flushResult.Reasoning != nil {
        fullReasoning += *flushResult.Reasoning
    }
    allCitations = append(allCitations, flushResult.Citations...)
}

/*
Expected output:
fullContent = "foo bar"
fullReasoning = "This is a rainbow emoji: 🌈"
allCitations = []melody.FilterCitation{
    {StartIndex: 18, EndIndex: 26, Text: "emoji: 🌈",
     Sources: []melody.Source{{ToolCallIndex: 0, ToolResultIndices: []uint{1}}},
     IsThinking: true},
    {StartIndex: 4, EndIndex: 7, Text: "bar",
     Sources: []melody.Source{
         {ToolCallIndex: 0, ToolResultIndices: []uint{1, 2}},
         {ToolCallIndex: 1, ToolResultIndices: []uint{3, 4}},
     },
     IsThinking: false},
}
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderCMD3

func RenderCMD3(opts RenderCmd3Options) (string, error)

RenderCMD3 renders CMD3 using the Rust templating engine via FFI.

func RenderCMD4

func RenderCMD4(opts RenderCmd4Options) (string, error)

RenderCMD4 renders CMD4 using the Rust templating engine via FFI.

Types

type AccumulatedToolCall added in v0.6.0

type AccumulatedToolCall struct {
	Index           uint
	ID              string
	Name            string
	Arguments       string
	ProcessedParams []FilterToolParameter
}

AccumulatedToolCall represents a tool call (possibly partial in streaming)

type AggregatedResult added in v0.6.0

type AggregatedResult struct {
	Content       *string
	Reasoning     *string
	ToolCalls     []AccumulatedToolCall
	Citations     []FilterCitation
	SearchQueries []SearchQueryDelta
}

AggregatedResult is the result of a write_decoded or flush_partials call

type CitationQuality

type CitationQuality int32
const (
	CitationQualityUnknown CitationQuality = 0
	CitationQualityOff     CitationQuality = 1
	CitationQualityOn      CitationQuality = 2
)

func (*CitationQuality) UnmarshalJSON

func (q *CitationQuality) UnmarshalJSON(data []byte) error

type Content

type Content struct {
	Type      ContentType        `json:"type"`
	Text      string             `json:"text,omitempty"`     // optional: empty means omitted
	Thinking  string             `json:"thinking,omitempty"` // optional: empty means omitted
	Image     *Image             `json:"image,omitempty"`    // optional
	Document  orderedjson.Object `json:"document,omitempty"`
	Multipart []Content          `json:"multipart,omitempty"` // optional
}

type ContentType

type ContentType int32
const (
	ContentUnknown   ContentType = 0
	ContentText      ContentType = 1
	ContentThinking  ContentType = 2
	ContentImage     ContentType = 3
	ContentDocument  ContentType = 4
	ContentMultipart ContentType = 5
)

func (*ContentType) UnmarshalJSON

func (t *ContentType) UnmarshalJSON(data []byte) error

type Filter

type Filter interface {
	// WriteDecoded writes a decoded token string to the filter
	WriteDecoded(decodedToken string) (*AggregatedResult, error)

	// FlushPartials flushes any partial outputs
	FlushPartials() (*AggregatedResult, error)
}

Filter is the interface used to parse the output of a cohere model

func NewFilter

func NewFilter(options ...FilterOption) Filter

NewFilter creates a new synchronous filter

type FilterCitation

type FilterCitation struct {
	// The beginning index of the citation in the larger generation.
	// E.g. "Hello world" where the citation is "world" would have a StartIndex of 6.
	StartIndex uint `json:"start_index"`
	// The end index of the citation in the larger generation.
	// E.g. "Hello world" where the citation is "world" would have an EndIndex of 10.
	EndIndex   uint     `json:"end_index"`
	Text       string   `json:"text"`
	Sources    []Source `json:"sources"`
	IsThinking bool     `json:"is_thinking"`
}

FilterCitation represents a citation parsed from a model generation

type FilterOption

type FilterOption func(*filterConfig)

FilterOption is a function that configures a filter

func HandleMultiHop

func HandleMultiHop() FilterOption

HandleMultiHop configures the filter to handle multi-hop format

func HandleMultiHopCmd3

func HandleMultiHopCmd3() FilterOption

HandleMultiHopCmd3 configures the filter to handle multi-hop CMD3 format

func HandleMultiHopCmd4

func HandleMultiHopCmd4() FilterOption

HandleMultiHopCmd4 configures the filter to handle multi-hop CMD4 format

func HandleRAG

func HandleRAG() FilterOption

HandleRAG configures the filter to handle RAG (Retrieval Augmented Generation) format

func HandleSearchQuery

func HandleSearchQuery() FilterOption

HandleSearchQuery configures the filter to handle search query format

func RemoveToken

func RemoveToken(token string) FilterOption

RemoveToken removes a specific token from the output

func StreamNonGroundedAnswer

func StreamNonGroundedAnswer() FilterOption

StreamNonGroundedAnswer enables streaming of non-grounded answer

func StreamProcessedParams

func StreamProcessedParams() FilterOption

StreamProcessedParams enables streaming of processed parameters

func StreamToolActions

func StreamToolActions() FilterOption

StreamToolActions enables streaming of tool actions

func WithChunkSize

func WithChunkSize(size int) FilterOption

WithChunkSize sets the chunk size

func WithExclusiveStops

func WithExclusiveStops(stops []string) FilterOption

WithExclusiveStops sets exclusive stop sequences

func WithInclusiveStops

func WithInclusiveStops(stops []string) FilterOption

WithInclusiveStops sets inclusive stop sequences

func WithLeftTrimmed

func WithLeftTrimmed() FilterOption

WithLeftTrimmed enables left trimming

func WithRightTrimmed

func WithRightTrimmed() FilterOption

WithRightTrimmed enables right trimming

type FilterOptions

type FilterOptions struct {
	// contains filtered or unexported fields
}

FilterOptions is the Go wrapper around CFilterOptions

func NewFilterOptions

func NewFilterOptions() *FilterOptions

NewFilterOptions creates a new FilterOptions instance

func (*FilterOptions) Cmd3

func (opts *FilterOptions) Cmd3() *FilterOptions

Cmd3 configures options for multi-hop CMD3 format

func (*FilterOptions) Cmd4

func (opts *FilterOptions) Cmd4() *FilterOptions

HandleMultiHopCmd4 configures options for multi-hop CMD4 format

func (*FilterOptions) Free

func (opts *FilterOptions) Free()

Free releases the FilterOptions resources

func (*FilterOptions) HandleMultiHop

func (opts *FilterOptions) HandleMultiHop() *FilterOptions

HandleMultiHop configures options for multi-hop format

func (*FilterOptions) HandleRAG

func (opts *FilterOptions) HandleRAG() *FilterOptions

HandleRAG configures options for RAG format

func (*FilterOptions) HandleSearchQuery

func (opts *FilterOptions) HandleSearchQuery() *FilterOptions

HandleSearchQuery configures options for search query handling

func (*FilterOptions) RemoveToken

func (opts *FilterOptions) RemoveToken(token string) *FilterOptions

RemoveToken removes a specific token from the output

func (*FilterOptions) StreamNonGroundedAnswer

func (opts *FilterOptions) StreamNonGroundedAnswer() *FilterOptions

StreamNonGroundedAnswer enables streaming of non-grounded answer

func (*FilterOptions) StreamProcessedParams

func (opts *FilterOptions) StreamProcessedParams() *FilterOptions

StreamProcessedParams enables streaming of processed parameters

func (*FilterOptions) StreamToolActions

func (opts *FilterOptions) StreamToolActions() *FilterOptions

StreamToolActions enables streaming of tool actions

func (*FilterOptions) WithChunkSize

func (opts *FilterOptions) WithChunkSize(size int) *FilterOptions

WithChunkSize sets the chunk size

func (*FilterOptions) WithExclusiveStops

func (opts *FilterOptions) WithExclusiveStops(stops []string) *FilterOptions

WithExclusiveStops sets exclusive stop sequences

func (*FilterOptions) WithInclusiveStops

func (opts *FilterOptions) WithInclusiveStops(stops []string) *FilterOptions

WithInclusiveStops sets inclusive stop sequences

func (*FilterOptions) WithLeftTrimmed

func (opts *FilterOptions) WithLeftTrimmed() *FilterOptions

WithLeftTrimmed enables left trimming

func (*FilterOptions) WithRightTrimmed

func (opts *FilterOptions) WithRightTrimmed() *FilterOptions

WithRightTrimmed enables right trimming

type FilterToolParameter

type FilterToolParameter struct {
	Name       string
	ValueDelta string
}

FilterToolParameter represents a change to a tool parameter

type Grounding

type Grounding int32
const (
	GroundingUnknown  Grounding = 0
	GroundingEnabled  Grounding = 1
	GroundingDisabled Grounding = 2
)

func (*Grounding) UnmarshalJSON

func (g *Grounding) UnmarshalJSON(data []byte) error

type Image

type Image struct {
	TemplatePlaceholder string `json:"template_placeholder"`
}

type Message

type Message struct {
	Role       Role             `json:"role"`
	Content    []Content        `json:"content"`
	ToolCalls  []ToolCall       `json:"tool_calls,omitempty"`
	ToolCallID string           `json:"tool_call_id,omitempty"` // optional: empty means omitted
	Citations  []FilterCitation `json:"citations,omitempty"`
}

type ReasoningType

type ReasoningType int32
const (
	ReasoningTypeUnknown  ReasoningType = 0
	ReasoningTypeEnabled  ReasoningType = 1
	ReasoningTypeDisabled ReasoningType = 2
)

func (*ReasoningType) UnmarshalJSON

func (rt *ReasoningType) UnmarshalJSON(data []byte) error

type RenderCmd3Options

type RenderCmd3Options struct {
	Messages                 []Message            `json:"messages"`
	TemplateID               *string              `json:"template_id,omitempty"`
	Template                 string               `json:"template"`
	TemplateJinja            string               `json:"template_jinja"`
	UseJinja                 bool                 `json:"use_jinja"`
	DevInstruction           *string              `json:"dev_instruction,omitempty"`
	Documents                []orderedjson.Object `json:"documents,omitempty"` // JSON objects
	AvailableTools           []Tool               `json:"available_tools,omitempty"`
	SafetyMode               *SafetyMode          `json:"safety_mode,omitempty"`      // optional
	CitationQuality          *CitationQuality     `json:"citation_quality,omitempty"` // optional
	ReasoningType            *ReasoningType       `json:"reasoning_type,omitempty"`   // optional
	SkipPreamble             bool                 `json:"skip_preamble,omitempty"`
	ResponsePrefix           *string              `json:"response_prefix,omitempty"`
	JSONSchema               *string              `json:"json_schema,omitempty"`
	JSONMode                 bool                 `json:"json_mode,omitempty"`
	AdditionalTemplateFields map[string]any       `json:"additional_template_fields,omitempty"` // optional: JSON-encoded
	EscapedSpecialTokens     map[string]string    `json:"escaped_special_tokens,omitempty"`     // optional: JSON-encoded
}

type RenderCmd4Options

type RenderCmd4Options struct {
	Messages                 []Message            `json:"messages"`
	TemplateID               *string              `json:"template_id,omitempty"`
	Template                 string               `json:"template"`
	TemplateJinja            string               `json:"template_jinja"`
	UseJinja                 bool                 `json:"use_jinja"`
	DevInstruction           *string              `json:"dev_instruction,omitempty"`
	PlatformInstruction      *string              `json:"platform_instruction,omitempty"`
	Documents                []orderedjson.Object `json:"documents,omitempty"`
	AvailableTools           []Tool               `json:"available_tools,omitempty"`
	Grounding                *Grounding           `json:"grounding,omitempty"` // optional
	ResponsePrefix           *string              `json:"response_prefix,omitempty"`
	JSONSchema               *string              `json:"json_schema,omitempty"`
	JSONMode                 bool                 `json:"json_mode,omitempty"`
	AdditionalTemplateFields map[string]any       `json:"additional_template_fields,omitempty"` // optional
	EscapedSpecialTokens     map[string]string    `json:"escaped_special_tokens,omitempty"`     // optional
}

type Role

type Role int32

Templating enums (mirror ffi.rs C enums)

const (
	RoleUnknown Role = 0
	RoleSystem  Role = 1
	RoleUser    Role = 2
	RoleChatbot Role = 3
	RoleTool    Role = 4
)

func (*Role) UnmarshalJSON

func (r *Role) UnmarshalJSON(data []byte) error

type SafetyMode

type SafetyMode int32
const (
	SafetyModeUnknown    SafetyMode = 0
	SafetyModeNone       SafetyMode = 1
	SafetyModeStrict     SafetyMode = 2
	SafetyModeContextual SafetyMode = 3
)

func (*SafetyMode) UnmarshalJSON

func (s *SafetyMode) UnmarshalJSON(data []byte) error

type SearchQueryDelta added in v0.6.0

type SearchQueryDelta struct {
	Index uint
	Text  string
}

SearchQueryDelta represents a search query update

type Source

type Source struct {
	ToolCallIndex     uint   `json:"tool_call_index"`
	ToolResultIndices []uint `json:"tool_result_indices"`
}

Source indicates which tool call and which tool results from that tool are being cited

type SyncFilter

type SyncFilter struct {
	// contains filtered or unexported fields
}

SyncFilter is a synchronous filter implementation

func (*SyncFilter) FlushPartials

func (f *SyncFilter) FlushPartials() (*AggregatedResult, error)

FlushPartials flushes any partial outputs

func (*SyncFilter) WriteDecoded

func (f *SyncFilter) WriteDecoded(decodedToken string) (*AggregatedResult, error)

WriteDecoded writes a decoded token string to the filter

type Tool

type Tool struct {
	Name        string             `json:"name"`
	Description string             `json:"description,omitempty"`
	Parameters  orderedjson.Object `json:"parameters,omitempty"`
}

Templating Go-side types

type ToolCall

type ToolCall struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Parameters string `json:"parameters,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL