Skip to content

Commit

Permalink
claude could use context as well (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho authored Apr 6, 2023
1 parent f60b14f commit e693631
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## 规则

- 第一个消息是系统消息(prompt)
- OPENAI 上下文默认附带最新创建的10条消息, (Claude 模型不带上下文)
- 上下文默认附带最新创建的10条消息
- 第一个注册的用户是管理员
- 默认限流 100 chatGPT call /10分钟 (OPENAI_RATELIMIT=100)

Expand All @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e693631

Please sign in to comment.