Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gml 1809 feedback analysis access control #256

Merged
merged 15 commits into from
Aug 2, 2024
2 changes: 1 addition & 1 deletion chat-history/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ clean:

run: clean test build
clear
CONFIG="config.json" DEV=true ./chat-history
CONFIG_FILES="chat_config.json,db_config.json" DEV=true ./chat-history


52 changes: 29 additions & 23 deletions chat-history/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ type LLMConfig struct {
}

type DbConfig struct {
Port string `json:"apiPort"`
DbPath string `json:"dbPath"`
DbLogPath string `json:"dbLogPath"`
LogPath string `json:"logPath"`
// DbHostname string `json:"hostname"`
// Username string `json:"username"`
// Password string `json:"password"`
Port string `json:"apiPort"`
DbPath string `json:"dbPath"`
DbLogPath string `json:"dbLogPath"`
LogPath string `json:"logPath"`
TgCloud bool `json:"tgCloud"`
ConversationAccessRoles []string `json:"conversationAccessRoles"`
TgDbHost string `json:"hostname"`
Username string `json:"username"`
Password string `json:"password"`
GsPort string `json:"gsPort"`
// GetToken string `json:"getToken"`
// DefaultTimeout string `json:"default_timeout"`
// DefaultMemThreshold string `json:"default_mem_threshold"`
Expand All @@ -29,25 +32,28 @@ type Config struct {
// LLMConfig
}

func LoadConfig(path string) (Config, error) {
var b []byte
if _, err := os.Stat(path); os.IsNotExist(err) {
// file doesn't exist read from env
cfg := os.Getenv("CONFIG")
if cfg == "" {
fmt.Println("CONFIG path is not found nor is the CONFIG json env variable defined")
os.Exit(1)
func LoadConfig(paths ...string) (Config, error) {
var cfg Config
for _, path := range paths {
var b []byte
if _, err := os.Stat(path); os.IsNotExist(err) {
// file doesn't exist read from env
cfg := os.Getenv("CONFIG_FILES")
if cfg == "" {
fmt.Println("CONFIG path is not found nor is the CONFIG json env variable defined")
os.Exit(1)
}
b = []byte(cfg)
} else {
b, err = os.ReadFile(path)
if err != nil {
return Config{}, err
}
}
b = []byte(cfg)
} else {
b, err = os.ReadFile(path)
if err != nil {

if err := json.Unmarshal(b, &cfg); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're reading multiple config files, won't cfg just be the last file that was parsed in the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I update the loadConfig to load multiple config file in to different config structs. i.e., ChatDbConfig, and TgDbConfig. It looks like this: router.HandleFunc("GET /get_feedback", routes.GetFeedback(cfg.TgDbConfig.Hostname, cfg.TgDbConfig.GsPort, cfg.ChatDbConfig.ConversationAccessRoles, cfg.TgDbConfig.TgCloud))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also added test cases for non-admin users and non-existent users.

return Config{}, err
}
}

var cfg Config
json.Unmarshal(b, &cfg)

return cfg, nil
}
32 changes: 16 additions & 16 deletions chat-history/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

func TestLoadConfig(t *testing.T) {
pth := setup(t)

// Print the path for debugging
fmt.Println("Configuration file path:", pth)

cfg, err := LoadConfig(pth)
if err != nil {
t.Fatal(err)
Expand All @@ -23,25 +27,21 @@ func TestLoadConfig(t *testing.T) {

func setup(t *testing.T) string {
tmp := t.TempDir()
pth := fmt.Sprintf("%s/%s", tmp, "config.json")
pth := fmt.Sprintf("%s/%s", tmp, "chat_config.json")
dat := `

{
"apiPort":"8000",
"hostname": "http://localhost:14240",
"dbPath": "chats.db",
"dbLogPath": "db.log",
"logPath": "requestLogs.jsonl",
"username": "tigergraph",
"password": "tigergraph",
"getToken": false,
"default_timeout": 300,
"default_mem_threshold": 5000,
"default_thread_limit": 8
"apiPort":"8000",
"dbPath": "chats.db",
"dbLogPath": "db.log",
"logPath": "requestLogs.jsonl",
"tgCloud": true,
"conversationAccessRoles": ["superuser", "globaldesigner"]
}`
err := os.WriteFile(pth, []byte(dat), 0644)
if err != nil {
t.Fatal("error setting up config.json")

if err := os.WriteFile(pth, []byte(dat), 0644); err != nil {
t.Fatal("error setting up chat_config.json")
}

return pth

}
12 changes: 12 additions & 0 deletions chat-history/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ func UpdateConversationById(message structs.Message) (*structs.Conversation, err
return &convo, nil
}

// GetAllMessages retrieves all messages from the database
func GetAllMessages() ([]structs.Message, error) {
var messages []structs.Message

// Use GORM to query all messages
if err := db.Find(&messages).Error; err != nil {
return nil, err
}

return messages, nil
}

func populateDB() {
mu.Lock()
defer mu.Unlock()
Expand Down
23 changes: 23 additions & 0 deletions chat-history/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,29 @@ func TestParallelWrites(t *testing.T) {
}
}

func TestGetAllMessages(t *testing.T) {
setupTest(t, true)

messages, err := GetAllMessages()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

// Ensure that messages are returned
if len(messages) == 0 {
t.Fatalf("Expected some messages, got none")
}

// Validate the structure of the messages
for _, m := range messages {
if uuid.Validate(m.ConversationId.String()) != nil ||
uuid.Validate(m.MessageId.String()) != nil ||
(m.Role != "system" && m.Role != "user") {
t.Fatalf("Invaid message structure: %v", m)
}
}
}

/*
helper functions
*/
Expand Down
4 changes: 2 additions & 2 deletions chat-history/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module chat-history
go 1.22.3

require (
github.com/go-chi/chi/v5 v5.0.12
github.com/go-chi/httplog/v2 v2.0.11
github.com/google/uuid v1.6.0
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.10
)

require (
github.com/go-chi/httplog/v2 v2.0.11 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
Expand Down
2 changes: 2 additions & 0 deletions chat-history/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/GenericP3rson/TigerGo v0.0.4 h1:xI7d/cLJ6sRP4fzanInakARE0XGk1YAmvn5KrH1fwFU=
github.com/GenericP3rson/TigerGo v0.0.4/go.mod h1:PGpAFO9vNA7l34WSGYCtWb/eqVKHuIq1xqvizBlNhRM=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/httplog/v2 v2.0.11 h1:eu6kYksMEJzBcOP+ba/iYudc0m5rv4VvBAzroJMkaY4=
Expand Down
14 changes: 9 additions & 5 deletions chat-history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import (
)

func main() {
configPath:= os.Getenv("CONFIG")
config, err := config.LoadConfig(configPath)
configPath := os.Getenv("CONFIG_FILES")
// Split the paths into a slice
configPaths := strings.Split(configPath, ",")

cfg, err := config.LoadConfig(configPaths...)
if err != nil {
panic(err)
}
db.InitDB(config.DbPath, config.DbLogPath)
db.InitDB(cfg.DbPath, cfg.DbLogPath)

// make router
router := http.NewServeMux()
Expand All @@ -30,14 +33,15 @@ func main() {
router.HandleFunc("GET /user/{userId}", routes.GetUserConversations)
router.HandleFunc("GET /conversation/{conversationId}", routes.GetConversation)
router.HandleFunc("POST /conversation", routes.UpdateConversation)
router.HandleFunc("GET /get_feedback", routes.GetFeedback(cfg.TgDbHost, cfg.GsPort, cfg.ConversationAccessRoles, cfg.TgCloud))

// create server with middleware
dev := strings.ToLower(os.Getenv("DEV")) == "true"
var port string
if dev {
port = fmt.Sprintf("localhost:%s", config.Port)
port = fmt.Sprintf("localhost:%s", cfg.Port)
} else {
port = fmt.Sprintf(":%s", config.Port)
port = fmt.Sprintf(":%s", cfg.Port)
}

handler := middleware.ChainMiddleware(router,
Expand Down
Loading
Loading