(
followUpPromptsConfig: FollowUpPromptConfig,
apiMessageContent: string,
options: ICommonObject
)
| 21 | } |
| 22 | |
| 23 | export const generateFollowUpPrompts = async ( |
| 24 | followUpPromptsConfig: FollowUpPromptConfig, |
| 25 | apiMessageContent: string, |
| 26 | options: ICommonObject |
| 27 | ): Promise<FollowUpPromptResult | undefined> => { |
| 28 | if (followUpPromptsConfig) { |
| 29 | if (!followUpPromptsConfig.status) return undefined |
| 30 | const providerConfig = followUpPromptsConfig[followUpPromptsConfig.selectedProvider] |
| 31 | if (!providerConfig) return undefined |
| 32 | const credentialId = providerConfig.credentialId as string |
| 33 | const credentialData = await getCredentialData(credentialId ?? '', options) |
| 34 | const followUpPromptsPrompt = providerConfig.prompt.replace('{history}', apiMessageContent) |
| 35 | |
| 36 | switch (followUpPromptsConfig.selectedProvider) { |
| 37 | case FollowUpPromptProvider.ANTHROPIC: { |
| 38 | const llm = new ChatAnthropic({ |
| 39 | apiKey: credentialData.anthropicApiKey, |
| 40 | model: providerConfig.modelName, |
| 41 | temperature: parseFloat(`${providerConfig.temperature}`) |
| 42 | }) |
| 43 | // @ts-ignore |
| 44 | const structuredLLM = llm.withStructuredOutput(FollowUpPromptType, { |
| 45 | method: 'functionCalling' |
| 46 | }) |
| 47 | const structuredResponse = await structuredLLM.invoke(followUpPromptsPrompt) |
| 48 | return structuredResponse as FollowUpPromptResult |
| 49 | } |
| 50 | case FollowUpPromptProvider.AZURE_OPENAI: { |
| 51 | const azureOpenAIApiKey = credentialData['azureOpenAIApiKey'] |
| 52 | const azureOpenAIApiInstanceName = credentialData['azureOpenAIApiInstanceName'] |
| 53 | const azureOpenAIApiDeploymentName = credentialData['azureOpenAIApiDeploymentName'] |
| 54 | const azureOpenAIApiVersion = credentialData['azureOpenAIApiVersion'] |
| 55 | |
| 56 | const llm = new AzureChatOpenAI({ |
| 57 | azureOpenAIApiKey, |
| 58 | azureOpenAIApiInstanceName, |
| 59 | azureOpenAIApiDeploymentName, |
| 60 | azureOpenAIApiVersion, |
| 61 | model: providerConfig.modelName, |
| 62 | temperature: parseFloat(`${providerConfig.temperature}`) |
| 63 | }) |
| 64 | // use structured output parser because withStructuredOutput is not working |
| 65 | const parser = StructuredOutputParser.fromZodSchema(FollowUpPromptType as any) |
| 66 | const formatInstructions = parser.getFormatInstructions() |
| 67 | const prompt = PromptTemplate.fromTemplate(` |
| 68 | ${providerConfig.prompt} |
| 69 | |
| 70 | {format_instructions} |
| 71 | `) |
| 72 | const chain = prompt.pipe(llm).pipe(parser) |
| 73 | const structuredResponse = await chain.invoke({ |
| 74 | history: apiMessageContent, |
| 75 | format_instructions: formatInstructions |
| 76 | }) |
| 77 | return structuredResponse as FollowUpPromptResult |
| 78 | } |
| 79 | case FollowUpPromptProvider.GOOGLE_GENAI: { |
| 80 | const model = new ChatGoogleGenerativeAI({ |
no test coverage detected