(options: ChatOptions)
| 77 | } |
| 78 | |
| 79 | async chat(options: ChatOptions) { |
| 80 | const messages = options.messages.map((v) => ({ |
| 81 | // "error_code": 336006, "error_msg": "the role of message with even index in the messages must be user or function", |
| 82 | role: v.role === "system" ? "user" : v.role, |
| 83 | content: getMessageTextContent(v), |
| 84 | })); |
| 85 | |
| 86 | // "error_code": 336006, "error_msg": "the length of messages must be an odd number", |
| 87 | if (messages.length % 2 === 0) { |
| 88 | if (messages.at(0)?.role === "user") { |
| 89 | messages.splice(1, 0, { |
| 90 | role: "assistant", |
| 91 | content: " ", |
| 92 | }); |
| 93 | } else { |
| 94 | messages.unshift({ |
| 95 | role: "user", |
| 96 | content: " ", |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | const modelConfig = { |
| 102 | ...useAppConfig.getState().modelConfig, |
| 103 | ...useChatStore.getState().currentSession().mask.modelConfig, |
| 104 | ...{ |
| 105 | model: options.config.model, |
| 106 | }, |
| 107 | }; |
| 108 | |
| 109 | const shouldStream = !!options.config.stream; |
| 110 | const requestPayload: RequestPayload = { |
| 111 | messages, |
| 112 | stream: shouldStream, |
| 113 | model: modelConfig.model, |
| 114 | temperature: modelConfig.temperature, |
| 115 | presence_penalty: modelConfig.presence_penalty, |
| 116 | frequency_penalty: modelConfig.frequency_penalty, |
| 117 | top_p: modelConfig.top_p, |
| 118 | }; |
| 119 | |
| 120 | console.log("[Request] Baidu payload: ", requestPayload); |
| 121 | |
| 122 | const controller = new AbortController(); |
| 123 | options.onController?.(controller); |
| 124 | |
| 125 | try { |
| 126 | let chatPath = this.path(Baidu.ChatPath(modelConfig.model)); |
| 127 | |
| 128 | // getAccessToken can not run in browser, because cors error |
| 129 | if (!!getClientConfig()?.isApp) { |
| 130 | const accessStore = useAccessStore.getState(); |
| 131 | if (accessStore.useCustomConfig) { |
| 132 | if (accessStore.isValidBaidu()) { |
| 133 | const { access_token } = await getAccessToken( |
| 134 | accessStore.baiduApiKey, |
| 135 | accessStore.baiduSecretKey, |
| 136 | ); |
nothing calls this directly
no test coverage detected