Skip to content

Commit

Permalink
feat: handle stream errors in ExamplePage
Browse files Browse the repository at this point in the history
Demonstrates how to use grafana/grafana-experimental#86
to handle errors occurring in a stream.
  • Loading branch information
sd2k committed Sep 27, 2023
1 parent efae477 commit cd06572
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/extensions/panelExplainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const ExplainPanelModal = ({ context }: ExplainPanelModalProps) => {
// Accumulate the stream chunks into a single string.
.pipe(llms.openai.accumulateContent())
// Subscribe to the stream and update the state for each returned value.
.subscribe(setStreamState);
.subscribe({
next: setStreamState,
error: console.error,
});
return { enabled: true };
});
if (state.loading || streamState === '') {
Expand Down
7 changes: 6 additions & 1 deletion src/pages/ExamplePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function ExamplePage() {
const [message, setMessage] = useState('');
// The latest reply from the LLM.
const [reply, setReply] = useState('');
const [streamError, setStreamError] = useState('');

const [useStream, setUseStream] = useState(false);

Expand Down Expand Up @@ -70,7 +71,10 @@ export function ExamplePage() {
// Subscribe to the stream and update the state for each returned value.
return {
enabled,
stream: stream.subscribe(setReply),
stream: stream.subscribe({
next: setReply,
error: (e) => setStreamError(String(e)),
}),
};
}
}, [message]);
Expand All @@ -96,6 +100,7 @@ export function ExamplePage() {
<div>{loading ? <Spinner /> : reply}</div>
<div>{started ? "Response is started" : "Response is not started"}</div>
<div>{finished ? "Response is finished" : "Response is not finished"}</div>
{streamError && <div>Error: {streamError}</div>}
</>
) : (
<div>LLM plugin not enabled.</div>
Expand Down

0 comments on commit cd06572

Please sign in to comment.