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 df0f99f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/extensions/panelExplainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ const ExplainPanelModal = ({ context }: ExplainPanelModalProps) => {
]
})
// Accumulate the stream chunks into a single string.
.pipe(llms.openai.accumulateContent())
.pipe(
llms.openai.handleError(setStreamState),
llms.openai.accumulateContent()
)
// Subscribe to the stream and update the state for each returned value.
.subscribe(setStreamState);
return { enabled: true };
Expand Down
4 changes: 4 additions & 0 deletions 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 @@ -54,6 +55,8 @@ export function ExamplePage() {
{ role: 'user', content: message },
],
}).pipe(
// Set the error message, if one occurs.
llms.openai.handleError(setStreamError),
// Accumulate the stream content into a stream of strings, where each
// element contains the accumulated message so far.
llms.openai.accumulateContent(),
Expand Down Expand Up @@ -96,6 +99,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 df0f99f

Please sign in to comment.