Closed zisanjun closed 1 year ago
openai消息体转claude消息体的逻辑代码在 convert.go 文件和 NewChatMessageRequest 方法中
package service
import (
"encoding/json"
"fmt"
"github.com/oldweipro/claude-to-chatgpt/model"
)
// OpenaiToClaudeParams 转换成claude的参数
func OpenaiToClaudeParams(chatCompletionRequest model.ChatCompletionRequest) *model.ChatMessageRequest {
completionMessages := chatCompletionRequest.Messages
text := completionMessages[len(completionMessages)-1]
history := completionMessages[:len(completionMessages)-1]
textMarshal, err := json.Marshal(text)
if err != nil {
fmt.Println("Text marshal err:", err)
}
textMessage := string(textMarshal)
historyMessage := ""
if len(history) > 0 {
historyMarshal, err := json.Marshal(history)
if err != nil {
fmt.Println("History marshal err:", err)
}
historyMessage = string(historyMarshal)
}
return model.NewChatMessageRequest(textMessage, historyMessage)
}
主要是把历史消息存放在文件中传给Claude,用户当前提问prompt的放在Text和Completion中,因为每次对话都是新的聊天窗口(聊天结束会删除当前对话),所以每次都需要提交历史消息(历史消息由你的业务方传入)。
感谢!卫哥太强啦!
详情如图,麻烦您有空的时候帮忙看看~感谢