Skip to content

Commit

Permalink
Fix StreamQueryClient handling of HTTP errors and add support for Non…
Browse files Browse the repository at this point in the history
…e reranker and GenerationInfo event. (#24)

* Add prettier rules.
  • Loading branch information
cjcenizal authored Jul 12, 2024
1 parent d4c1e85 commit 066bb88
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 212 deletions.
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"printWidth": 120,
"trailingComma": "none"
}
1 change: 1 addition & 0 deletions docs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const App = () => {
sentencesBefore: 2,
sentencesAfter: 2,
},
reranker: "none",
},
generation: {
maxUsedSearchResults: 5,
Expand Down
10 changes: 5 additions & 5 deletions src/apiV1/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ describe("stream-query-client API v1", () => {
let server: SetupServerApi;

beforeAll(async () => {
server = createTestStreamingServer(
"/v1/stream-query",
server = createTestStreamingServer({
path: "/v1/stream-query",
chunks,
(json: any) => {
createChunk: (json: any) => {
return encoder.encode(JSON.stringify(json));
}
);
},
});
await server.listen();
});

Expand Down
14 changes: 7 additions & 7 deletions src/apiV2/EventBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ data:{"type":"end"}
expect(onStreamEvent).toHaveBeenNthCalledWith(1, {
type: "error",
messages: [
"INVALID_ARGUMENT: The filter expression contains an error. Syntax error at 1:0 nc79bc8s must be referenced as doc.nc79bc8s or part.nc79bc8s",
],
"INVALID_ARGUMENT: The filter expression contains an error. Syntax error at 1:0 nc79bc8s must be referenced as doc.nc79bc8s or part.nc79bc8s"
]
});

expect(onStreamEvent).toHaveBeenNthCalledWith(2, { type: "end" });
Expand All @@ -42,7 +42,7 @@ data:{"type":"search_results",

expect(onStreamEvent).toHaveBeenCalledWith({
type: "searchResults",
searchResults: [{ id: "doc1" }],
searchResults: [{ id: "doc1" }]
});
});

Expand All @@ -63,8 +63,8 @@ data:{"type":"end"}
expect(onStreamEvent).toHaveBeenNthCalledWith(1, {
type: "error",
messages: [
"INVALID_ARGUMENT: The filter expression contains an error. Syntax error at 1:0 nc79bc8s must be referenced as doc.nc79bc8s or part.nc79bc8s",
],
"INVALID_ARGUMENT: The filter expression contains an error. Syntax error at 1:0 nc79bc8s must be referenced as doc.nc79bc8s or part.nc79bc8s"
]
});

expect(onStreamEvent).toHaveBeenNthCalledWith(2, { type: "end" });
Expand All @@ -82,7 +82,7 @@ data:{"type":"end"}
type: "unexpectedError",
raw: `
{"messages":["Request failed. See https://status.vectara.com for the latest info on any outages. If the problem persists, please contact us via support or via our community forums at https://discuss.vectara.com if you’re a Growth user."],"request_id":"00000000000000000000000000000000"}
`,
`
});
});

Expand All @@ -98,7 +98,7 @@ data:{"type":"apocalypse"}
expect(onStreamEvent).toHaveBeenCalledWith({
type: "unexpectedEvent",
rawType: "meteor_strike",
raw: { type: "apocalypse" },
raw: { type: "apocalypse" }
});
});
});
45 changes: 28 additions & 17 deletions src/apiV2/EventBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export class EventBuffer {
private eventInProgress = "";
private updatedText = "";

constructor(
onStreamEvent: (event: any) => void,
includeRaw = false,
status = 200
) {
constructor(onStreamEvent: (event: any) => void, includeRaw = false, status = 200) {
this.events = [];
this.onStreamEvent = onStreamEvent;
this.includeRaw = includeRaw;
Expand Down Expand Up @@ -44,8 +40,12 @@ export class EventBuffer {
const rawEvent = JSON.parse(this.eventInProgress);
this.enqueueEvent(rawEvent);
this.eventInProgress = "";
} catch {
// @tes-expect-error no-empty
} catch (error: any) {
const isJsonError = error.stack.includes("at JSON.parse");
// Silently ignore JSON parsing errors, as they are expected.
if (!isJsonError) {
console.error(error);
}
}
});

Expand All @@ -61,22 +61,24 @@ export class EventBuffer {
turn_id,
factual_consistency_score,
generation_chunk,
rendered_prompt,
rephrased_query
} = rawEvent;

switch (type) {
case "error":
this.events.push({
type: "error",
messages,
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

case "search_results":
this.events.push({
type: "searchResults",
searchResults: search_results,
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

Expand All @@ -85,7 +87,7 @@ export class EventBuffer {
type: "chatInfo",
chatId: chat_id,
turnId: turn_id,
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

Expand All @@ -95,29 +97,38 @@ export class EventBuffer {
type: "generationChunk",
updatedText: this.updatedText,
generationChunk: generation_chunk,
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

case "generation_info":
this.events.push({
type: "generationInfo",
renderedPrompt: rendered_prompt,
rephrasedQuery: rephrased_query,
...(this.includeRaw && { raw: rawEvent })
});
break;

case "generation_end":
this.events.push({
type: "generationEnd",
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

case "factual_consistency_score":
this.events.push({
type: "factualConsistencyScore",
factualConsistencyScore: factual_consistency_score,
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

case "end":
this.events.push({
type: "end",
...(this.includeRaw && { raw: rawEvent }),
...(this.includeRaw && { raw: rawEvent })
});
break;

Expand All @@ -126,20 +137,20 @@ export class EventBuffer {
this.events.push({
type: "unexpectedEvent",
rawType: type,
raw: rawEvent,
raw: rawEvent
});
} else if (this.status !== 200) {
// Assume an error.
this.events.push({
type: "requestError",
status: this.status,
raw: rawEvent,
raw: rawEvent
});
} else {
// Assume an error.
this.events.push({
type: "unexpectedError",
raw: rawEvent,
raw: rawEvent
});
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/apiV2/apiTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SummaryLanguage } from "../common/types";

export namespace Query {
export type NoneReranker = { type: "none" };

export type CustomerSpecificReranker = {
type: "customer_reranker";
reranker_id: string;
Expand Down Expand Up @@ -29,7 +31,7 @@ export namespace Query {
start_tag?: string;
end_tag?: string;
};
reranker?: CustomerSpecificReranker | MmrReranker;
reranker?: NoneReranker | CustomerSpecificReranker | MmrReranker;
};

export type NoneCitations = {
Expand Down Expand Up @@ -64,11 +66,7 @@ export namespace Query {
frequency_penalty: number;
presence_penalty: number;
};
citations?:
| NoneCitations
| NumericCitations
| HtmlCitations
| MarkdownCitations;
citations?: NoneCitations | NumericCitations | HtmlCitations | MarkdownCitations;
enable_factual_consistency_score?: boolean;
};

Expand Down
10 changes: 7 additions & 3 deletions src/apiV2/client.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ data:{"type":"chat_info","chat_id":"cht_74b5a5f3-1f51-4427-a317-f62efb493928","t
const chunk3 = `event:generation_chunk
data:{"type":"generation_chunk","generation_chunk":"Markdown is "}`;

// Generation info.
const chunk4 = `event:generation_info
data:{"type":"generation_info","rephrased_query":"Rephrased query","rendered_prompt":"Rendered prompt"}`;

// FCS.
const chunk4 = `event:factual_consistency_score
const chunk5 = `event:factual_consistency_score
data:{"type":"factual_consistency_score","factual_consistency_score":0.41796625}`;

// // End.
const chunk5 = `event:end
const chunk6 = `event:end
data:{"type":"end"}`;

export const chunks = [chunk1, chunk2, chunk3, chunk4, chunk5];
export const chunks = [chunk1, chunk2, chunk3, chunk4, chunk5, chunk6];
Loading

0 comments on commit 066bb88

Please sign in to comment.