Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Minor Improvements
Browse files Browse the repository at this point in the history
Change Log:

* Print style modified
* API KEY field added for profile view
* Profile view added
  • Loading branch information
dabumana committed Apr 4, 2023
1 parent 7a389e9 commit 46ec00c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
17 changes: 10 additions & 7 deletions service/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package service
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
Expand All @@ -20,6 +19,7 @@ import (
// Agent - Contextual client API
type Agent struct {
id string
key string
ctx context.Context
client gpt3.Client
exClient *http.Client
Expand All @@ -33,6 +33,8 @@ type Agent struct {
func (c Agent) Initialize() Agent {
// ID
c.id = "anon"
// Key
c.key = c.GetKeyFromLocal()
// Role
c.preferences.Role = model.Assistant
// Background context
Expand Down Expand Up @@ -68,25 +70,26 @@ func (c Agent) Initialize() Agent {
func (c Agent) Connect() (gpt3.Client, *http.Client) {
godotenv.Load()

apiKey := os.Getenv("API_KEY")
if apiKey == "" {
log.Fatalln("Missing API KEY")
}

externalClient := http.Client{
Transport: http.DefaultTransport,
Timeout: time.Duration(12 * time.Second),
}

option := gpt3.WithHTTPClient(&externalClient)
client := gpt3.NewClient(apiKey, option)
client := gpt3.NewClient(c.key, option)

c.client = client
c.exClient = &externalClient

return c.client, c.exClient
}

// GetKeyFromVault - Get the currect key stablished on the environment
func (c Agent) GetKeyFromLocal() string {
apiKey := os.Getenv("API_KEY")
return apiKey
}

// SetEngineParameters - Set engine parameters for the current prompt
func (c Agent) SetEngineParameters(id string, pmodel string, role model.Roles, temperature float32, topp float32, penalty float32, frequency float32) model.EngineProperties {
properties := model.EngineProperties{
Expand Down
11 changes: 11 additions & 0 deletions service/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func OnExportTrainedTopic() {
}
}

// OnProfile - Profile view event
func OnProfile() {
ReturnToPage(4)
}

// OnChangeRoles - Dropdown from input to change role
func OnChangeRoles(option string, optionIndex int) {
if strings.Contains(option, string(model.User)) {
Expand Down Expand Up @@ -531,6 +536,7 @@ func CreateConsoleView() bool {
AddButton("New conversation", OnNewTopic).
AddButton("Export conversation", OnExportTopic).
AddButton("Export training", OnExportTrainedTopic).
AddButton("Profile", OnProfile).
SetHorizontal(true).
SetLabelColor(tcell.ColorDarkCyan.TrueColor()).
SetFieldBackgroundColor(tcell.ColorDarkGrey.TrueColor()).
Expand Down Expand Up @@ -665,7 +671,12 @@ func CreateIDModalView() {
node.controller.currentAgent.id = textToCheck
return true
}, nil).
AddInputField("Enter your API key: ", node.controller.currentAgent.key, 30, func(textToCheck string, lastChar rune) bool {
node.controller.currentAgent.key = textToCheck
return true
}, nil).
AddButton("Save", func() {
node.controller.currentAgent.client, node.controller.currentAgent.exClient = node.controller.currentAgent.Connect()
ReturnToPage(2)
}).
AddButton("Cancel", func() {
Expand Down
8 changes: 4 additions & 4 deletions service/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func (c Prompt) SendStreamingChatCompletion(service Agent) *gpt3.ChatCompletionS
buffer = append(buffer, out.Choices[0].Delta.Content)
bWriter.Write([]byte(out.Choices[0].Delta.Content))
if out.Choices[0].Delta.Content == "\n" {
fmt.Print("\r" + out.Choices[0].Delta.Content)
fmt.Print("\x1b[32m\r", out.Choices[0].Delta.Content)
} else {
fmt.Print(out.Choices[0].Delta.Content)
fmt.Print("\x1b[32m", out.Choices[0].Delta.Content)
}
})

Expand Down Expand Up @@ -240,9 +240,9 @@ func (c Prompt) SendStreamingCompletion(service Agent) *gpt3.CompletionResponse
buffer = append(buffer, out.Choices[i].Text)
in <- out.Choices[i].Text
if out.Choices[i].Text == "\n" {
fmt.Print("\r" + out.Choices[i].Text)
fmt.Print("\x1b[32m\r", out.Choices[i].Text)
} else {
fmt.Print(out.Choices[i].Text)
fmt.Print("\x1b[32m", out.Choices[i].Text)
}
}
}(node.controller.currentAgent.preferences.InlineText)
Expand Down

0 comments on commit 46ec00c

Please sign in to comment.