Skip to content

Commit 4a6fbb3

Browse files
author
Jake Lazaroff
committed
Only use forevervm
1 parent 4556bfc commit 4a6fbb3

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/components/altair/Altair.tsx

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ const fvm = new ForeverVM({ token: await generateReplToken() });
2525

2626
const repl = fvm.repl();
2727

28+
const env = repl.exec(
29+
`import os
30+
31+
os.environ['AWS_ACCESS_KEY_ID'] = '${process.env.REACT_APP_AWS_ACCESS_KEY_ID}'
32+
os.environ['AWS_SECRET_ACCESS_KEY'] = '${process.env.REACT_APP_AWS_SECRET_ACCESS_KEY}'
33+
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
34+
`.trim(),
35+
);
36+
await env.result;
37+
2838
const renderAltair: FunctionDeclaration = {
2939
name: "render_altair",
3040
description: "Displays an altair graph in json format.",
@@ -41,10 +51,10 @@ const renderAltair: FunctionDeclaration = {
4151
},
4252
};
4353

44-
const runPython: FunctionDeclaration = {
45-
name: "run_python",
54+
const forevervm: FunctionDeclaration = {
55+
name: "forevervm",
4656
description:
47-
"Run Python code in a stateful read-eval-print loop. Variables, imports, and functions are persisted between calls. Imports of common packages, including requests, matplotlib, and pandas, are permitted, but you may not install libraries.",
57+
"Run Python code in a stateful read-eval-print loop. Variables, imports, and functions are persisted between calls. Imports of common packages, including requests, matplotlib, and pandas and boto3 are permitted, but you may not install libraries.",
4858
parameters: {
4959
type: SchemaType.OBJECT,
5060
properties: {
@@ -58,7 +68,8 @@ const runPython: FunctionDeclaration = {
5868
};
5969

6070
function AltairComponent() {
61-
const [jsonString, setJSONString] = useState<string>("");
71+
const [vegaSource, setVegaSource] = useState<string>("");
72+
const [imgSource, setImgSource] = useState<string>("");
6273
const { client, setConfig } = useLiveAPIContext();
6374

6475
useEffect(() => {
@@ -73,14 +84,14 @@ function AltairComponent() {
7384
systemInstruction: {
7485
parts: [
7586
{
76-
text: 'You are my helpful assistant. Any time I ask you for a graph call the "render_altair" function I have provided you. Dont ask for additional information just make your best judgement.',
87+
text: 'You are my helpful assistant. Any time I ask you to execute code, use the "forevervm" function I have provided you. You can display charts and graphs using matplotlib with plt.show(). Dont ask for additional information just make your best judgement.',
7788
},
7889
],
7990
},
8091
tools: [
8192
// there is a free-tier quota for search
8293
{ googleSearch: {} },
83-
{ functionDeclarations: [renderAltair, runPython] },
94+
{ functionDeclarations: [forevervm] },
8495
],
8596
});
8697
}, [setConfig]);
@@ -95,20 +106,20 @@ function AltairComponent() {
95106
switch (fc?.name) {
96107
case renderAltair.name: {
97108
const str = (fc.args as any).json_graph;
98-
setJSONString(str);
109+
setVegaSource(str);
99110
functionResponses.push({ id: fc.id, response: { output: { success: true } } });
100111
break;
101112
}
102113

103-
case runPython.name: {
114+
case forevervm.name: {
104115
const code = (fc.args as any).code;
105116

106117
const instruction = repl.exec(code);
107118

108119
let output: StandardOutput[] = [];
109120
for await (const line of instruction.output) output.push(line);
110-
111121
const result = await repl.exec(code).result;
122+
if (result.data?.png) setImgSource(result.data.png as string);
112123

113124
functionResponses.push({ id: fc.id, response: { output: { output, result } } });
114125
break;
@@ -130,10 +141,18 @@ function AltairComponent() {
130141
const embedRef = useRef<HTMLDivElement>(null);
131142

132143
useEffect(() => {
133-
if (embedRef.current && jsonString) {
134-
vegaEmbed(embedRef.current, JSON.parse(jsonString));
135-
}
136-
}, [embedRef, jsonString]);
144+
if (!embedRef.current || !vegaSource) return;
145+
vegaEmbed(embedRef.current, JSON.parse(vegaSource));
146+
}, [embedRef, vegaSource]);
147+
148+
useEffect(() => {
149+
if (!embedRef.current || !imgSource) return;
150+
151+
const img = document.createElement("img");
152+
img.src = "data:image/png;base64," + imgSource;
153+
embedRef.current.replaceChildren(img);
154+
}, [embedRef, imgSource]);
155+
137156
return <div className="vega-embed" ref={embedRef} />;
138157
}
139158

0 commit comments

Comments
 (0)