Skip to content

Commit

Permalink
feat: add support for passthrough script executor config (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: Arjun Attam <arjunattam@gmail.com>
  • Loading branch information
saikatmitra91 and arjunattam authored Apr 3, 2024
1 parent b354507 commit 17860b0
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .changeset/warm-ravens-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@empiricalrun/types": minor
"@empiricalrun/core": minor
"@empiricalrun/cli": patch
---

feat: add support for passthrough config for script executor
2 changes: 1 addition & 1 deletion development/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"path": {
"type": "string"
},
"config": {
"parameters": {
"type": "object"
},
"scorers": {
Expand Down
7 changes: 6 additions & 1 deletion docs/models/custom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ does pre or post-processing around the LLM call or chains multiple LLM calls tog
## Run configuration

In your config file, set `type` as `py-script` and specify the Python file
path in the `value` field.
path in the `path` field.

```json
"runs": [
Expand All @@ -20,12 +20,17 @@ path in the `value` field.
}
]
```
You can additional pass following properties in run configuration:
`name`: Name of the custom run.
`parameters`: Parameters to be passed to the script file to modify its behavior.


The Python file is expected to have a method called `execute` with the following
signature:

- **Arguments**
- inputs: dict of key-value pairs with [sample inputs](../dataset/basics)
- parameters: dict of key-value pairs with the run parameters
- **Returns**: a dict with
- output (string): The response from the model/application
- metadata (dict): Custom key-value pairs that are passed on to the scorer and
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/empiricalrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://assets.empirical.run/config/schema/v1.12.json",
"$schema": "https://assets.empirical.run/config/schema/v1.13.json",
"runs": [
{
"type": "model",
Expand Down
2 changes: 1 addition & 1 deletion examples/chatbot/empiricalrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://assets.empirical.run/config/schema/v1.12.json",
"$schema": "https://assets.empirical.run/config/schema/v1.13.json",
"runs": [
{
"name": "less context setting",
Expand Down
2 changes: 1 addition & 1 deletion examples/humaneval/empiricalrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://assets.empirical.run/config/schema/v1.12.json",
"$schema": "https://assets.empirical.run/config/schema/v1.13.json",
"runs": [
{
"type": "model",
Expand Down
2 changes: 1 addition & 1 deletion examples/rag/empiricalrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://assets.empirical.run/config/schema/v1.12.json",
"$schema": "https://assets.empirical.run/config/schema/v1.13.json",
"runs": [
{
"name": "rag script run",
Expand Down
2 changes: 1 addition & 1 deletion examples/rag/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
nest_asyncio.apply()


def execute(inputs):
def execute(inputs, parameters):
# load documents
question = inputs["question"]
reader = SimpleDirectoryReader("./arxiv-papers/", num_files_limit=30)
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ program
);
const completion = await Promise.all(
runs.map((r) => {
if (r.type === "py-script") {
r.pythonPath = options.pythonPath;
}
r.parameters = r.parameters ? r.parameters : {};
r.parameters.pythonPath = options.pythonPath;
return execute(r, dataset, () => {
progressBar.increment();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/python/executor_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
sys.path.append(sys.argv[1])
user_module = importlib.import_module(sys.argv[2])

result = user_module.execute(json.loads(sys.argv[3]))
result = user_module.execute(json.loads(sys.argv[3]), json.loads(sys.argv[4]))
print("execution_output:", json.dumps(result))
9 changes: 7 additions & 2 deletions packages/core/src/run/executors/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ export const scriptExecutor: Executor = async (runConfig, sample) => {

let basePath = path.dirname(scriptPath);
let moduleName = path.basename(scriptPath).replace(".py", "");
let pythonArgs = [basePath, moduleName, JSON.stringify(sample.inputs)];
let pythonArgs = [
basePath,
moduleName,
JSON.stringify(sample.inputs),
JSON.stringify(runConfig.parameters || {}),
];

const runOutput = await new Promise<string[]>((resolve) => {
let output: string[] = [];
const shell = new PythonShell(wrapperScriptFile, {
pythonPath: runConfig.pythonPath || undefined,
pythonPath: runConfig.parameters?.pythonPath || undefined,
scriptPath: wrapperScriptDirectory,
args: pythonArgs,
});
Expand Down
25 changes: 14 additions & 11 deletions packages/core/src/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import score from "@empiricalrun/scorer";
import { getExecutor } from "./executors";

export async function execute(
run: RunConfig,
runConfig: RunConfig,
dataset: Dataset,
progressCallback?: (sample: RunOutputSample) => void,
): Promise<RunCompletion> {
const runCreationDate = new Date();
const sampleCompletions: RunOutputSample[] = [];
const runId = generateHex(4);
const { scorers } = run;
const { scorers } = runConfig;
const completionsPromises = [];
for (const datasetSample of dataset.samples) {
const executor = getExecutor(run);
const executor = getExecutor(runConfig);
if (executor) {
// if llm error then add to the completion object but if something else throw error and stop the run
completionsPromises.push(
executor(run, datasetSample).then(({ output, error }) => {
executor(runConfig, datasetSample).then(({ output, error }) => {
const sample: RunOutputSample = {
inputs: datasetSample.inputs,
output,
Expand Down Expand Up @@ -65,7 +65,7 @@ export async function execute(
output: sampleCompletion.output,
scorers,
options: {
pythonPath: run.type === "py-script" ? run.pythonPath : undefined,
pythonPath: runConfig.parameters?.pythonPath,
},
}).then((scores) => {
sampleCompletion.scores = scores;
Expand All @@ -79,7 +79,10 @@ export async function execute(
}
return {
id: runId,
run_config: { ...run, name: run.name || getDefaultRunName(run, runId) },
run_config: {
...runConfig,
name: runConfig.name || getDefaultRunName(runConfig, runId),
},
dataset_config: {
id: dataset.id,
},
Expand All @@ -88,12 +91,12 @@ export async function execute(
};
}

function getDefaultRunName(run: RunConfig, id: string): string {
function getDefaultRunName(runConfig: RunConfig, id: string): string {
let name = "";
if (run.type === "model") {
name = run.model;
} else if (run.type === "py-script" || run.type === "js-script") {
name = run.path;
if (runConfig.type === "model") {
name = runConfig.model;
} else if (runConfig.type === "py-script" || runConfig.type === "js-script") {
name = runConfig.path;
}
return `Run #${id}: ${name}`;
}
8 changes: 7 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ interface RunConfigBase {
type: string;
name?: string;
scorers?: Scorer[];
parameters?: {
[key: string]: any;
};
}

export interface ModelRunConfig extends RunConfigBase {
Expand All @@ -62,7 +65,10 @@ export interface JSScriptRunConfig extends RunConfigBase {
export interface PyScriptRunConfig extends RunConfigBase {
type: "py-script";
path: string;
pythonPath?: string;
parameters?: {
pythonPath?: string;
[key: string]: any;
};
}

export type RunConfig = ModelRunConfig | PyScriptRunConfig | JSScriptRunConfig;
Expand Down

0 comments on commit 17860b0

Please sign in to comment.