Integrate Python with OpenAI API for Promptfoo
Integrate custom Python logic with promptfoo for advanced prompt testing and evaluation using the OpenAI API.
Why it matters
Extend promptfoo's capabilities by creating a custom Python provider. This allows seamless integration with the OpenAI API for advanced prompt testing and evaluation.
Outcomes
What it gets done
Develop a custom provider using Python.
Connect promptfoo to the OpenAI API.
Enable programmatic interaction with LLMs for testing.
Facilitate code generation and review within promptfoo.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-python-provider | bash Capabilities
What this chain does
Writes source code or scripts from a description.
Traces errors to their root cause and suggests fixes.
Analyzes code for bugs, style issues, and improvements.
Overview
Python Provider
What it does
This example demonstrates creating a custom Python provider for promptfoo. It enables the integration of your own Python scripts to interact with external services, such as the OpenAI API, directly within prompt testing workflows.
How it connects
Use this when you require custom Python logic for promptfoo evaluations, like calling external APIs or performing complex data transformations. Avoid it if your needs are met by built-in providers or if you lack custom Python code for integration.
Source README
yaml-language-server: $schema=https://promptfoo.dev/config-schema.json
description: Custom Python provider with functions and multi-format configs
prompts:
- 'Write a very concise funny tweet about {{topic}}'
providers:
id: file://provider.py # defaults to call_api function
config:
someOption: foobarid: file://provider.py:some_other_function
id: file://provider.py:async_provider
label: async provider
Demonstrates how to load configuration from various file formats
- id: 'file://provider.py:call_api'
config:YAML
settings: 'file://configs/fileConfig.yaml'JavaScript file
formatting: 'file://configs/fileConfig.js:getFormatConfig'
nested: # Python file
parameters: 'file://configs/fileConfig.py:get_params'
tests:
- vars:
topic: bananas
assert:- type: contains
value: Bananamax
- type: contains
- vars:
topic: fruits
assert:- type: llm-rubric
value: includes at least one emoji
- type: llm-rubric
- vars:
topic: turtles
assert:- type: llm-rubric
value: is funny
- type: llm-rubric
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.