MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / executePythonScript

Method executePythonScript

src/providers/pythonCompletion.ts:318–402  ·  view source on GitHub ↗

* Execute the Python script with the specified API type * Handles caching, file reference processing, and executing the Python script * * @param prompt - The prompt to pass to the Python script * @param context - Optional context information * @param apiType - The type of API to call

(
    prompt: string,
    context: CallApiContextParams | undefined,
    apiType: PythonApiType,
  )

Source from the content-addressed store, hash-verified

316 * @returns The response from the Python script
317 */
318 private async executePythonScript(
319 prompt: string,
320 context: CallApiContextParams | undefined,
321 apiType: PythonApiType,
322 ): Promise<any> {
323 if (!this.isInitialized || !this.pool) {
324 await this.initialize();
325 }
326
327 const absPath = path.resolve(path.join(this.options?.config.basePath || '', this.scriptPath));
328 logger.debug(`Computing file hash for script ${absPath}`);
329 const fileHash = sha256(await fs.readFile(absPath, 'utf-8'));
330
331 // Create cache key including the function name to ensure different functions don't share caches
332 const cacheKey = `python:${this.scriptPath}:${this.functionName || 'default'}:${apiType}:${fileHash}:${prompt}:${JSON.stringify(
333 this.options,
334 )}:${JSON.stringify(context?.vars)}`;
335 logger.debug(`PythonProvider cache key: ${cacheKey}`);
336
337 const cache = await getCache();
338 let cachedResult;
339 const cacheEnabled = isCacheEnabled();
340 logger.debug(`PythonProvider cache enabled: ${cacheEnabled}`);
341
342 if (cacheEnabled) {
343 cachedResult = await cache.get(cacheKey);
344 logger.debug(`PythonProvider cache hit: ${Boolean(cachedResult)}`);
345 }
346
347 if (cachedResult) {
348 logger.debug(`Returning cached ${apiType} result for script ${absPath}`);
349 const parsedResult = JSON.parse(cachedResult as string);
350
351 logger.debug(
352 `PythonProvider parsed cached result type: ${typeof parsedResult}, keys: ${Object.keys(parsedResult).join(',')}`,
353 );
354
355 // IMPORTANT: Set cached flag to true so evaluator recognizes this as cached
356 return applyCachedCallApiMetadata(apiType, parsedResult);
357 } else {
358 const sanitizedContext = sanitizeScriptContext('PythonProvider', context);
359
360 // Create a new options object with processed file references included in the config
361 // This ensures any file:// references are replaced with their actual content
362 const optionsWithProcessedConfig = {
363 ...this.options,
364 config: {
365 ...this.options?.config,
366 ...this.config, // Merge in the processed config containing resolved file references
367 },
368 };
369
370 const args = buildPythonScriptArgs(
371 apiType,
372 prompt,
373 optionsWithProcessedConfig,
374 sanitizedContext,
375 );

Callers 3

callApiMethod · 0.95
callEmbeddingApiMethod · 0.95
callClassificationApiMethod · 0.95

Calls 15

initializeMethod · 0.95
sha256Function · 0.90
getCacheFunction · 0.90
isCacheEnabledFunction · 0.90
sanitizeScriptContextFunction · 0.90
safeJsonStringifyFunction · 0.90
buildPythonScriptArgsFunction · 0.85
hasPythonResultErrorFunction · 0.85
resolveMethod · 0.80

Tested by

no test coverage detected