Skip to content

Commit

Permalink
Fix Duplicate Prefix "🤖 AI:" (#15)
Browse files Browse the repository at this point in the history
* Fix [GenAI] Duplicate Prefix "🤖 AI:"

- [+] fix(chat.go): fix bug where colon is added twice to the message if user string already ends with a colon
- [+] feat(chat.go): add functionality to remove emoji prefixes from messages in GetHistory() method

* Chore Typing Delay

- [+] chore(constant.go): update TypingDelay constant value to 60 milliseconds

* Docs [README] add Another Example

- [+] docs(README.md): add example conversation with AI assistant and information about cyclomatic complexity and simplicity in Go programming
- [+] feat(README.md): add contribution guidelines for adhering to cyclomatic complexity of 5 or less per function
  • Loading branch information
H0llyW00dzZ authored Jan 3, 2024
1 parent 037720f commit fed26b9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,48 @@ Here are some additional resources that you may find helpful:

- [Kubernetes Audit Logging Documentation](https://kubernetes.io/docs/tasks/debug-application-cluster/audit/)

---

### Another Example

$ go run cmd/main.go

🤖 AI: Hello! How can I assist you today?

🤓 You: I have question does this correct about "Strive for simplicity in each function (aim for a cyclomatic complexity under 5 to demonstrate expertise); simplicity is a sign of Go programming mastery." ?

🤖 AI: Yes, the statement "Strive for simplicity in each function (aim for a cyclomatic complexity under 5 to demonstrate expertise); simplicity is a sign of Go programming mastery" is generally correct.

**Cyclomatic complexity** is a measure of the complexity of a function. It is calculated by counting the number of independent paths through the function. A high cyclomatic complexity indicates that the function is difficult to understand and maintain.

**Simplicity** is a key principle of Go programming. Go code is typically concise and easy to read. This makes it easier to understand and maintain.

**Aiming for a cyclomatic complexity under 5** is a good way to ensure that your functions are simple and easy to understand. This will help you to write better code and avoid introducing bugs.

Of course, there are times when it is necessary to write functions that are more complex. However, you should always strive for simplicity whenever possible.

Here are some tips for writing simple functions:

* Use short, descriptive variable names.
* Avoid nesting loops and conditionals.
* Use functions to break down complex tasks into smaller, more manageable tasks.
* Document your functions clearly.

By following these tips, you can write simple, easy-to-understand functions that will make your code more maintainable and bug-free.

---

🤓 You: alright thx for info

🤖 AI: You're welcome! I'm glad I could be of assistance. If you have any more questions, feel free to ask.

---

🤓 You: :quit

🤖 AI: It's been a pleasure assisting you today. Remember, I'm always here if you have any more questions. Take care and have a wonderful day!

---

## Contributing
Contributions is always welcome! If you're able to write code that adheres to a cyclomatic complexity of 5 or less per function, please feel free to submit a pull request. Pull requests that do not meet this criterion will be rejected. Alternatively, if you encounter any issues, do not hesitate to submit an issue report.
29 changes: 27 additions & 2 deletions terminal/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ type ChatHistory struct {
// This method does not return any value or error. It assumes that all input
// is valid and safe to add to the chat history.
func (h *ChatHistory) AddMessage(user, text string) {
h.Messages = append(h.Messages, fmt.Sprintf("%s: %s", user, text))
// Check if the last character of the user string is not a colon
if !strings.HasSuffix(user, PrefixChar) {
// If it is, don't add another colon
h.Messages = append(h.Messages, fmt.Sprintf("%s %s", user, text))
}
// Append the message with the user string (which now has a guaranteed single colon)
h.Messages = append(h.Messages, fmt.Sprintf("%s %s", user, text))
}

// GetHistory concatenates all messages in the chat history into a single
Expand All @@ -37,7 +43,26 @@ func (h *ChatHistory) AddMessage(user, text string) {
//
// string: A newline-separated string of all messages in the chat history.
func (h *ChatHistory) GetHistory() string {
return strings.Join(h.Messages, "\n")
// Create a new slice to hold messages without emoji prefixes
sanitizedMessages := make([]string, 0, len(h.Messages))

// Define the prefixes to be removed
prefixesToRemove := []string{YouNerd, AiNerd}

for _, msg := range h.Messages {
sanitizedMsg := msg
// Remove each prefix from the start of the message
for _, prefix := range prefixesToRemove {
if strings.HasPrefix(sanitizedMsg, prefix) {
sanitizedMsg = strings.TrimPrefix(sanitizedMsg, prefix)
break // Assume only one prefix will match and then break the loop
}
}
sanitizedMessages = append(sanitizedMessages, sanitizedMsg)
}

// Join the sanitized messages with a newline character
return strings.Join(sanitizedMessages, "\n")
}

// PrintHistory outputs all messages in the chat history to the standard output,
Expand Down
5 changes: 3 additions & 2 deletions terminal/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const (
// this animated chars is magic, it used to show the user that the AI is typing just like human would type
AnimatedChars = "%c"
// this model is subject to changed in future
ModelAi = "gemini-pro"
TypingDelay = 100 * time.Millisecond
ModelAi = "gemini-pro"
// this may subject to changed in future for example can customize the delay
TypingDelay = 60 * time.Millisecond
)

// Defined constants for language
Expand Down

0 comments on commit fed26b9

Please sign in to comment.