dexaai / openai-fetch Goto Github PK
View Code? Open in Web Editor NEWMinimal and opinionated OpenAI client powered by fetch.
License: MIT License
Minimal and opinionated OpenAI client powered by fetch.
License: MIT License
Thanks for this API wrapper !
Calls to the model endpoint (i.e. call GET https://api.openai.com/v1/models
) is not included in this wrapper.
It's not critical, but could be nice (type safety and error management).
Openai-node no longer uses axios; it uses node-fetch by default in Node, and builtin fetch in all other envs.
Wanted to run API calls from edge, and had to find fetch-based client.
Stumbled upon this repo, but when searching npmjs.org, I found 2 packages pointing to this repo: openai-fetch
and @bytemain/openai-fetch
. Found this a bit strange/questioning.
Is this function already implemented?
Nice!
Can this be relied upon for production, or might anything break it at the openai end?
Thanks
Opening an issue to track this feature, since it seems to keep coming up, and I'm expecting to make use of it soon for a project.
See also:
Seeing the following error when trying to load an Open AI completion from a Next.js 13 page (server component).
import createOpenAiClient from '~/lib/openai';
async function getData() {
const data = await createOpenAiClient().createCompletion({
prompt: `List some cool objects in a room`,
model: 'text-curie-001',
temperature: 0,
frequency_penalty: 0.1,
max_tokens: 256,
});
console.log(JSON.stringify(data.response, null, 2));
return data.completion;
}
export default async function Page() {
const response = await getData();
To create the client, I originally used the code cited in the README. However, after seeing this error, I tried to create the client on-demand:
import { OpenAIClient } from 'openai-fetch';
function createOpenAiClient() {
return new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY });
}
export default createOpenAiClient;
But to no avail.
I noticed you're using ky
. A cursory search yielded a seemingly related issue: sindresorhus/ky#479
Currently, the library doesn't support any interaction with fine tunings.
The following is a list of wanted methods to be included:
retrieveFineTune
listFineTunes
createFineTune
cancelFineTune
listFineTuneEvents
Thanks for your efforts.
it is a ky
's issue.
it seem that CloudFlare Workers hasn't implements the property.
The README on this project says this:
Unfortunately, the official openai-node uses Axios, which only supports Node and is bloated.
Reasons to consider using openai-fetch:
Supports all envs with native fetch: Node 18+, browsers, Deno, Cloudflare Workers, etc
As of https://github.com/openai/openai-node/releases/tag/v4.0.0, openai
uses the native fetch
by default in Deno, Cloudflare workers, etc. It does use node-fetch
in Node 18+, though may be moving to undici
there soon.
The package download size stats may have changed as well, though most of the size of the official package is types, sourcemaps, etc which aren't imported at runtime. The runtime impact is usually around 25kb (still larger than this package).
Please can you elaborate more on this phrase in readme?
Unfortunately, the official [openai-node](https://github.com/openai/openai-node) uses Axios, which only supports Node and is bloated.
I got curious why you say that the axios and nodejs client is "bloated"
Hi, first of all, thanks for great lib
These lines would cause an error in environments where process is not available (ex. Cloudflare or browsers) in case if apiKey
or organizationId
is not specified
Error I get when trying to run in Cloudflare worker
โ [ERROR] Uncaught (in promise) ReferenceError: process is not defined
const organizationId = opts.organizationId || process.env.OPENAI_ORG_ID;
^
at OpenAIClient
(/root/dev/app/node_modules/openai-fetch/src/openai-client.ts:62:50)
at fetch (/root/dev/app/src/worker.ts:32:17)
As lib claims it supports all environments, including browsers, process
should not be used as fallback
Is it possible to extend API configuration to pass a specific fetch library?
It might solve some issues related to the different hosting environments.
something like that:
const client = new OpenAIClient({ apiKey: '<your api key>', fetch: myFetch });
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.