MCPcopy
hub / github.com/FlowiseAI/Flowise / runPrediction

Function runPrediction

packages/components/nodes/chains/LLMChain/LLMChain.ts:161–293  ·  view source on GitHub ↗
(
    inputVariables: string[],
    chain: LLMChain<string | object | BaseLanguageModel<any, BaseLanguageModelCallOptions>>,
    input: string,
    promptValuesRaw: ICommonObject | undefined,
    options: ICommonObject,
    nodeData: INodeData,
    disableStreaming?: boolean
)

Source from the content-addressed store, hash-verified

159}
160
161const runPrediction = async (
162 inputVariables: string[],
163 chain: LLMChain<string | object | BaseLanguageModel<any, BaseLanguageModelCallOptions>>,
164 input: string,
165 promptValuesRaw: ICommonObject | undefined,
166 options: ICommonObject,
167 nodeData: INodeData,
168 disableStreaming?: boolean
169) => {
170 const loggerHandler = new ConsoleCallbackHandler(options.logger, options?.orgId)
171 const callbacks = await additionalCallbacks(nodeData, options)
172
173 const moderations = nodeData.inputs?.inputModeration as Moderation[]
174
175 // this is true if the prediction is external and the client has requested streaming='true'
176 const shouldStreamResponse = !disableStreaming && options.shouldStreamResponse
177 const sseStreamer: IServerSideEventStreamer = options.sseStreamer as IServerSideEventStreamer
178 const chatId = options.chatId
179
180 if (moderations && moderations.length > 0) {
181 try {
182 // Use the output of the moderation chain as input for the LLM chain
183 input = await checkInputs(moderations, input)
184 } catch (e) {
185 await new Promise((resolve) => setTimeout(resolve, 500))
186 if (shouldStreamResponse) {
187 streamResponse(sseStreamer, chatId, e.message)
188 }
189 return formatResponse(e.message)
190 }
191 }
192
193 /**
194 * Apply string transformation to reverse converted special chars:
195 * FROM: { "value": "hello i am benFLOWISE_NEWLINEFLOWISE_NEWLINEFLOWISE_TABhow are you?" }
196 * TO: { "value": "hello i am ben\n\n\thow are you?" }
197 */
198 const promptValues = handleEscapeCharacters(promptValuesRaw, true)
199
200 if (llmSupportsVision(chain.llm)) {
201 const visionChatModel = chain.llm as IVisionChatModal
202 const messageContent = await addImagesToMessages(nodeData, options, visionChatModel.multiModalOption)
203 if (messageContent?.length) {
204 // Add image to the message
205 if (chain.prompt instanceof PromptTemplate) {
206 const existingPromptTemplate = chain.prompt.template as string
207 const msg = HumanMessagePromptTemplate.fromTemplate([
208 ...messageContent,
209 {
210 text: existingPromptTemplate
211 }
212 ])
213 msg.inputVariables = chain.prompt.inputVariables
214 chain.prompt = ChatPromptTemplate.fromMessages([msg])
215 } else if (chain.prompt instanceof ChatPromptTemplate) {
216 if (chain.prompt.promptMessages.at(-1) instanceof HumanMessagePromptTemplate) {
217 const lastMessage = chain.prompt.promptMessages.pop() as HumanMessagePromptTemplate
218 const template = (lastMessage.prompt as PromptTemplate).template as string

Callers 2

initMethod · 0.85
runMethod · 0.85

Calls 9

additionalCallbacksFunction · 0.90
checkInputsFunction · 0.90
streamResponseFunction · 0.90
formatResponseFunction · 0.90
handleEscapeCharactersFunction · 0.90
llmSupportsVisionFunction · 0.90
addImagesToMessagesFunction · 0.90
runMethod · 0.65
callMethod · 0.45

Tested by

no test coverage detected