Skip to content

Commit

Permalink
feat(plugin-openai): add OpenAI integration for text generation (#2463)
Browse files Browse the repository at this point in the history
* feat: add plugin-openai for text generation using OpenAI API (#1234)

* fix: apply code review suggestions from coderabbitai

* add remaining updates for plugin

* Refactor: Centralize OpenAI action utilities in action.ts

- Introduced shared constants (DEFAULT_MODEL, DEFAULT_MAX_TOKENS, etc.).
- Added reusable functions:
  - validatePrompt: Validates text input for OpenAI actions.
  - validateApiKey: Ensures API key is present.
  - callOpenAiApi: Handles HTTP requests with timeout and error handling.
  - buildRequestData: Creates structured request payloads.
- Updated all action files to leverage shared logic.
- Improved maintainability and consistency across all OpenAI-related actions.

* feat(plugin-openai): finalize plugin setup and configuration

- Updated  to enhance terminal output with plugin details and accounts (Twitter, GitHub).
- Adjusted  scripts and dependencies for compatibility and functionality.
- Added a localized  for specific TypeScript configurations within the plugin.
- Improved initialization and logging to streamline the startup process.

This commit completes the setup for the OpenAI plugin and ensures integration with the framework.

---------

Co-authored-by: 0xrubusdata <[email protected]>
Co-authored-by: Sayo <[email protected]>
Co-authored-by: Sayo <[email protected]>
  • Loading branch information
4 people authored Jan 19, 2025
1 parent a5dccdb commit 9319fd3
Show file tree
Hide file tree
Showing 17 changed files with 1,565 additions and 735 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ EMBEDDING_OPENAI_MODEL= # Default: text-embedding-3-small
IMAGE_OPENAI_MODEL= # Default: dall-e-3
USE_OPENAI_EMBEDDING= # Set to TRUE for OpenAI/1536, leave blank for local

# Community Plugin for OpenAI Configuration
ENABLE_OPEN_AI_COMMUNITY_PLUGIN=false
OPENAI_DEFAULT_MODEL=
OPENAI_MAX_TOKENS=
OPENAI_TEMPERATURE=




# Atoma SDK Configuration
ATOMASDK_BEARER_AUTH= # Atoma SDK Bearer Auth token
ATOMA_API_URL= # Default: https://api.atoma.network/v1
Expand Down
235 changes: 118 additions & 117 deletions agent/package.json
Original file line number Diff line number Diff line change
@@ -1,119 +1,120 @@
{
"name": "@elizaos/agent",
"version": "0.1.9-alpha.1",
"main": "src/index.ts",
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.ts",
"dev": "node --loader ts-node/esm src/index.ts",
"check-types": "tsc --noEmit",
"test": "jest"
},
"nodemonConfig": {
"watch": [
"src",
"../core/dist"
],
"ext": "ts,json",
"exec": "node --enable-source-maps --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@elizaos/adapter-supabase": "workspace:*",
"@elizaos/adapter-postgres": "workspace:*",
"@elizaos/adapter-redis": "workspace:*",
"@elizaos/adapter-sqlite": "workspace:*",
"@elizaos/adapter-pglite": "workspace:*",
"@elizaos/client-auto": "workspace:*",
"@elizaos/client-direct": "workspace:*",
"@elizaos/client-discord": "workspace:*",
"@elizaos/client-farcaster": "workspace:*",
"@elizaos/client-lens": "workspace:*",
"@elizaos/client-telegram": "workspace:*",
"@elizaos/client-twitter": "workspace:*",
"@elizaos/client-instagram": "workspace:*",
"@elizaos/client-slack": "workspace:*",
"@elizaos/core": "workspace:*",
"@elizaos/plugin-0g": "workspace:*",
"@elizaos/plugin-abstract": "workspace:*",
"@elizaos/plugin-agentkit": "workspace:*",
"@elizaos/plugin-aptos": "workspace:*",
"@elizaos/plugin-birdeye": "workspace:*",
"@elizaos/plugin-coingecko": "workspace:*",
"@elizaos/plugin-coinmarketcap": "workspace:*",
"@elizaos/plugin-binance": "workspace:*",
"@elizaos/plugin-avail": "workspace:*",
"@elizaos/plugin-bootstrap": "workspace:*",
"@elizaos/plugin-di": "workspace:*",
"@elizaos/plugin-cosmos": "workspace:*",
"@elizaos/plugin-intiface": "workspace:*",
"@elizaos/plugin-coinbase": "workspace:*",
"@elizaos/plugin-conflux": "workspace:*",
"@elizaos/plugin-evm": "workspace:*",
"@elizaos/plugin-echochambers": "workspace:*",
"@elizaos/plugin-flow": "workspace:*",
"@elizaos/plugin-gitbook": "workspace:*",
"@elizaos/plugin-story": "workspace:*",
"@elizaos/plugin-gitcoin-passport": "workspace:*",
"@elizaos/plugin-goat": "workspace:*",
"@elizaos/plugin-lensNetwork": "workspace:*",
"@elizaos/plugin-icp": "workspace:*",
"@elizaos/plugin-image-generation": "workspace:*",
"@elizaos/plugin-movement": "workspace:*",
"@elizaos/plugin-massa": "workspace:*",
"@elizaos/plugin-nft-generation": "workspace:*",
"@elizaos/plugin-node": "workspace:*",
"@elizaos/plugin-solana": "workspace:*",
"@elizaos/plugin-injective": "workspace:*",
"@elizaos/plugin-solana-agent-kit": "workspace:*",
"@elizaos/plugin-squid-router": "workspace:*",
"@elizaos/plugin-autonome": "workspace:*",
"@elizaos/plugin-starknet": "workspace:*",
"@elizaos/plugin-stargaze": "workspace:*",
"@elizaos/plugin-giphy": "workspace:*",
"@elizaos/plugin-ton": "workspace:*",
"@elizaos/plugin-sui": "workspace:*",
"@elizaos/plugin-sgx": "workspace:*",
"@elizaos/plugin-iq6900": "workspace:*",
"@elizaos/plugin-tee": "workspace:*",
"@elizaos/plugin-tee-log": "workspace:*",
"@elizaos/plugin-tee-marlin": "workspace:*",
"@elizaos/plugin-multiversx": "workspace:*",
"@elizaos/plugin-near": "workspace:*",
"@elizaos/plugin-zksync-era": "workspace:*",
"@elizaos/plugin-twitter": "workspace:*",
"@elizaos/plugin-primus": "workspace:*",
"@elizaos/plugin-cronoszkevm": "workspace:*",
"@elizaos/plugin-3d-generation": "workspace:*",
"@elizaos/plugin-fuel": "workspace:*",
"@elizaos/plugin-avalanche": "workspace:*",
"@elizaos/plugin-video-generation": "workspace:*",
"@elizaos/plugin-web-search": "workspace:*",
"@elizaos/plugin-dexscreener": "workspace:*",
"@elizaos/plugin-letzai": "workspace:*",
"@elizaos/plugin-thirdweb": "workspace:*",
"@elizaos/plugin-genlayer": "workspace:*",
"@elizaos/plugin-tee-verifiable-log": "workspace:*",
"@elizaos/plugin-depin": "workspace:*",
"@elizaos/plugin-open-weather": "workspace:*",
"@elizaos/plugin-obsidian": "workspace:*",
"@elizaos/plugin-arthera": "workspace:*",
"@elizaos/plugin-allora": "workspace:*",
"@elizaos/plugin-opacity": "workspace:*",
"@elizaos/plugin-hyperliquid": "workspace:*",
"@elizaos/plugin-akash": "workspace:*",
"@elizaos/plugin-quai": "workspace:*",
"@elizaos/plugin-b2": "workspace:*",
"@elizaos/plugin-nft-collections": "workspace:*",
"@elizaos/plugin-pyth-data": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
"name": "@elizaos/agent",
"version": "0.1.9-alpha.1",
"main": "src/index.ts",
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.ts",
"dev": "node --loader ts-node/esm src/index.ts",
"check-types": "tsc --noEmit",
"test": "jest"
},
"nodemonConfig": {
"watch": [
"src",
"../core/dist"
],
"ext": "ts,json",
"exec": "node --enable-source-maps --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@elizaos/adapter-supabase": "workspace:*",
"@elizaos/adapter-postgres": "workspace:*",
"@elizaos/adapter-redis": "workspace:*",
"@elizaos/adapter-sqlite": "workspace:*",
"@elizaos/adapter-pglite": "workspace:*",
"@elizaos/client-auto": "workspace:*",
"@elizaos/client-direct": "workspace:*",
"@elizaos/client-discord": "workspace:*",
"@elizaos/client-farcaster": "workspace:*",
"@elizaos/client-lens": "workspace:*",
"@elizaos/client-telegram": "workspace:*",
"@elizaos/client-twitter": "workspace:*",
"@elizaos/client-instagram": "workspace:*",
"@elizaos/client-slack": "workspace:*",
"@elizaos/core": "workspace:*",
"@elizaos/plugin-0g": "workspace:*",
"@elizaos/plugin-abstract": "workspace:*",
"@elizaos/plugin-agentkit": "workspace:*",
"@elizaos/plugin-aptos": "workspace:*",
"@elizaos/plugin-birdeye": "workspace:*",
"@elizaos/plugin-coingecko": "workspace:*",
"@elizaos/plugin-coinmarketcap": "workspace:*",
"@elizaos/plugin-binance": "workspace:*",
"@elizaos/plugin-avail": "workspace:*",
"@elizaos/plugin-bootstrap": "workspace:*",
"@elizaos/plugin-di": "workspace:*",
"@elizaos/plugin-cosmos": "workspace:*",
"@elizaos/plugin-intiface": "workspace:*",
"@elizaos/plugin-coinbase": "workspace:*",
"@elizaos/plugin-conflux": "workspace:*",
"@elizaos/plugin-evm": "workspace:*",
"@elizaos/plugin-echochambers": "workspace:*",
"@elizaos/plugin-flow": "workspace:*",
"@elizaos/plugin-gitbook": "workspace:*",
"@elizaos/plugin-story": "workspace:*",
"@elizaos/plugin-gitcoin-passport": "workspace:*",
"@elizaos/plugin-goat": "workspace:*",
"@elizaos/plugin-lensNetwork": "workspace:*",
"@elizaos/plugin-icp": "workspace:*",
"@elizaos/plugin-image-generation": "workspace:*",
"@elizaos/plugin-movement": "workspace:*",
"@elizaos/plugin-massa": "workspace:*",
"@elizaos/plugin-nft-generation": "workspace:*",
"@elizaos/plugin-node": "workspace:*",
"@elizaos/plugin-solana": "workspace:*",
"@elizaos/plugin-injective": "workspace:*",
"@elizaos/plugin-solana-agent-kit": "workspace:*",
"@elizaos/plugin-squid-router": "workspace:*",
"@elizaos/plugin-autonome": "workspace:*",
"@elizaos/plugin-starknet": "workspace:*",
"@elizaos/plugin-stargaze": "workspace:*",
"@elizaos/plugin-giphy": "workspace:*",
"@elizaos/plugin-ton": "workspace:*",
"@elizaos/plugin-sui": "workspace:*",
"@elizaos/plugin-sgx": "workspace:*",
"@elizaos/plugin-iq6900": "workspace:*",
"@elizaos/plugin-tee": "workspace:*",
"@elizaos/plugin-tee-log": "workspace:*",
"@elizaos/plugin-tee-marlin": "workspace:*",
"@elizaos/plugin-multiversx": "workspace:*",
"@elizaos/plugin-near": "workspace:*",
"@elizaos/plugin-zksync-era": "workspace:*",
"@elizaos/plugin-twitter": "workspace:*",
"@elizaos/plugin-primus": "workspace:*",
"@elizaos/plugin-cronoszkevm": "workspace:*",
"@elizaos/plugin-3d-generation": "workspace:*",
"@elizaos/plugin-fuel": "workspace:*",
"@elizaos/plugin-avalanche": "workspace:*",
"@elizaos/plugin-video-generation": "workspace:*",
"@elizaos/plugin-web-search": "workspace:*",
"@elizaos/plugin-dexscreener": "workspace:*",
"@elizaos/plugin-letzai": "workspace:*",
"@elizaos/plugin-thirdweb": "workspace:*",
"@elizaos/plugin-genlayer": "workspace:*",
"@elizaos/plugin-tee-verifiable-log": "workspace:*",
"@elizaos/plugin-depin": "workspace:*",
"@elizaos/plugin-open-weather": "workspace:*",
"@elizaos/plugin-obsidian": "workspace:*",
"@elizaos/plugin-arthera": "workspace:*",
"@elizaos/plugin-allora": "workspace:*",
"@elizaos/plugin-opacity": "workspace:*",
"@elizaos/plugin-hyperliquid": "workspace:*",
"@elizaos/plugin-akash": "workspace:*",
"@elizaos/plugin-quai": "workspace:*",
"@elizaos/plugin-b2": "workspace:*",
"@elizaos/plugin-nft-collections": "workspace:*",
"@elizaos/plugin-pyth-data": "workspace:*",
"@elizaos/plugin-openai": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
}
5 changes: 5 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import { hyperliquidPlugin } from "@elizaos/plugin-hyperliquid";
import { echoChambersPlugin } from "@elizaos/plugin-echochambers";
import { dexScreenerPlugin } from "@elizaos/plugin-dexscreener";
import { pythDataPlugin } from "@elizaos/plugin-pyth-data";
import { openaiPlugin } from '@elizaos/plugin-openai';

import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";
import Database from "better-sqlite3";
Expand Down Expand Up @@ -1044,6 +1045,9 @@ export async function createAgent(
getSecret(character, "PYTH_MAINNET_PROGRAM_KEY")
? pythDataPlugin
: null,
getSecret(character, "OPENAI_API_KEY") && getSecret(character, "ENABLE_OPEN_AI_COMMUNITY_PLUGIN")
? openaiPlugin
: null,
].filter(Boolean),
providers: [],
actions: [],
Expand Down Expand Up @@ -1287,3 +1291,4 @@ if (
console.error("unhandledRejection", err);
});
}

63 changes: 63 additions & 0 deletions packages/plugin-openai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

# @elizaos/plugin-openai

A plugin for OpenAI integration, providing automated text generation capabilities.

## Overview

This plugin provides functionality to:

- Generate text using OpenAI's GPT models.
- Customize prompts for context-aware content generation.

## Installation

```bash
npm install @elizaos/plugin-openai
```

## Configuration

The plugin requires the following environment variable:

```env
OPENAI_API_KEY=your_openai_api_key
```

## Usage

Import and register the plugin in your Eliza configuration:

```typescript
import { openaiPlugin } from "@elizaos/plugin-openai";

export default {
plugins: [openaiPlugin],
// ... other configuration
};
```

### Generating Text

```typescript
const result = await generateTextAction.handler(runtime, message, state);
console.log(result.text); // Output generated by OpenAI
```

## Development

### Building

```bash
npm run build
```

### Testing

```bash
npm run test
```

## License

This plugin is part of the Eliza project. See the main project repository for license information.
34 changes: 34 additions & 0 deletions packages/plugin-openai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{
"name": "@elizaos/plugin-openai",
"version": "0.1.0",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@elizaos/source": "./src/index.ts",
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"files": [
"dist"
],
"dependencies": {
"@elizaos/core": "workspace:*",
"axios": "^1.0.0"
},
"devDependencies": {
"tsup": "8.3.5",
"vitest": "^1.0.0"
},
"scripts": {
"build": "tsup src/index.ts --format esm --dts --tsconfig ./tsconfig.json",
"dev": "tsup src/index.ts --format esm --dts --watch --tsconfig ./tsconfig.json"
}
}
Loading

0 comments on commit 9319fd3

Please sign in to comment.