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

feat: Adding ability to attach 'annotations' (custom metadata) to messages #879

Merged
merged 8 commits into from
Jan 30, 2024

Conversation

nick-inkeep
Copy link
Contributor

@nick-inkeep nick-inkeep commented Dec 21, 2023

Scenario

Many developers would like to add domain or provider-specific information to a given message. For example, for Inkeep we stream information about the sources used to answer a question at the end of the event stream. This might be used by a client to, for example, show a "Sources" section at the end of a message in the UI:

image

Dependency for:
#861 (adding Inkeep as a provider)

Resolves:
#585
#425 (comment)

Implementation

  • add a new 'message_annotations' stream-part
  • add appendMessageAnnotation(n) function to data (experimental_StreamData) object
  • pass the event type to a CustomParser so streams can handle non-homogeneous server-side event. For example, so that the Inkeep stream provider can parse the onRecordsCited event type that's unique to it's SSE and has the information to append to a message.
  • accessible in the client as message.annotations[n]

adding onEvent callback to ai-stream

fixing streaming part typo

removing onevent

adding optional event param to AIStreamParser

passing the extra arg in the transformer for event type
@nick-inkeep nick-inkeep marked this pull request as ready for review December 21, 2023 20:43
@lgrammel
Copy link
Collaborator

@nick-inkeep this looks great. To help me understand it better, could you provide an example of how to use it, or ideally add it to the next-openai examples?

Updates to the documentation and a test case for the full streaming (e.g. add to packages/core/streams/openai-stream.test.ts) would be super helpful.

@nick-inkeep
Copy link
Contributor Author

nick-inkeep commented Jan 26, 2024

@lgrammel few things as discussed:

  1. renamed to 'message_annotations'. Updated references and added that to the client Message type.
  2. added testing for parse-complex-response.ts

As an example, I'm not sure how to construe an example with OpenAI, thought it might be best to illustrate the use with the corresponding PR for the Inkeep provider. See:

  1. use of appendMessageAnnotations in the /route here.
  2. rendering the Inkeep citations in the UI for the base next-inkeep example here.

Screen recording of the citations in use in that example.

Copy link
Collaborator

@lgrammel lgrammel left a comment

Choose a reason for hiding this comment

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

Looks great, had just one small comment.

@lgrammel lgrammel requested a review from MaxLeiter January 26, 2024 09:11
@nick-inkeep nick-inkeep changed the title feat: Adding ability to attach 'data' to messages feat: Adding ability to attach 'annotations' (custom metadata) to messages Jan 26, 2024
@MaxLeiter MaxLeiter merged commit 7851fa0 into vercel:main Jan 30, 2024
8 checks passed
@mohamedcherifmo
Copy link
Contributor

I see the annotations array repeats the same annotation 100s of times... I created an issue around that

@nick-inkeep
Copy link
Contributor Author

I see the annotations array repeats the same annotation 100s of times... I created an issue around that

Proposed fix here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants