Skip to content

Conversation

@Bijan-Massoumi
Copy link
Contributor

Title

Add Openai chat completion methods + support for streaming

Description

This PR adds a client function to request the AI toolkit method that returns completions in OpenAI's standard response format.
Additionally, it adds a general streaming request function to client.ts that takes a generic T and wraps the readable stream in a transformer that unwraps and parses it into type T and then returns it to the client. As a result, there are two new methods to call this single endpoint. There is the non-streaming version and then the streaming version.
It also adds a new example to request the streaming endpoints.

pnpm examples:start:ai openai stream

Type of change

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] This change requires a documentation update

Screenshots (if appropriate):

image

responseData = { data: await response.text() } as { data: T };
const jsonResponse = await response.json();
// If response has data field and no error, unwrap it, otherwise use the whole response
const data = jsonResponse.data && !jsonResponse.error ? jsonResponse.data : jsonResponse;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of parsing it into a data object and then unwrapping it later we just check if a data object already exists and there's no error and if so we return that raw object or if there is no data but also no error then we just parse the JSON response as is.

Bijan-Massoumi and others added 2 commits April 15, 2025 09:24
Co-authored-by: Daniel Khoo <daniel.khoo@messari.io>
@danielkhoo danielkhoo self-requested a review April 15, 2025 14:02
@Bijan-Massoumi Bijan-Massoumi merged commit cec9492 into master Apr 15, 2025
2 checks passed
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