( request: FastifyRequestTypebox<typeof GenerateFromPromptResponseSchema>, reply: FastifyReplyTypebox<typeof GenerateFromPromptResponseSchema>, )
| 206 | * @returns {Promise<void>} - A promise that resolves when the response is generated |
| 207 | */ |
| 208 | export const generateResponseFromPrompt = async ( |
| 209 | request: FastifyRequestTypebox<typeof GenerateFromPromptResponseSchema>, |
| 210 | reply: FastifyReplyTypebox<typeof GenerateFromPromptResponseSchema>, |
| 211 | ) => { |
| 212 | try { |
| 213 | const stream = request?.body?.stream; |
| 214 | const inputData = request?.body?.data?.inputData; |
| 215 | const n = request?.body?.data?.n; |
| 216 | const language = request?.body?.data?.language; |
| 217 | const userId = request?.body?.data?.userId; |
| 218 | const prompt = await request.server.orm |
| 219 | .getRepository(Prompt) |
| 220 | .findOne({ where: { id: request?.body?.data?.promptId } }); |
| 221 | const modelIds = prompt?.models ?? []; |
| 222 | const timeout = request?.body?.timeout ?? DEFAULT_TIMEOUT_IN_MS; |
| 223 | const maxRetries = request?.body?.maxRetries ?? DEFAULT_MAX_RETRIES; |
| 224 | |
| 225 | if (inputData) { |
| 226 | if (prompt?.promptParams?.messages) { |
| 227 | prompt.promptParams.messages = prompt.promptParams.messages.map(message => { |
| 228 | const matches = message.content.match(/\[(.*?)\]/g); |
| 229 | if (matches) { |
| 230 | matches.forEach(match => { |
| 231 | const key = match.replace("[", "").replace("]", ""); |
| 232 | message.content = message.content.replace(match, inputData[key as keyof typeof inputData]); |
| 233 | }); |
| 234 | } |
| 235 | return message; |
| 236 | }); |
| 237 | if (language) { |
| 238 | prompt.promptParams.messages.push({ |
| 239 | role: Roles.user, |
| 240 | content: "Please generate in " + language + " language.", |
| 241 | }); |
| 242 | } |
| 243 | } |
| 244 | if (prompt?.promptParams?.prompt) { |
| 245 | const matches = prompt.promptParams.prompt.match(/\[(.*?)\]/g); |
| 246 | if (matches) { |
| 247 | matches.forEach(match => { |
| 248 | const key = match.replace("[", "").replace("]", ""); |
| 249 | prompt.promptParams.prompt = prompt.promptParams.prompt.replace( |
| 250 | match, |
| 251 | inputData[key as keyof typeof inputData], |
| 252 | ); |
| 253 | }); |
| 254 | } |
| 255 | if (language) { |
| 256 | prompt.promptParams.prompt += "\n\nPlease generate in " + language + " language."; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | if (n && prompt?.promptParams?.n) { |
| 261 | prompt.promptParams.n = n; |
| 262 | } |
| 263 | |
| 264 | const orm = request.server.orm; |
| 265 | for (const modelId of modelIds) { |
nothing calls this directly
no test coverage detected