接入 ChatGPT API

This commit is contained in:
RockYang
2023-03-17 18:54:26 +08:00
parent 729612275b
commit 396b7440fa
6 changed files with 156 additions and 65 deletions

View File

@@ -9,14 +9,16 @@ import (
)
type Config struct {
Listen string
Session Session
OpenAi OpenAi
Listen string
Session Session
ProxyURL string
OpenAi OpenAi
}
// OpenAi configs struct
type OpenAi struct {
ApiKey string
ApiURL string
ApiKey []string
Model string
Temperature float32
MaxTokens int
@@ -49,6 +51,8 @@ func NewDefaultConfig() *Config {
SameSite: http.SameSiteLaxMode,
},
OpenAi: OpenAi{
ApiURL: "https://api.openai.com/v1/chat/completions",
ApiKey: []string{""},
Model: "gpt-3.5-turbo",
MaxTokens: 1024,
Temperature: 1.0,

25
types/gpt.go Normal file
View File

@@ -0,0 +1,25 @@
package types
// ApiRequest API 请求实体
type ApiRequest struct {
Model string `json:"model"`
Temperature float32 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
Stream bool `json:"stream"`
Messages []Message `json:"messages"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type ApiResponse struct {
Choices []ChoiceItem `json:"choices"`
}
// ChoiceItem API 响应实体
type ChoiceItem struct {
Delta Message `json:"delta"`
FinishReason string `json:"finish_reason"`
}

View File

@@ -1,49 +0,0 @@
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
}