It is common for most projects to use only one method from the OpenAI JavaScript SDK’s implementation, so why not use native fetch
for the this one REST endpoint instead?
Question?
In AI web projects or any web AI template, if you want to access OpenAI API, why are you using
openai
npm module rather than the REST API endpoints directly?
I personally think using REST endpoints is the best possible approach in terms of runtimes compatibility.
For JavaScript AI projects the default recommendation should be to use native fetch and move to SDK later if necessary.
Performance benefits are clear:
- Reduced bundle size
- Using native fetch for compatibility with any runtime (edge, workers, deno, browser)
- streaming
const res = await fetch('https://api.openai.com/v1/chat/completions', {
headers: requestHeaders,
method: 'POST',
body: JSON.stringify(payload),
})
If you are running long-running AI calls, remember to use Edge Functions instead of Serverless Functions:
References:
Godspeed
PS. Follow me on Twitter or LinkedIn
https://twitter.com/dom_sipowicz
https://www.linkedin.com/in/dominiksipowicz/
Top comments (1)
This is really great. I thought I'd leave yet another method to use OpenAI without the official SDK. The Ragged library just does a REST call under the hood, and is very well tested. dev.to/monarchwadia/use-openai-in-...