完成前后端框架搭建,完成聊天页面布局

This commit is contained in:
RockYang
2023-03-17 14:17:27 +08:00
commit c25cc97450
35 changed files with 21044 additions and 0 deletions

85
types/config.go Normal file
View File

@@ -0,0 +1,85 @@
package types
import (
"bytes"
"github.com/BurntSushi/toml"
"net/http"
"openai/utils"
"os"
)
type Config struct {
Listen string
Session Session
OpenAi OpenAi
}
// OpenAi configs struct
type OpenAi struct {
ApiKey string
Model string
Temperature float32
MaxTokens int
}
// Session configs struct
type Session struct {
SecretKey string // session encryption key
Name string
Path string
Domain string
MaxAge int
Secure bool
HttpOnly bool
SameSite http.SameSite
}
func NewDefaultConfig() *Config {
return &Config{
Listen: "0.0.0.0:5678",
Session: Session{
SecretKey: utils.RandString(64),
Name: "CHAT_GPT_SESSION_ID",
Domain: "",
Path: "/",
MaxAge: 86400,
Secure: false,
HttpOnly: false,
SameSite: http.SameSiteLaxMode,
},
OpenAi: OpenAi{
Model: "gpt-3.5-turbo",
MaxTokens: 1024,
Temperature: 1.0,
},
}
}
func LoadConfig(configFile string) (*Config, error) {
var config *Config
_, err := os.Stat(configFile)
if err != nil {
config = NewDefaultConfig()
// generate types file
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
if err := encoder.Encode(&config); err != nil {
return nil, err
}
err := os.WriteFile(configFile, buf.Bytes(), 0644)
if err != nil {
return nil, err
}
return config, nil
}
_, err = toml.DecodeFile(configFile, &config)
if err != nil {
return nil, err
}
return config, err
}

49
types/types.go Normal file
View File

@@ -0,0 +1,49 @@
package types
import (
"sync"
)
type LockedMap struct {
lock sync.RWMutex
data map[string]interface{}
}
func NewLockedMap() *LockedMap {
return &LockedMap{
lock: sync.RWMutex{},
data: make(map[string]interface{}),
}
}
func (m *LockedMap) Put(key string, value interface{}) {
m.lock.Lock()
defer m.lock.Unlock()
m.data[key] = value
}
func (m *LockedMap) Get(key string) interface{} {
m.lock.RLock()
defer m.lock.RUnlock()
return m.data[key]
}
func (m *LockedMap) Delete(key string) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.data, key)
}
func (m *LockedMap) ToList() []interface{} {
m.lock.Lock()
defer m.lock.Unlock()
var s = make([]interface{}, 0)
for _, v := range m.data {
s = append(s, v)
}
return s
}