The agent's brain

The chat bot type that we will use for the live chat feature, AgentCase, will implement the following Bot interface:

type Bot interface {
Greeting() string
Reply(string) string
Name() string
Title() string
ThumbnailPath() string
SetName(string)
SetTitle(string)
SetThumbnailPath(string)
}

The Greeting method will be used to send an initial greeting to the user, enticing them to interact with the chat bot.

The Reply method accepts a question (a string) and returns a reply (also a string) for the given question.

The rest of the methods implemented are for purely psychological reasons to give humans the illusion that they are communicating with someone, rather than something.

The Name method is a getter method that returns the chat bot's name.

The Title method is a getter method that returns the chat bot's title.

The ThumbnailPath method is a getter method that returns the path to the chat bot's avatar image.

Each of the getter methods has a corresponding setter method: SetName, SetTitle, and SetThumbnailPath.

By defining the Bot interface, we are clearly stating the expectations of a chat bot. This allows us to make the chat bot solution extensible in the future. For example, the intelligence that Case exhibits may be too rudimentary and limiting. In the near future, we may want to implement a bot named Molly, whose intelligence may be implemented using a more powerful algorithm. As long as the Molly chat bot implements the Bot interface, the new chat bot can be easily plugged into our web application.

In fact, from the perspective of the server-side web application, it would just be a one-line code change. Instead of instantiating an AgentCase instance, we would instantiate an AgentMolly instance instead. Besides the difference in intelligence, the new chat bot, Molly, would come with its own name, title, and avatar image, so humans would be able to differentiate it from Case.

Here's the AgentCase struct:

type AgentCase struct {
Bot
name string
title string
thumbnailPath string
knowledgeBase map[string]string
knowledgeCorpus []string
sampleQuestions []string
}

We have embedded the Bot interface to the struct definition, indicating that the AgentCase type will implement the Bot interface. The field name is for the name of the agent. The field title is for the title of the agent. The field thumbnailPath is used to specify the path to the chat bot's avatar image.

The knowledgeBase field is  map of type map[string]string. This is essentially the agent's brain. Keys in the map are the common terms found in a particular question. Values in the map are the answers to the question.

The knowledgeCorpus field, a string byte slice, is a knowledge corpus of the terms that may exist in questions that the bot will be asked. We use the keys of the knowledgeBase map to construct the knowledgeCorpus. A corpus is a collection of text that is used to conduct linguistic analysis. In our case, we will conduct the linguistic analysis based on the question (the query) that the human user provided to the bot.

The sampleQuestions field, a string byte slice, will contain a list of sample questions that the user may ask the chat bot. The chat bot will provide the user with a sample question when it greets them to entice the human user into a conversation. It is understood that the human user is free to paraphrase the sample question or ask an entirely different question depending on their preference.

The initializeIntelligence method is used to initialize Case's brain:

func (a *AgentCase) initializeIntelligence() {

a.knowledgeBase = map[string]string{
"isomorphic go isomorphic go web applications": "Isomorphic Go is the methodology to create isomorphic web applications using the Go (Golang) programming language. An isomorphic web application, is a web application, that contains code which can run, on both the web client and the web server.",
"kick recompile code restart web server instance instant kickstart lightweight mechanism": "Kick is a lightweight mechanism to provide an instant kickstart to a Go web server instance, upon the modification of a Go source file within a particular project directory (including any subdirectories). An instant kickstart consists of a recompilation of the Go code and a restart of the web server instance. Kick comes with the ability to take both the go and gopherjs commands into consideration when performing the instant kickstart. This makes it a really handy tool for isomorphic golang projects.",
"starter code starter kit": "The isogoapp, is a basic, barebones web app, intended to be used as a starting point for developing an Isomorphic Go application. Here's the link to the github page: https://github.com/isomorphicgo/isogoapp",
"lack intelligence idiot stupid dumb dummy don't know anything": "Please don't question my intelligence, it's artificial after all!",
"find talk topic presentation lecture subject": "Watch the Isomorphic Go talk by Kamesh Balasubramanian at GopherCon India: https://youtu.be/zrsuxZEoTcs",
"benefits of the technology significance of the technology importance of the technology": "Here are some benefits of Isomorphic Go: Unlike JavaScript, Go provides type safety, allowing us to find and eliminate many bugs at compile time itself. Eliminates mental context-shifts between back-end and front-end coding. Page loading prompts are not necessary.",
"perform routing web app register routes define routes": "You can implement client-side routing in your web application using the isokit Router preventing the dreaded full page reload.",
"render templates perform template rendering": "Use template sets, a set of project templates that are persisted in memory and are available on both the server-side and the client-side",
"cogs reusable components react-like react": "Cogs are reuseable components in an Isomorphic Go web application.",
}

a.knowledgeCorpus = make([]string, 1)
for k, _ := range a.knowledgeBase {
a.knowledgeCorpus = append(a.knowledgeCorpus, k)
}

a.sampleQuestions = []string{"What is isomorphic go?", "What are the benefits of this technology?", "Does isomorphic go offer anything react-like?", "How can I recompile code instantly?", "How can I perform routing in my web app?", "Where can I get starter code?", "Where can I find a talk on this topic?"}

}

There are three important tasks that occur within this method:

  • First, we set Case's knowledge base.
  • Second, we set Case's knowledge corpus.
  • Third, we set the sample questions, which Case will utilize when greeting the human user.

The first task we must take care of is to set Case's knowledge base. This consists of setting the knowledgeBase property of the AgentCase instance. As mentioned earlier, the keys in the map refer to terms found in the question, and the values in the map are the answers to the question. For example, the "isomorphic go isomorphic go web applications" key could service the following questions:

  • What is Isomorphic Go?
  • What can you tell me about Isomorphic Go?

It can also service statements that aren't questions:

  • Tell me about Isomorphic Go
  • Give me the rundown on Isomorphic Go
Due to the the large amount of text contained within the map literal declaration for the knowledgeBase map, I encourage you to view the source file, agentcase.go, on a computer.

The second task we must take care of is to set Case's corpus, the collection of text used for linguistic analysis used against the user's question. The corpus is constructed from the keys of the knowledgeBase map. We set the knowledgeCorpus field property of the AgentCase instance to a newly created string byte slice using the built-in make function. Using a for loop, we iterate through all the entries in the knowledgeBase map and append each key to the knowledgeCorpus field slice.

The third and last task we must take care of is to set the sample questions that Case will present to the human user. We simply populate the sampleQuestions property of the AgentCase instance. We use the string literal declaration to populate all the sample questions that are contained in the string byte slice.

Here are the getter and setter methods of the AgentCase type:

func (a *AgentCase) Name() string {
return a.name
}

func (a *AgentCase) Title() string {
return a.title
}

func (a *AgentCase) ThumbnailPath() string {
return a.thumbnailPath
}

func (a *AgentCase) SetName(name string) {
a.name = name
}

func (a *AgentCase) SetTitle(title string) {
a.title = title
}

func (a *AgentCase) SetThumbnailPath(thumbnailPath string) {
a.thumbnailPath = thumbnailPath
}

These methods are used to get and set the name, title, and thumbnailPath fields of the AgentCase object.

Here's the constructor function used to create a new AgentCase instance:

func NewAgentCase() *AgentCase {
agentCase := &AgentCase{name: "Case", title: "Resident Isomorphic Gopher Agent", thumbnailPath: "/static/images/chat/Case.png"}
agentCase.initializeIntelligence()
return agentCase
}

We declare and initialize the agentCase variable with a new AgentCase instance, setting the fields for name, title, and thumbnailPath. We then call the initializeIntelligence method to initialize Case's brain. Finally, we return the newly created and initialized AgentCase instance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.40