4 Commits

Author SHA1 Message Date
cookeem
3af6192633 支持上下文对话方式追加问题 2023-03-27 12:00:57 +08:00
cookeem
5b75b51059 update README 2023-03-23 22:34:09 +08:00
cookeem
49b89d5aad update docker-compose 2023-03-23 22:26:38 +08:00
cookeem
87386c5061 update README 2023-03-23 22:16:30 +08:00
6 changed files with 37 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
# Real-time ChatGPT service, support GPT3/GPT4 # Real-time ChatGPT service, support GPT3/GPT4, support conversation and generate pictures from sentences
- [English README](README.md) - [English README](README.md)
- [中文 README](README_CN.md) - [中文 README](README_CN.md)
@@ -17,8 +17,13 @@
## Demo ## Demo
- Real-time conversation mode
![](chatgpt-service.gif) ![](chatgpt-service.gif)
- Generate picture patterns from sentences
![](chatgpt-image.jpeg)
## Quick start ## Quick start
@@ -36,6 +41,9 @@ vi config.yaml
# your openai.com API key # your openai.com API key
apiKey: "xxxxxx" apiKey: "xxxxxx"
# create pictures directory
mkdir -p assets
chown -R 1000:1000 assets
# Start the service with docker-compose # Start the service with docker-compose
docker-compose up -d docker-compose up -d

View File

@@ -1,4 +1,4 @@
# 实时ChatGPT服务支持GPT3/GPT4 # 实时ChatGPT服务支持GPT3/GPT4,支持对话和通过句子生成图片
- [English README](README.md) - [English README](README.md)
- [中文 README](README_CN.md) - [中文 README](README_CN.md)
@@ -17,8 +17,13 @@
## 效果图 ## 效果图
- 实时对话模式
![](chatgpt-service.gif) ![](chatgpt-service.gif)
- 通过句子生成图片模式
![](chatgpt-image.jpeg)
## 快速开始 ## 快速开始
@@ -36,6 +41,9 @@ vi config.yaml
# openai的apiKey改为你的apiKey # openai的apiKey改为你的apiKey
apiKey: "xxxxxx" apiKey: "xxxxxx"
# 创建生成的图片目录
mkdir -p assets
chown -R 1000:1000 assets
# 使用docker-compose启动服务 # 使用docker-compose启动服务
docker-compose up -d docker-compose up -d

View File

@@ -76,7 +76,7 @@ func (api *Api) wsPingMsg(conn *websocket.Conn, chClose, chIsCloseSet chan int)
} }
} }
func (api *Api) GetChatMessage(conn *websocket.Conn, cli *openai.Client, mutex *sync.Mutex, requestMsg string) { func (api *Api) GetChatMessage(conn *websocket.Conn, cli *openai.Client, mutex *sync.Mutex, reqMsgs []openai.ChatCompletionMessage) {
var err error var err error
var strResp string var strResp string
@@ -84,16 +84,12 @@ func (api *Api) GetChatMessage(conn *websocket.Conn, cli *openai.Client, mutex *
switch api.Config.Model { switch api.Config.Model {
case openai.GPT3Dot5Turbo0301, openai.GPT3Dot5Turbo, openai.GPT4, openai.GPT40314, openai.GPT432K0314, openai.GPT432K: case openai.GPT3Dot5Turbo0301, openai.GPT3Dot5Turbo, openai.GPT4, openai.GPT40314, openai.GPT432K0314, openai.GPT432K:
prompt := reqMsgs[len(reqMsgs)-1].Content
req := openai.ChatCompletionRequest{ req := openai.ChatCompletionRequest{
Model: api.Config.Model, Model: api.Config.Model,
MaxTokens: api.Config.MaxLength, MaxTokens: api.Config.MaxLength,
Temperature: 1.0, Temperature: 1.0,
Messages: []openai.ChatCompletionMessage{ Messages: reqMsgs,
{
Role: openai.ChatMessageRoleUser,
Content: requestMsg,
},
},
Stream: true, Stream: true,
TopP: 1, TopP: 1,
FrequencyPenalty: 0.1, FrequencyPenalty: 0.1,
@@ -151,7 +147,7 @@ func (api *Api) GetChatMessage(conn *websocket.Conn, cli *openai.Client, mutex *
if len(response.Choices) > 0 { if len(response.Choices) > 0 {
var s string var s string
if i == 0 { if i == 0 {
s = fmt.Sprintf(`%s# %s`, s, requestMsg) s = fmt.Sprintf("%s# %s\n\n", s, prompt)
} }
for _, choice := range response.Choices { for _, choice := range response.Choices {
s = s + choice.Delta.Content s = s + choice.Delta.Content
@@ -173,11 +169,12 @@ func (api *Api) GetChatMessage(conn *websocket.Conn, cli *openai.Client, mutex *
api.Logger.LogInfo(fmt.Sprintf("[RESPONSE] %s\n", strResp)) api.Logger.LogInfo(fmt.Sprintf("[RESPONSE] %s\n", strResp))
} }
case openai.GPT3TextDavinci003, openai.GPT3TextDavinci002, openai.GPT3TextCurie001, openai.GPT3TextBabbage001, openai.GPT3TextAda001, openai.GPT3TextDavinci001, openai.GPT3DavinciInstructBeta, openai.GPT3Davinci, openai.GPT3CurieInstructBeta, openai.GPT3Curie, openai.GPT3Ada, openai.GPT3Babbage: case openai.GPT3TextDavinci003, openai.GPT3TextDavinci002, openai.GPT3TextCurie001, openai.GPT3TextBabbage001, openai.GPT3TextAda001, openai.GPT3TextDavinci001, openai.GPT3DavinciInstructBeta, openai.GPT3Davinci, openai.GPT3CurieInstructBeta, openai.GPT3Curie, openai.GPT3Ada, openai.GPT3Babbage:
prompt := reqMsgs[len(reqMsgs)-1].Content
req := openai.CompletionRequest{ req := openai.CompletionRequest{
Model: api.Config.Model, Model: api.Config.Model,
MaxTokens: api.Config.MaxLength, MaxTokens: api.Config.MaxLength,
Temperature: 0.6, Temperature: 0.6,
Prompt: requestMsg, Prompt: prompt,
Stream: true, Stream: true,
//Stop: []string{"\n\n\n"}, //Stop: []string{"\n\n\n"},
TopP: 1, TopP: 1,
@@ -236,7 +233,7 @@ func (api *Api) GetChatMessage(conn *websocket.Conn, cli *openai.Client, mutex *
if len(response.Choices) > 0 { if len(response.Choices) > 0 {
var s string var s string
if i == 0 { if i == 0 {
s = fmt.Sprintf(`%s# %s`, s, requestMsg) s = fmt.Sprintf("%s# %s\n\n", s, prompt)
} }
for _, choice := range response.Choices { for _, choice := range response.Choices {
s = s + choice.Text s = s + choice.Text
@@ -393,6 +390,8 @@ func (api *Api) WsChat(c *gin.Context) {
api.Logger.LogInfo(fmt.Sprintf("websocket connection open")) api.Logger.LogInfo(fmt.Sprintf("websocket connection open"))
cli := openai.NewClient(api.Config.ApiKey) cli := openai.NewClient(api.Config.ApiKey)
reqMsgs := make([]openai.ChatCompletionMessage, 0)
var latestRequestTime time.Time var latestRequestTime time.Time
for { for {
if isClosed { if isClosed {
@@ -466,7 +465,11 @@ func (api *Api) WsChat(c *gin.Context) {
mutex.Lock() mutex.Lock()
_ = conn.WriteJSON(chatMsg) _ = conn.WriteJSON(chatMsg)
mutex.Unlock() mutex.Unlock()
go api.GetChatMessage(conn, cli, mutex, requestMsg) reqMsgs = append(reqMsgs, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: requestMsg,
})
go api.GetChatMessage(conn, cli, mutex, reqMsgs)
} }
} }
} }

BIN
chatgpt-image.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 KiB

View File

@@ -18,5 +18,6 @@ services:
- "9000:9000" - "9000:9000"
volumes: volumes:
- ./config.yaml:/chatgpt-service/config.yaml - ./config.yaml:/chatgpt-service/config.yaml
- ./assets:/chatgpt-service/assets
command: /chatgpt-service/chatgpt-service command: /chatgpt-service/chatgpt-service
restart: always restart: always

2
go.mod
View File

@@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.8.2 github.com/gin-gonic/gin v1.8.2
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.0
github.com/sashabaranov/go-openai v1.5.4 github.com/sashabaranov/go-openai v1.5.7
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )