openai fetch
v3.3.2
由Fetch提供动力的最小且自以为是的Openai客户。
不幸的是,官方的OpenAi套件以有问题的方式获取,并且非常肿。
openai-fetch的原因:openai-fetch是〜14KB, openai为〜152KBopenai :npm install openai-fetch此软件包需要node >= 18或具有fetch支持的环境。
此软件包导出ESM。如果您的项目使用COMPONJ,请考虑切换到ESM或使用Dynamic import()函数。
import { OpenAIClient } from 'openai-fetch' ;
const client = new OpenAIClient ( { apiKey : '<your api key>' } ) ; apiKey是可选的,如果存在,将从process.env.OPENAI_API_KEY中读取。
API非常紧密地遵循OpenAI,因此通常可以使用其参考文档。所有内容都是强烈键入的,因此,只要打字说明您的代码,您就会知道是否有什么不同。
// Generate a single chat completion
client . createChatCompletion ( params : ChatParams ) : Promise < ChatResponse > ;
// Stream a single completion via a ReadableStream
client . streamChatCompletion ( params : ChatStreamParams ) : Promise < ChatStreamResponse > ;
// Generate one or more completions
client . createCompletions ( params : CompletionParams ) : Promise < CompletionResponse > ;
// Stream a single completion via a ReadableStream
client . streamCompletion ( params : CompletionStreamParams ) : Promise < CompletionStreamResponse > ;
// Generate one or more embeddings
client . createEmbeddings ( params : EmbeddingParams ) : Promise < EmbeddingResponse >
// Checks for potentially harmful content
client . createModeration ( params : ModerationParams ) : Promise < ModerationResponse >
// Text-to-Speech
client . createSpeech ( params : SpeechParams ) : Promise < SpeechResponse >类型定义可通过TSServer避免使用,可以在此处找到:类型定义。
麻省理工学院©Dexa