Quick Start
Let's use rModel
to build a Brain
as shown below.

Defining a Brainprint
Define the graph's topology by outlining a brainprint (a shorthand for brain blueprint).
1. Create a brainprint
1bp := rModel.NewBrainPrint()
2. Add Neuron
s
Bind a processing function to a neuron or custom Processor
. In this example, a function is bound, and its definition is omitted for brevity. For more details, see examples/chat_agent_with_function_calling.
1// add neuron with function
2bp.AddNeuron("llm", chatLLM)
3bp.AddNeuron("action", callTools)
3. Add Link
s
There are three types of Link
s:
- Normal Links: Include the
source Neuron
anddestination Neuron
- Entry Links: Only have the
destination Neuron
- End Links: The
Brain
will automatically go into aSleeping
state when there are no activeNeuron
s andLink
s, but you can also explicitly define end links to set an endpoint for theBrain
to run. You only need to specify thesource Neuron
, thedestination Neuron
will be END
1/* This example omits error handling */
2// add entry link
3_, _ = bp.AddEntryLink("llm")
4
5// add link
6continueLink, _ := bp.AddLink("llm", "action")
7_, _ = bp.AddLink("action", "llm")
8
9// add end link
10endLink, _ := bp.AddEndLink("llm")
4. Set cast select at a branch
By default, all outbound links of a Neuron
will propagate (belonging to the default casting group). To set up branch selections where you only want certain links to propagate, define casting groups (CastGroup) along with a casting selection function (CastGroupSelectFunc). Each cast group contains a set of links, and the return string of the cast group selection function determines which cast group to propagate to.
1// add link to cast group of a neuron
2_ = bp.AddLinkToCastGroup("llm", "continue", continueLink)
3_ = bp.AddLinkToCastGroup("llm", "end", endLink)
4// bind cast group select function for neuron
5_ = bp.BindCastGroupSelectFunc("llm", llmNext)
1func llmNext(b rModel.BrainRuntime) string {
2 if !b.ExistMemory("messages") {
3 return "end"
4 }
5 messages, _ := b.GetMemory("messages").([]openai.ChatCompletionMessage)
6 lastMsg := messages[len(messages)-1]
7 if len(lastMsg.ToolCalls) == 0 { // no need to call any tools
8 return "end"
9 }
10
11 return "continue"
12}
Building a Brain
from a Brainprint
Build with various withOpts parameters, although it can be done without configuring any, similar to the example below, using default construction parameters.
1brain := bp.Build()
Running the Brain
As long as any Link
or Neuron
of Brain
is activated, it is considered to be running.
The Brain
can only be triggered to run through Link
s. You can set initial brain memory Memory
before the Brain
runs to store some initial context, but this is an optional step. The following methods are used to trigger Link
s:
- Use
brain.Entry()
to trigger all entry links. - Use
brain.EntryWithMemory()
to set initialMemory
and trigger all entry links. - Use
brain.TrigLinks()
to trigger specificLinks
. - You can also use
brain.SetMemory()
+brain.TrigLinks()
to set initialMemory
and trigger specificLinks
.
⚠️Note: Once a Link
is triggered, the program is non-block; the operation of the Brain
is asynchronous.
1// import "github.com/sashabaranov/go-openai" // just for message struct
2
3// set memory and trigger all entry links
4_ = brain.EntryWithMemory("messages", []openai.ChatCompletionMessage{{Role: openai.ChatMessageRoleUser, Content: "What is the weather in Boston today?"}})
Retrieving Results from Memory
Brain
operations are asynchronous and unlimited in terms of timing for fetching results. We typically call Wait()
to wait for Brain
to enter Sleeping
state or for a certain Memory
to reach the expected value before retrieving results. Results are obtained from Memory
.
1// block process until the brain is sleeping
2brain.Wait()
3
4messages, _ := json.Marshal(brain.GetMemory("messages"))
5fmt.Printf("messages: %s\n", messages)