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

Add client and types for API v2 #19

merged 18 commits into from
Jun 7, 2024

Conversation

cjcenizal
Copy link
Contributor

@cjcenizal cjcenizal commented Jun 5, 2024

Summary

The API v2 client is unopinionated. It accepts configuration options that are a 1:1 representation of the API configuration options. It also doesn't preprocess the results -- that's left to the consumer. This will make it easier for folks who are familiar with the API or reading the API docs to map concepts to the client.

To-do

  • Enable using a JWT instead of an API key.

Bug fixes

  • Events might be very large, e.g. many or large search results, which causes them to be split across multiple chunks. Original code didn't accommodate for this, and risked a JSON.parse error. The code now buffers these chunks until it detects the completion of the event (indicated by a successful JSON.parse call), at which point it processes the buffered event.

New features

  • Errors are emitted as regular events to the onStreamEvent handler.
  • Calling streamQueryV2 returns { cancelStream } which can be used to cancel the stream.

Breaking changes

Package interface is now:

import {
  streamQueryV1,
  ApiV1,
  streamQueryV2,
  ApiV2,
} from "@vectara/stream-query-client";

The API has been dramatically simplified and ergonomics much improved, so I don't think we need to inject any opinions or sugar into the interface. As a result, requests are now configured with the same objects and fields as described in the Corpus Query docs.

StreamEvent now has this shape:

export type StreamEvent =
  | ErrorEvent
  | SearchResultsEvent
  | ChatInfoEvent
  | GenerationChunkEvent
  | FactualConsistencyScoreEvent
  | EndEvent;

export type ErrorEvent = {
  type: "error";
  messages?: string[];
};

export type SearchResultsEvent = {
  type: "searchResults";
  searchResults: SearchResult[];
};

export type ChatInfoEvent = {
  type: "chatInfo";
  chatId: string;
  turnId: string;
};

export type GenerationChunkEvent = {
  type: "generationChunk";
  updatedText: string;
  generationChunk: string;
};

export type FactualConsistencyScoreEvent = {
  type: "factualConsistencyScore";
  factualConsistencyScore: number;
};

export type EndEvent = {
  type: "end";
};

References

https://javascript.info/fetch-abort
whatwg/streams#1255
https://github.com/openai/openai-node/blob/master/src/streaming.ts

@cjcenizal cjcenizal force-pushed the api-v2 branch 4 times, most recently from b706359 to 3607435 Compare June 6, 2024 16:23
} 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.

@@ -0,0 +1,2 @@
// 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.

Copy link
Contributor

@mrderyk mrderyk left a comment

Choose a reason for hiding this comment

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

Quick request to update the types.

src/apiV2/types.ts Show resolved Hide resolved
@cjcenizal cjcenizal merged commit d7ad2e6 into main Jun 7, 2024
@cjcenizal cjcenizal deleted the api-v2 branch June 7, 2024 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants