oldweipro / claude-to-chatgpt

Claude 聊天接口适配到 OpenAI API 标准接口。
MIT License
102 stars 18 forks source link

大佬,向您请教一个问题,我这边发送给claude.ai的消息是json的格式,是正常的不? #8

Closed zisanjun closed 1 year ago

zisanjun commented 1 year ago

详情如图,麻烦您有空的时候帮忙看看~感谢 O_2CK`U 8Y))0~A_WR@1{_R

oldweipro commented 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中,因为每次对话都是新的聊天窗口(聊天结束会删除当前对话),所以每次都需要提交历史消息(历史消息由你的业务方传入)。

zisanjun commented 1 year ago

感谢!卫哥太强啦!