Skip to content

Commit

Permalink
Add some more error handling to RAG UI Modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Xantier committed Oct 3, 2024
1 parent 7ea5c6e commit 74c738f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-vans-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/rag-ai': patch
---

Add some error handling for better messages on RAG AI modal.
55 changes: 36 additions & 19 deletions plugins/frontend/rag-ai/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,43 @@ export class RoadieRagAiClient implements RagAiApi {
async *ask(question: string, source: string): AsyncGenerator<ParsedEvent> {
const { token } = await this.identityApi.getCredentials();

const stream = await this.fetch(`query/${source}`, {
body: JSON.stringify({
query: question,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
try {
const stream = await this.fetch(`query/${source}`, {
body: JSON.stringify({
query: question,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
if (stream) {
const reader = stream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new EventSourceParserStream())
.getReader();

const reader = stream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new EventSourceParserStream())
.getReader();

while (true) {
const { done, value } = await reader.read();
if (done) break;
yield value;
while (true) {
const { done, value } = await reader.read();
if (done) break;
yield value;
}
} else {
yield {
data: 'No response received from the LLM',
event: 'message',
type: 'event',
};
}
} catch (e: any) {
// eslint-disable-next-line
console.error(e.message);
yield {
data: `Failed to receive response from the backend endpoint.`,
event: 'error',
type: 'event',
};
}
}
}
10 changes: 9 additions & 1 deletion plugins/frontend/rag-ai/src/components/RagModal/RagModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ragAiApiRef } from '../../api';
import { useApi } from '@backstage/core-plugin-api';
import { ResponseEmbedding } from '../../types';
import { Thinking } from './Thinking';
import { WarningPanel } from '@backstage/core-components';

export type RagModalProps = {
title?: string;
Expand Down Expand Up @@ -88,11 +89,13 @@ export const ControlledRagModal = ({
const [thinking, setThinking] = useState(false);
const [questionResult, setQuestionResult] = useState('');
const [embeddings, setEmbeddings] = useState<ResponseEmbedding[]>([]);
const [warning, setWarning] = useState<string | undefined>();
const ragApi = useApi(ragAiApiRef);
const askLlm = useCallback(
async (question: string, source: string) => {
setThinking(true);
setQuestionResult('');
setWarning(undefined);
setEmbeddings([]);

for await (const chunk of ragApi.ask(question, source)) {
Expand All @@ -105,6 +108,10 @@ export const ControlledRagModal = ({
setEmbeddings(JSON.parse(chunk.data));
break;
}
case 'error': {
setWarning(chunk.data);
break;
}
default:
throw new Error(`Unknown event type: ${chunk.event}`);
}
Expand Down Expand Up @@ -148,7 +155,8 @@ export const ControlledRagModal = ({
}}
/>
</Box>
{thinking && !questionResult ? (
{warning && <WarningPanel severity="warning" message={warning} />}
{thinking && !questionResult && !warning ? (
<Box p={6} display="flex" justifyContent="center" alignItems="center">
<Thinking />
</Box>
Expand Down

0 comments on commit 74c738f

Please sign in to comment.