Skip to content

Commit 44cf7cc

Browse files
kamathkwt00
andauthored
Adding Cerebras inference support (#502) (#523)
* Adding Cerebras inference support (#502) * Added support for Cerebras models Full function calling support for both llama-3.3-70b and llama-3.1-8b models (llama 3.1 8b struggles with tools) * Added higher performance model as default for example * Changed Cerebras model prefixes to include provider * Update CerebrasClient.ts * Update LLMProvider.ts * Update LLMClient.ts * Update model.ts * Update LLMClient.ts * Update LLMProvider.ts * Update LLMProvider.ts --------- Co-authored-by: Anirudh Kamath <[email protected]> * changeset --------- Co-authored-by: kwt00 <[email protected]>
1 parent d7a6108 commit 44cf7cc

File tree

7 files changed

+459
-7
lines changed

7 files changed

+459
-7
lines changed

Diff for: .changeset/young-insects-look.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@browserbasehq/stagehand": minor
3+
---
4+
5+
Added CerebrasClient.ts:
6+
7+
- Handles API communication with Cerebras
8+
- Supports function calling
9+
- Includes robust response parsing with fallbacks
10+
- Handles caching and error logging
11+
- Infrastructure Changes:
12+
13+
Updated LLMClient.ts to support Cerebras provider type:
14+
15+
- Added Cerebras models to model.ts type definitions
16+
- Added Cerebras case to LLMProvider.ts
17+
- Added CEREBRAS_API_KEY to environment variables

Diff for: .env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
OPENAI_API_KEY=""
2+
CEREBRAS_API_KEY=""
23
BROWSERBASE_API_KEY=""
34
BRAINTRUST_API_KEY=""
45
ANTHROPIC_API_KEY=""
@@ -7,4 +8,4 @@ ENABLE_CACHING=false
78
EVAL_MODELS="gpt-4o,claude-3-5-sonnet-latest"
89
EXPERIMENTAL_EVAL_MODELS="gpt-4o,claude-3-5-sonnet-latest,o1-mini,o1-preview"
910
EVAL_CATEGORIES="observe,act,combination,extract,experimental"
10-
STAGEHAND_API_URL="http://localhost:80"
11+
STAGEHAND_API_URL="http://localhost:80"

Diff for: examples/form_filling_sensible_cerebras.ts

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { Stagehand } from "@/dist";
2+
import StagehandConfig from "@/stagehand.config";
3+
import { CerebrasClient } from "../lib/llm/CerebrasClient";
4+
import chalk from "chalk";
5+
6+
async function formFillingSensible() {
7+
const stagehand = new Stagehand({
8+
...StagehandConfig,
9+
env: "LOCAL",
10+
llmClient: new CerebrasClient({
11+
modelName: "cerebras-llama-3.3-70b",
12+
clientOptions: {
13+
apiKey: process.env.CEREBRAS_API_KEY,
14+
},
15+
logger: console.log,
16+
}),
17+
});
18+
await stagehand.init();
19+
20+
// Block manifest worker to prevent PWA installation popup.
21+
// This is necessary because the website prompts the user to install the PWA and prevents form filling.
22+
await stagehand.page.route("**/manifest.json", (route) => route.abort());
23+
24+
// Go to the website and wait for it to load
25+
await stagehand.page.goto("https://file.1040.com/estimate/", {
26+
waitUntil: "networkidle",
27+
timeout: 30000,
28+
});
29+
30+
// Observe the form fields with suggested actions
31+
const observed = await stagehand.page.observe({
32+
instruction:
33+
"fill all the form fields in the page with mock data. In the description inlcude the field name",
34+
returnAction: true,
35+
});
36+
37+
// Uncomment the following snippet to see the stagehand candidate suggestions (initial)
38+
console.log(
39+
`${chalk.green("Observe:")} Form fields found:\n${observed
40+
.map((r) => `${chalk.yellow(r.description)} -> ${chalk.gray(r.selector)}`)
41+
.join("\n")}`,
42+
);
43+
44+
// Create a mapping of 1+ keywords in the form fields to standardize field names
45+
const mapping = (description: string): string | null => {
46+
const keywords: { [key: string]: string[] } = {
47+
age: ["old"],
48+
dependentsUnder17: ["under age 17", "child", "minor"],
49+
dependents17to23: ["17-23", "school", "student"],
50+
wages: ["wages", "W-2 Box 1"],
51+
federalTax: ["federal tax", "Box 2"],
52+
stateTax: ["state tax", "Box 17"],
53+
};
54+
55+
for (const [key, terms] of Object.entries(keywords)) {
56+
if (terms.some((term) => description.toLowerCase().includes(term))) {
57+
return key;
58+
}
59+
}
60+
return null;
61+
};
62+
63+
// Fill the form fields with sensible data. This data will only be used in your session and not be shared with LLM providers/external APIs.
64+
const userInputs: { [key: string]: string } = {
65+
age: "26",
66+
dependentsUnder17: "1",
67+
wages: "54321",
68+
federalTax: "8345",
69+
stateTax: "2222",
70+
};
71+
72+
const updatedFields = observed.map((candidate) => {
73+
const key = mapping(candidate.description);
74+
if (key && userInputs[key]) {
75+
candidate.arguments = [userInputs[key]];
76+
}
77+
return candidate;
78+
});
79+
// List of sensible-data candidates
80+
console.log(
81+
`\n${chalk.green("Sensible Data form inputs:")} Form fields to be filled:\n${updatedFields
82+
.map(
83+
(r) =>
84+
`${chalk.yellow(r.description)} -> ${chalk.blue(r.arguments?.[0] || "no value")}`,
85+
)
86+
.join("\n")}`,
87+
);
88+
89+
// Fill all the form fields with the sensible candidates
90+
for (const candidate of updatedFields) {
91+
await stagehand.page.act(candidate);
92+
}
93+
}
94+
95+
(async () => {
96+
await formFillingSensible();
97+
})();

0 commit comments

Comments
 (0)