-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from CsterKuroi/main
support proxy and context
- Loading branch information
Showing
10 changed files
with
260 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package chatgpt | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
|
||
gpt35 "github.com/poorjobless/wechatbot/gpt-client" | ||
) | ||
|
||
type SessionService struct { | ||
cache *cache.Cache | ||
} | ||
|
||
type SessionMeta struct { | ||
Msg []gpt35.Message `json:"msg,omitempty"` | ||
} | ||
|
||
type CacheInterface interface { | ||
GetMsg(sessionId string) []gpt35.Message | ||
SetMsg(sessionId string, msg []gpt35.Message) | ||
Clear(sessionId string) | ||
} | ||
|
||
var sessionServices *SessionService | ||
|
||
func getLength(strPool []gpt35.Message) int { | ||
var total int | ||
for _, v := range strPool { | ||
bytes, _ := json.Marshal(v) | ||
total += len(string(bytes)) | ||
} | ||
return total | ||
} | ||
|
||
func (s *SessionService) GetMsg(sessionId string) (msg []gpt35.Message) { | ||
sessionContext, ok := s.cache.Get(sessionId) | ||
if !ok { | ||
return nil | ||
} | ||
sessionMeta := sessionContext.(*SessionMeta) | ||
return sessionMeta.Msg | ||
} | ||
|
||
func (s *SessionService) SetMsg(sessionId string, msg []gpt35.Message) { | ||
maxLength := 4096 | ||
maxCacheTime := time.Hour * 12 | ||
|
||
for getLength(msg) > maxLength { | ||
msg = append(msg[:1], msg[2:]...) | ||
} | ||
|
||
sessionContext, ok := s.cache.Get(sessionId) | ||
if !ok { | ||
sessionMeta := &SessionMeta{Msg: msg} | ||
s.cache.Set(sessionId, sessionMeta, maxCacheTime) | ||
return | ||
} | ||
sessionMeta := sessionContext.(*SessionMeta) | ||
sessionMeta.Msg = msg | ||
s.cache.Set(sessionId, sessionMeta, maxCacheTime) | ||
} | ||
|
||
func (s *SessionService) Clear(sessionId string) { | ||
s.cache.Delete(sessionId) | ||
} | ||
|
||
func GetSessionCache() CacheInterface { | ||
if sessionServices == nil { | ||
sessionServices = &SessionService{cache: cache.New(time.Hour*12, time.Hour*1)} | ||
} | ||
return sessionServices | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
github.com/eatmoreapple/openwechat v1.2.1 h1:ez4oqF/Y2NSEX/DbPV8lvj7JlfkYqvieeo4awx5lzfU= | ||
github.com/eatmoreapple/openwechat v1.2.1/go.mod h1:61HOzTyvLobGdgWhL68jfGNwTJEv0mhQ1miCXQrvWU8= | ||
github.com/eatmoreapple/openwechat v1.3.0 h1:HlAx7emjaTnvAJ+fcPSc3J3PyF/tULBDWk0KoizARs8= | ||
github.com/eatmoreapple/openwechat v1.3.0/go.mod h1:61HOzTyvLobGdgWhL68jfGNwTJEv0mhQ1miCXQrvWU8= | ||
github.com/eatmoreapple/openwechat v1.4.1 h1:hIVEr2Xaj+r1SXzdTigqhIXiuu6TZd+NPWdEVVt/qeM= | ||
github.com/eatmoreapple/openwechat v1.4.1/go.mod h1:ZxMcq7IpVWVU9JG7ERjExnm5M8/AQ6yZTtX30K3rwRQ= | ||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= | ||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package gpt35 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
const ModelGpt35Turbo = "gpt-3.5-turbo" | ||
|
||
const MaxTokensGpt35Turbo = 4096 | ||
|
||
const ( | ||
RoleUser RoleType = "user" | ||
RoleAssistant RoleType = "assistant" | ||
RoleSystem RoleType = "system" | ||
) | ||
|
||
const DefaultUrl = "https://api.openai.com/v1/chat/completions" | ||
|
||
type Client struct { | ||
transport *http.Client | ||
apiKey string | ||
url string | ||
} | ||
|
||
func NewClient(apiKey, proxy string) *Client { | ||
proxyUrl, err := url.Parse(proxy) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
transport := &http.Transport{ | ||
Proxy: http.ProxyURL(proxyUrl), | ||
} | ||
|
||
return &Client{ | ||
transport: &http.Client{ | ||
Transport: transport, | ||
Timeout: 110 * time.Second, | ||
}, | ||
apiKey: apiKey, | ||
url: DefaultUrl, | ||
} | ||
} | ||
|
||
func NewClientCustomUrl(apiKey string, url string) *Client { | ||
return &Client{ | ||
transport: http.DefaultClient, | ||
apiKey: apiKey, | ||
url: url, | ||
} | ||
} | ||
|
||
func (c *Client) GetChat(r *Request) (*Response, error) { | ||
jsonData, err := json.Marshal(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", c.url, bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.apiKey) | ||
|
||
httpResp, err := c.transport.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
_ = httpResp.Body.Close() | ||
}() | ||
|
||
var resp Response | ||
err = json.NewDecoder(httpResp.Body).Decode(&resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package gpt35 | ||
|
||
type RoleType string | ||
type ModelType string | ||
|
||
type Request struct { | ||
Model string `json:"model"` | ||
Messages []Message `json:"messages"` | ||
MaxTokens int `json:"max_tokens"` | ||
Temperature float64 `json:"temperature"` | ||
} | ||
|
||
type Response struct { | ||
Id string `json:"id"` | ||
Object string `json:"object"` | ||
Created int64 `json:"created"` | ||
Model string `json:"model"` | ||
Choices []struct { | ||
Message Message `json:"message"` | ||
FinishReason string `json:"finish_reason"` | ||
} `json:"choices"` | ||
Usage struct { | ||
PromptTokens int `json:"prompt_tokens"` | ||
CompletionTokes int `json:"completion_tokens"` | ||
TotalTokens int `json:"total_tokens"` | ||
} | ||
} | ||
|
||
type Message struct { | ||
Role RoleType `json:"role"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type Choice struct { | ||
Index int `json:"index"` | ||
Message Message `json:"message"` | ||
FinishReason string `json:"finish_reason"` | ||
} | ||
|
||
type Usage struct { | ||
PromptTokens int `json:"prompt_tokens"` | ||
CompletionTokens int `json:"completion_tokens"` | ||
TotalTokens int `json:"total_tokens"` | ||
} | ||
|
||
type Error struct { | ||
Message string `json:"message"` | ||
Type string `json:"type"` | ||
Param string `json:"param"` | ||
Code string `json:"code"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters