English | 中文
AIGNE OpenAI SDK for integrating with OpenAI's GPT models and API services within the AIGNE Framework.
@aigne/openai
provides a seamless integration between the AIGNE Framework and OpenAI's powerful language models and APIs. This package enables developers to easily leverage OpenAI's GPT models in their AIGNE applications, providing a consistent interface across the framework while taking advantage of OpenAI's advanced AI capabilities.
- OpenAI API Integration: Direct connection to OpenAI's API services using the official SDK
- Chat Completions: Support for OpenAI's chat completions API with all available models
- Function Calling: Built-in support for OpenAI's function calling capability
- Streaming Responses: Support for streaming responses for more responsive applications
- Type-Safe: Comprehensive TypeScript typings for all APIs and models
- Consistent Interface: Compatible with the AIGNE Framework's model interface
- Error Handling: Robust error handling and retry mechanisms
- Full Configuration: Extensive configuration options for fine-tuning behavior
npm install @aigne/openai @aigne/core
yarn add @aigne/openai @aigne/core
pnpm add @aigne/openai @aigne/core
import { OpenAIChatModel } from "@aigne/openai";
const model = new OpenAIChatModel({
// Provide API key directly or use environment variable OPENAI_API_KEY
apiKey: "your-api-key", // Optional if set in env variables
model: "gpt-4o", // Defaults to "gpt-4o-mini" if not specified
modelOptions: {
temperature: 0.7,
},
});
const result = await model.invoke({
messages: [{ role: "user", content: "Hello, who are you?" }],
});
console.log(result);
/* Output:
{
text: "Hello! How can I assist you today?",
model: "gpt-4o",
usage: {
inputTokens: 10,
outputTokens: 9
}
}
*/
import { OpenAIChatModel } from "@aigne/openai";
const model = new OpenAIChatModel({
apiKey: "your-api-key",
model: "gpt-4o",
});
const stream = await model.invoke(
{
messages: [{ role: "user", content: "Hello, who are you?" }],
},
undefined,
{ streaming: true },
);
let fullText = "";
const json = {};
for await (const chunk of stream) {
const text = chunk.delta.text?.text;
if (text) fullText += text;
if (chunk.delta.json) Object.assign(json, chunk.delta.json);
}
console.log(fullText); // Output: "Hello! How can I assist you today?"
console.log(json); // { model: "gpt-4o", usage: { inputTokens: 10, outputTokens: 9 } }
Elastic-2.0