Comments (4)
Hey @Wehzie,
Just pushed a change 7308df2 that should resolve Promises returned by an external JS file. Can you give it a try?
btw, multiline Python asserts are also supported as of a recent update. You should be able to pass a multiline string. It expected the assertion result to be print
ed (e.g. print(True)
, print(0.5)
, etc), which is a bit weird, but it should work nonetheless.
from promptfoo.
Thanks for the breakdown, @Wehzie. I can probably make this clearer in the documentation, and/or break it out onto its own page.
from promptfoo.
Thank you @typpo.
All works. Issue is correctly closed.
Lessons learned:
Paths to scripts, either .py
or .js
are provided with
assert:
- type: javascript
value: file://relative/path/to/script.js
- type: python
value: file///absolute/path/to/script.py
For Python scripts, since print
triggers the return, other prints should be avoided.
For debugging purposes, a simple if DEBUG: print
does the job.
Multiline Python works like JavaScript, for example
assert:
- type: python
value: |
some_var = "hello"
print(some_var == "hello")
Thank you for documenting this in promptfoo/promptfoo-docs@9b8c04f
For JavaScript files, the to-be-executed function must be exported. For example, main
, must be exported with module.exports = main;
.
JavaScript async
works as expected. For an example of assert.js
, see
const VALIDATION_ENDPOINT = "https://example.com/api/validate";
async function evaluate(modelResponse) {
try {
const response = await fetch(VALIDATION_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: modelResponse
});
const data = await response.json();
return data;
} catch (error) {
throw error;
}
}
async function main (output, context) {
const success = await evaluate(output);
console.log(`success: ${testResult}`);
return success;
}
module.exports = main;
When JavaScript or Python asserts are defined within promptfooconfig.yaml
the variable output
is injected and can be accessed without declaration.
This variable holds the model response.
When JavaScript or Python asserts are loaded with file://
, the output is accessed with positional arguments.
The call to Python is of the form python path/to/assert.py model_output context
.
The context variable holds the prompt and the variables of the test case.
The call to JavaScript is of the form main(output, context)
.
For an example of a assert.py
, see
import sys
def main():
if len(sys.argv) >= 3:
output = sys.argv[1]
context = sys.argv[2]
else:
raise ValueError("Model output and context are expected from promptfoo.")
processed = preprocess_output(output)
success = test_output(processed)
return success
print(main())
I missed most of this from the documentation somehow. Anyway, thanks for implementing all this!
from promptfoo.
This change (promptfoo/promptfoo-docs@69e29d2) improves assertion documentation and adds much of the detail you recorded above.
from promptfoo.
Related Issues (20)
- Support `systemInstruction` for Gemini (PALM) HOT 4
- Allow options to avoid using special characters HOT 4
- How can I set threshold for avg. of test cases scores with csv? HOT 2
- Types for `promptfoo.evaluate` broken when using TS
- Add ability to bust cache from the web UI
- When Python provider raises exception, details are no longer recorded HOT 3
- Ensure percent complete accurately reflects test suite
- Migrations path incorrect in self host docker build HOT 4
- [Web UI] Image previewer not working for variables or failed tests
- CI passes despite failing build HOT 1
- python external assertion not working HOT 2
- [Web UI] Increase robustness of markdown rendering
- Specify a label with a prompt function HOT 3
- --no confirmation for cli view HOT 1
- expression to select subset of output to display in view HOT 3
- SqliteError: no such table: evals HOT 1
- Error: Unknown Amazon Bedrock model: meta.llama3-70b-instruct-v1:0 HOT 4
- Compare providers based on latency HOT 3
- Failed to fetch when attempting to log in/sign up on the web-UI after promptfoo share
- Issue when using Vertex Gemini - "undefined" JSON Error HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promptfoo.