(options: ChatOptions)
| 82 | } |
| 83 | |
| 84 | async chat(options: ChatOptions) { |
| 85 | const visionModel = isVisionModel(options.config.model); |
| 86 | const messages: ChatOptions["messages"] = []; |
| 87 | for (const v of options.messages) { |
| 88 | if (v.role === "assistant") { |
| 89 | const content = getMessageTextContentWithoutThinking(v); |
| 90 | messages.push({ role: v.role, content }); |
| 91 | } else { |
| 92 | const content = visionModel |
| 93 | ? await preProcessImageContent(v.content) |
| 94 | : getMessageTextContent(v); |
| 95 | messages.push({ role: v.role, content }); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | const modelConfig = { |
| 100 | ...useAppConfig.getState().modelConfig, |
| 101 | ...useChatStore.getState().currentSession().mask.modelConfig, |
| 102 | ...{ |
| 103 | model: options.config.model, |
| 104 | providerName: options.config.providerName, |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | const requestPayload: RequestPayload = { |
| 109 | messages, |
| 110 | stream: options.config.stream, |
| 111 | model: modelConfig.model, |
| 112 | temperature: modelConfig.temperature, |
| 113 | presence_penalty: modelConfig.presence_penalty, |
| 114 | frequency_penalty: modelConfig.frequency_penalty, |
| 115 | top_p: modelConfig.top_p, |
| 116 | // max_tokens: Math.max(modelConfig.max_tokens, 1024), |
| 117 | // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. |
| 118 | }; |
| 119 | |
| 120 | console.log("[Request] openai payload: ", requestPayload); |
| 121 | |
| 122 | const shouldStream = !!options.config.stream; |
| 123 | const controller = new AbortController(); |
| 124 | options.onController?.(controller); |
| 125 | |
| 126 | try { |
| 127 | const chatPath = this.path(SiliconFlow.ChatPath); |
| 128 | const chatPayload = { |
| 129 | method: "POST", |
| 130 | body: JSON.stringify(requestPayload), |
| 131 | signal: controller.signal, |
| 132 | headers: getHeaders(), |
| 133 | }; |
| 134 | |
| 135 | // console.log(chatPayload); |
| 136 | |
| 137 | // Use extended timeout for thinking models as they typically require more processing time |
| 138 | const requestTimeoutId = setTimeout( |
| 139 | () => controller.abort(), |
| 140 | getTimeoutMSByModel(options.config.model), |
| 141 | ); |
nothing calls this directly
no test coverage detected