File tree 5 files changed +174
-0
lines changed
5 files changed +174
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ # @elizaos/plugin-openai
3
+
4
+ A plugin for OpenAI integration, providing automated text generation capabilities.
5
+
6
+ ## Overview
7
+
8
+ This plugin provides functionality to:
9
+
10
+ - Generate text using OpenAI's GPT models.
11
+ - Customize prompts for context-aware content generation.
12
+
13
+ ## Installation
14
+
15
+ ``` bash
16
+ npm install @elizaos/plugin-openai
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ The plugin requires the following environment variable:
22
+
23
+ ``` env
24
+ OPENAI_API_KEY=your_openai_api_key
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Import and register the plugin in your Eliza configuration:
30
+
31
+ ``` typescript
32
+ import { openaiPlugin } from " @elizaos/plugin-openai" ;
33
+
34
+ export default {
35
+ plugins: [openaiPlugin ],
36
+ // ... other configuration
37
+ };
38
+ ```
39
+
40
+ ### Generating Text
41
+
42
+ ``` typescript
43
+ const result = await generateTextAction .handler (runtime , message , state );
44
+ console .log (result .text ); // Output generated by OpenAI
45
+ ```
46
+
47
+ ## Development
48
+
49
+ ### Building
50
+
51
+ ``` bash
52
+ npm run build
53
+ ```
54
+
55
+ ### Testing
56
+
57
+ ``` bash
58
+ npm run test
59
+ ```
60
+
61
+ ## License
62
+
63
+ This plugin is part of the Eliza project. See the main project repository for license information.
Original file line number Diff line number Diff line change
1
+
2
+ {
3
+ "name" : " @elizaos/plugin-openai" ,
4
+ "version" : " 0.1.0" ,
5
+ "type" : " module" ,
6
+ "main" : " dist/index.js" ,
7
+ "module" : " dist/index.js" ,
8
+ "types" : " dist/index.d.ts" ,
9
+ "exports" : {
10
+ "./package.json" : " ./package.json" ,
11
+ "." : {
12
+ "import" : {
13
+ "@elizaos/source" : " ./src/index.ts" ,
14
+ "types" : " ./dist/index.d.ts" ,
15
+ "default" : " ./dist/index.js"
16
+ }
17
+ }
18
+ },
19
+ "files" : [
20
+ " dist"
21
+ ],
22
+ "dependencies" : {
23
+ "@elizaos/core" : " workspace:*" ,
24
+ "axios" : " ^1.0.0"
25
+ },
26
+ "devDependencies" : {
27
+ "tsup" : " 8.3.5" ,
28
+ "vitest" : " ^1.0.0"
29
+ },
30
+ "scripts" : {
31
+ "build" : " tsup --format esm --dts" ,
32
+ "dev" : " tsup --format esm --dts --watch" ,
33
+ "test" : " vitest run" ,
34
+ "test:watch" : " vitest"
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+
2
+ import axios from "axios" ;
3
+ import { Action } from "@elizaos/core" ;
4
+ import { OpenAIResponse , OpenAIRequest } from "../types" ;
5
+
6
+ export const generateTextAction : Action = {
7
+ name : "generateText" ,
8
+ description : "Generate text using OpenAI" ,
9
+ async handler ( runtime , message , state ) {
10
+ const prompt = message . content . text || "" ;
11
+ const apiKey = process . env . OPENAI_API_KEY ;
12
+
13
+ if ( ! apiKey ) {
14
+ throw new Error ( "OpenAI API key is not set" ) ;
15
+ }
16
+
17
+ const requestData : OpenAIRequest = {
18
+ model : "text-davinci-003" ,
19
+ prompt,
20
+ max_tokens : 200 ,
21
+ temperature : 0.7 ,
22
+ } ;
23
+
24
+ try {
25
+ const response = await axios . post < OpenAIResponse > (
26
+ "https://api.openai.com/v1/completions" ,
27
+ requestData ,
28
+ {
29
+ headers : {
30
+ Authorization : `Bearer ${ apiKey } ` ,
31
+ "Content-Type" : "application/json" ,
32
+ } ,
33
+ }
34
+ ) ;
35
+
36
+ return { text : response . data . choices [ 0 ] . text . trim ( ) } ;
37
+ } catch ( error ) {
38
+ console . error ( "Error communicating with OpenAI API:" , error ) ;
39
+ throw new Error ( "Failed to generate text with OpenAI" ) ;
40
+ }
41
+ } ,
42
+ } ;
Original file line number Diff line number Diff line change
1
+
2
+ import { Plugin } from "@elizaos/core" ;
3
+ import { generateTextAction } from "./actions/generateText" ;
4
+
5
+ export const openaiPlugin : Plugin = {
6
+ name : "openai" ,
7
+ description : "OpenAI integration plugin for generating text" ,
8
+ actions : [ generateTextAction ] ,
9
+ evaluators : [ ] ,
10
+ providers : [ ] ,
11
+ } ;
12
+
13
+ export default openaiPlugin ;
Original file line number Diff line number Diff line change
1
+
2
+ export interface OpenAIRequest {
3
+ model : string ;
4
+ prompt : string ;
5
+ max_tokens : number ;
6
+ temperature : number ;
7
+ }
8
+
9
+ export interface OpenAIResponse {
10
+ id : string ;
11
+ object : string ;
12
+ created : number ;
13
+ model : string ;
14
+ choices : Array < {
15
+ text : string ;
16
+ index : number ;
17
+ logprobs : null | any ;
18
+ finish_reason : string ;
19
+ } > ;
20
+ }
You can’t perform that action at this time.
0 commit comments