From e6936319a2bfde01a9118e9191a8a972c5d34f14 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Thu, 6 Apr 2023 15:29:16 +0800 Subject: [PATCH] claude could use context as well (#64) --- README.md | 4 ++-- api/chat_main_handler.go | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f6eda247..38da4f73 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## 规则 - 第一个消息是系统消息(prompt) -- OPENAI 上下文默认附带最新创建的10条消息, (Claude 模型不带上下文) +- 上下文默认附带最新创建的10条消息 - 第一个注册的用户是管理员 - 默认限流 100 chatGPT call /10分钟 (OPENAI_RATELIMIT=100) @@ -22,7 +22,7 @@ ## How to Use - The first message is a system message (prompt) -- by default, the latest 10 messages are context for opeanai model. no context for claude v1. +- by default, the latest 10 messages are context - First user is superuser. - 100 chatgpt api call / 10 mins (OPENAI_RATELIMIT=100) diff --git a/api/chat_main_handler.go b/api/chat_main_handler.go index bf396e3c..fd077b11 100644 --- a/api/chat_main_handler.go +++ b/api/chat_main_handler.go @@ -380,19 +380,30 @@ func chatStreamClaude(w http.ResponseWriter, chatSession sqlc_queries.ChatSessio // set the url url := "https://api.anthropic.com/v1/complete" - msg := chat_compeletion_messages[len(chat_compeletion_messages)-1].Content + + var sb strings.Builder // create a new strings.Builder + // iterate through the messages and format them + for _, message := range chat_compeletion_messages { + // print the user's question + if message.Role != "assistant" { + sb.WriteString(fmt.Sprintf("\n\nHuman: %s\n\nAssistant: ", message.Content)) + } else { + // convert assistant's response to json format + sb.WriteString(fmt.Sprintf("%s\n", message.Content)) + } + } // create the json data jsonData := map[string]interface{}{ - "prompt": "\n\nHuman: " + msg + "\n\nAssistant: ", + "prompt": sb.String(), "model": "claude-v1", "max_tokens_to_sample": chatSession.MaxTokens, + "temperature": chatSession.Temperature, "stop_sequences": []string{"\n\nHuman:"}, "stream": true, } // convert data to json format jsonValue, _ := json.Marshal(jsonData) - // create the request req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue)) if err != nil {