Skip to content

Commit

Permalink
Add node.js code samples for all AutoEval docs
Browse files Browse the repository at this point in the history
  • Loading branch information
saqadri committed Dec 24, 2024
1 parent abd4221 commit 200fd4b
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 9 deletions.
20 changes: 19 additions & 1 deletion website/docs/api-keys.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export LASTMILE_API_TOKEN="your_api_key"
```

</TabItem>
<TabItem value="python">
<TabItem value="python">

```python
from lastmile import Lastmile
Expand All @@ -41,6 +41,24 @@ client = Lastmile(
)
```

</TabItem>
<TabItem value="node.js">

```typescript
import { Lastmile } from 'lastmile';
import { AutoEval } from "lastmile/lib/auto_eval";

// Recommended: AutoEval client SDK (higher-level APIs)
const client = new AutoEval({
apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set"
});

// Lastmile client (REST API wrappers)
const client2 = new Lastmile({
bearerToken: "api_token_if_LASTMILE_API_TOKEN_not_set",
})
```

</TabItem>
</Tabs>

Expand Down
46 changes: 46 additions & 0 deletions website/docs/autoeval/datasets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ dataset_id = client.upload_dataset(
)

print(dataset_id)
```

</TabItem>
<TabItem value="node.js">

```typescript
import { AutoEval } from "lastmile/lib/auto_eval";

const client = new AutoEval({ apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set" });
const datasetCSV = "path_to_dataset.csv"
const datasetId = await client.uploadDataset({
filePath: datasetCSV,
name: "My New Dataset",
description: "This Dataset is the latest batch of application trace data"
})

console.log(datasetId)
```

</TabItem>
Expand Down Expand Up @@ -84,6 +101,21 @@ dataset_df = client.download_dataset(
)

print(dataset_df.head(5))
```

</TabItem>
<TabItem value="node.js">

```typescript
import { AutoEval } from "lastmile/lib/auto_eval";

const client = new AutoEval({ apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set" });
const data = await client.downloadDataset(
datasetId,
/*outputFilePath*/ "optional_path_to_save_file"
);

console.table(data)
```

</TabItem>
Expand Down Expand Up @@ -112,5 +144,19 @@ for dataset in datasets:
print(f"Dataset ID: {dataset['id']}, Name: {dataset['name']}")
```

</TabItem>
<TabItem value="node.js">

```typescript
import { AutoEval } from "lastmile/lib/auto_eval";

const client = new AutoEval({ apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set" });
const datasets = await client.listDatasets();

for (const dataset of datasets) {
console.log(`Dataset ID: ${dataset.id}, Name: ${dataset.name}`);
}
```

</TabItem>
</Tabs>
56 changes: 56 additions & 0 deletions website/docs/autoeval/fine-tuning.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ fine_tune_job_id = client.fine_tune_model(
print(f"Fine-tuning job initiated with ID: {fine_tune_job_id}. Waiting for completion...")
client.wait_for_fine_tune_job(fine_tune_job_id)
print(f"Fine-tuning job completed with ID: {fine_tune_job_id}")
```

</TabItem>
<TabItem value="node.js">

```typescript title="fine_tune"
import { AutoEval } from "lastmile/lib/auto_eval";
const client = new AutoEval({
apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set",
});
const modelName = "My Custom Evaluation Metric";
const fineTuningJobId = await client.fineTuneModel({
trainDatasetId: datasetId,
modelName,
selectedColumns: ["input", "output", "ground_truth"],
waitForCompletion: false, // Set to true to block until completion
});

console.log(`Fine-tuning job initiated with ID: ${fineTuningJobId}. Waiting for completion...`);
await client.waitForFineTuneJob(fineTuningJobId);
console.log(`Fine-tuning job completed with ID: ${fineTuningJobId}`);
```

</TabItem>
Expand Down Expand Up @@ -168,6 +189,41 @@ eval_results_df = client.evaluate_data(
}),
metrics=[fine_tuned_metric],
)
```

</TabItem>
<TabItem value="node.js">

```typescript title="run_inference"
import { AutoEval, Metric } from "lastmile/lib/auto_eval";
const client = new AutoEval({
apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set",
});

const metric: Metric = {
name: "My Custom Evaluation Metric",
};

console.log(`Waiting for fine-tuned model '${metric.name}' to be available as a metric...`);
const fineTunedMetric = await client.waitForMetricOnline(metric);
console.log(`Fine-tuned model '${metric.name}' is now available as a metric with ID: ${fineTunedMetric.id}.`);

// Run evals on your test/holdout dataset to see how the model is performing
const testResults = await client.evaluateDataset(testDatasetId, fineTunedMetric);
console.table(testResults);

// Run evals on any application data
const results = await client.evaluateData(
/*data*/ [
{
"input": "What is the meaning of life?",
"output": "42",
"ground_truth": "Life, universe and everything"
}
],
/*metrics*/ [fineTunedMetric]
);
console.table(results);
```

</TabItem>
Expand Down
49 changes: 49 additions & 0 deletions website/docs/autoeval/guardrails.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,55 @@ guard(
)
```

</TabItem>
<TabItem value="node.js">

```typescript
import { AutoEval, Metric } from "lastmile/lib/auto_eval";

async function guard(
input: string,
output: string,
context: string,
metric: Metric,
threshold: number = 0.5
): Promise<boolean> {
const client = new AutoEval({
apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set",
});

// Evaluate the data
const result = await client.evaluateData(
/*data*/ [
{
"input": input,
"output": output,
"ground_truth": context
}
],
metric
);

// Extract the score
const scoreColumnName = `${metric.name}_score`;
const score = result[0][scoreColumnName];

// Return whether the score meets the threshold
return score >= threshold;
}

const faithfulnessMetric: Metric = { name: "Faithfulness" };

const isFaithful = await guard(
/*input*/ "Where did the author grow up?",
/*output*/ "France",
/*context*/ "England",
faithfulnessMetric
);

console.log(`Is the response faithful? ${isFaithful}`);
```

</TabItem>
</Tabs>

Expand Down
24 changes: 24 additions & 0 deletions website/docs/autoeval/labeling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ client.wait_for_label_dataset_job(job_id)
labeled_dataset = client.download_dataset(dataset_id)

print(f"Labeling Job with ID: {job_id} Completed")
```

</TabItem>
<TabItem value="node.js">

```typescript title="label_dataset"
import { AutoEval, BuiltinMetrics } from "lastmile/lib/auto_eval";

const client = new AutoEval({
apiKey: "api_token_if_LASTMILE_API_TOKEN_not_set",
});

const jobId = await client.labelDataset({
datasetId,
promptTemplate: BuiltinMetrics.FAITHFULNESS, // Or a custom evaluation prompt criteria
waitForCompletion: false, // Set to true to wait for the job to complete
});

console.log(`Waiting for labeling job ${jobId} to complete...`);
await client.waitForLabelDatasetJob(jobId);
console.log(`Labeling Job with ID: ${jobId} Completed`);
const labeledData = client.downloadDataset(datasetId);

console.table(labeledData);
```

</TabItem>
Expand Down
Loading

0 comments on commit 200fd4b

Please sign in to comment.