Quick Start

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

Define the graph's topology by outlining a brainprint (a shorthand for brain blueprint).

1bp := rModel.NewBrainPrint()

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)

There are three types of Links:

  • Normal Links: Include the source Neuron and destination Neuron
  • Entry Links: Only have the destination Neuron
  • End Links: The Brain will automatically go into a Sleeping state when there are no active Neurons and Links, but you can also explicitly define end links to set an endpoint for the Brain to run. You only need to specify the source Neuron, the destination 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")

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}

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()

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 Links. 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 Links:

  • Use brain.Entry() to trigger all entry links.
  • Use brain.EntryWithMemory() to set initial Memory and trigger all entry links.
  • Use brain.TrigLinks() to trigger specific Links.
  • You can also use brain.SetMemory() + brain.TrigLinks() to set initial Memory and trigger specific Links.

⚠️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?"}})

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)