@aigne/openai
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@aigne/openai

GitHub star chart Open Issues codecov NPM Version Elastic-2.0 licensed

English | 中文

AIGNE OpenAI SDK for integrating with OpenAI's GPT models and API services within the AIGNE Framework.

Introduction

@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.

Features

  • 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

Installation

Using npm

npm install @aigne/openai @aigne/core

Using yarn

yarn add @aigne/openai @aigne/core

Using pnpm

pnpm add @aigne/openai @aigne/core

Basic Usage

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
    }
  }
  */

Streaming Responses

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 } }

License

Elastic-2.0

Readme

Keywords

none

Package Sidebar

Install

npm i @aigne/openai

Weekly Downloads

343

Version

0.2.0

License

Elastic-2.0

Unpacked Size

62.7 kB

Total Files

17

Last publish

Collaborators

  • li-yechao
  • wangshijun