Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client and types for API v2 #19

Merged
merged 18 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,38 @@ npm install --save @vectara/stream-query-client
Then use it in your application like this:

```js
import {
streamQuery,
StreamUpdate,
StreamQueryConfig,
} from "@vectara/stream-query-client";
import { streamQueryV2, ApiV2 } from "@vectara/stream-query-client";

const sendQuery = async () => {
const configurationOptions: StreamQueryConfig = {
const configurationOptions: ApiV2.StreamQueryConfig = {
// Required fields.
customerId: "customerIdValue",
corpusIds: ["2"],
apiKey: "zqt_apiKeyValue",

// Optional fields.
queryValue: "Who put the ram in the ramalamadingdong?",
summaryNumResults: 5,
language: "eng",
debug: true,
enableFactualConsistencyScore: true,
summaryPromptName: "summary-prompt-name",
corpusKey: "corpora_1",
query: "How many coconuts can an African swallow carry?",
search: {
offset: 0,
limit: 5,
metadataFilter: "",
},
generation: {
maxUsedSearchResults: 5,
responseLanguage: "eng",
enableFactualConsistencyScore: true,
promptName: "summary-prompt-name",
},
};

const onStreamUpdate = (update: StreamUpdate) => {
const onStreamUpdate = (update: ApiV2.StreamUpdate) => {
// Perform operations on returned data, e.g. update state.
console.log(update.updatedText);
};

streamQuery(configurationOptions, onStreamUpdate);
streamQueryV2(configurationOptions, onStreamUpdate);
};
```

For more information on configuration options and callback types, see [the type definitions](src/types.ts) and our [Query API documentation](https://docs.vectara.com/docs/api-reference/search-apis/stream-query).
For more information on configuration options and callback types, see [the type definitions](src/apiV2/types.ts) and our [Query API documentation](https://docs.vectara.com/docs/rest-api/query).

## License

Expand Down
2 changes: 1 addition & 1 deletion docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 110 additions & 26 deletions docs/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,152 @@
import { useState } from "react";
import ReactDOM from "react-dom";
import {
streamQuery,
StreamUpdate,
StreamQueryConfig,
streamQueryV1,
ApiV1,
streamQueryV2,
ApiV2,
} from "@vectara/stream-query-client";

// TODO: Switch back to prod values before merging
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to switch this back before merging.

const CUSTOMER_ID = "3099635174";
const API_KEY = "zqt_uMCt5uGR7CXARu7QHg7GDYNG5Q5v58HOpvQO0A";
const CORPUS_NAME = "markdown";
const CORPUS_ID = "203";

// const CUSTOMER_ID = "1526022105";
// const API_KEY = "zqt_WvU_2ewh7ZGRwq8LdL2SV8B9RJmVGyUm1VAuOw";
// const CORPUS_NAME = "ofer-bm-moma-docs";
// const CORPUS_ID = "232";

const App = () => {
const [question, setQuestion] = useState("");
const [answer, setAnswer] = useState<string>();
const [conversationId, setConversationId] = useState<string>();
const [question, setQuestion] = useState("markdown");
const [answerV1, setAnswerV1] = useState<string>();
const [resultsV1, setResultsV1] = useState<string>();
const [answerV2, setAnswerV2] = useState<string>();
const [resultsV2, setResultsV2] = useState<string>();
const [conversationIdV1, setConversationIdV1] = useState<string>();
const [conversationIdV2, setConversationIdV2] = useState<string>();

const sendQuery = async () => {
const configurationOptions: StreamQueryConfig = {
const sendQueryV1 = async () => {
const configurationOptions: ApiV1.StreamQueryConfig = {
// Required fields.
customerId: "1366999410",
corpusIds: ["1"],
apiKey: "zqt_UXrBcnI2UXINZkrv4g1tQPhzj02vfdtqYJIDiA",
customerId: CUSTOMER_ID,
corpusIds: [CORPUS_ID],
apiKey: API_KEY,

// Optional fields.
queryValue: question,
summaryNumResults: 5,
summaryNumResults: 2,
language: "eng",
chat: {
store: true,
conversationId,
conversationId: conversationIdV1,
},
debug: true,
enableFactualConsistencyScore: true,
summaryPromptName: "vectara-experimental-summary-ext-2023-12-11-large",
};

const onStreamUpdate = (update: StreamUpdate) => {
console.log(update);
const { updatedText, details } = update;
if (details.chat) {
setConversationId(details.chat.conversationId);
const onStreamUpdate = (update: ApiV1.StreamUpdate) => {
// console.log("v1", update);
const { updatedText, details, references } = update;

if (details?.chat) {
setConversationIdV1(details.chat.conversationId);
}

setAnswerV1(updatedText);

if (references) {
setResultsV1(JSON.stringify(references));
}
setAnswer(updatedText);
};

streamQuery(configurationOptions, onStreamUpdate);
streamQueryV1(configurationOptions, onStreamUpdate);
};

const sendQueryV2 = async () => {
const configurationOptions: ApiV2.StreamQueryConfig = {
customerId: CUSTOMER_ID,
apiKey: API_KEY,
query: question,
corpusKey: `${CORPUS_NAME}_${CORPUS_ID}`,
search: {
offset: 0,
metadataFilter: "",
limit: 2,
lexicalInterpolation: 0,
contextConfiguration: {
sentencesBefore: 2,
sentencesAfter: 2,
},
},
generation: {
maxUsedSearchResults: 5,
responseLanguage: "eng",
enableFactualConsistencyScore: true,
promptName: "vectara-experimental-summary-ext-2023-12-11-large",
},
chat: {
store: true,
conversationId: conversationIdV2,
},
};

const onStreamEvent = (event: ApiV2.StreamEvent) => {
const { updatedText, chatId, searchResults } = event;
if (chatId) {
setConversationIdV2(chatId);
}

if (updatedText) {
setAnswerV2(updatedText);
}

if (searchResults) {
setResultsV2(JSON.stringify(searchResults));
}

if (event.type === "error") {
console.log("Error", event.messages);
}
};

streamQueryV2(configurationOptions, onStreamEvent);
};

return (
<>
<h1>Stream Query Client</h1>

<h2>Question</h2>

<input value={question} onChange={(e) => setQuestion(e.target.value)} />

<button
onClick={() => {
setAnswer("");
sendQuery();
setAnswerV1("");
setResultsV1("");
setAnswerV2("");
setResultsV2("");
sendQueryV1();
sendQueryV2();
}}
>
Send
</button>

<h2>Answer</h2>
<div style={{ display: "flex" }}>
<div style={{ flexGrow: "1", flexShrink: "1", width: "50%" }}>
<h2>Stream Query Client v1 answer</h2>
<p>{resultsV1}</p>
<p>{answerV1}</p>
</div>

<p>{answer}</p>
<div style={{ flexGrow: "1", flexShrink: "1", width: "50%" }}>
<h2>Stream Query Client v2 answer</h2>
<p>{resultsV2}</p>
<p>{answerV2}</p>
</div>
</div>
</>
);
};
Expand Down
File renamed without changes.
21 changes: 15 additions & 6 deletions src/index.test.ts → src/apiV1/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { SetupServerApi } from "msw/node";
import { streamQuery, StreamUpdate, StreamQueryConfig } from "./index";
import { createStreamingServer } from "./createStreamingServer";
import { chunks } from "./index.mocks";
import { streamQueryV1 } from "./client";
import { StreamQueryConfig, StreamUpdate } from "./types";
import { createTestStreamingServer } from "../common/createTestStreamingServer";
import { chunks } from "./client.mocks";

describe("stream-query-client", () => {
const encoder = new TextEncoder();

describe("stream-query-client API v1", () => {
let server: SetupServerApi;

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

Expand Down Expand Up @@ -44,7 +53,7 @@ describe("stream-query-client", () => {
handleUpdate(update);
};

await streamQuery(configurationOptions, onStreamUpdate);
await streamQueryV1(configurationOptions, onStreamUpdate);

expect(handleUpdate).toHaveBeenNthCalledWith(1, {
references: [
Expand Down
Loading