npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openai-agent

v1.6.0

Published

Connect NodeJS/Typescript Functions With OpenAI Function Call APIs and build super awesome agents. This is maintained by twitter.com/etherlegend and not by OpenAI

Downloads

79

Readme

OpenAI Agent Framework 🤖

Built in TypeScript, the openai-agent package is designed to make your life easier and your code cleaner. You can build agents that interact in a chat-like manner, perform tasks, and integrate real-world tooling - all by intuitively exposing your NodeJS functions to OpenAI GPT Functions backend - and this package will abstract most of it using easy to use function decorators.

With this package, you can easily create an agent to set a goal, and give a bunch of tools (functions) for the agent to solve the problem autonomously. After each step, the result is passed back to GPT via OpenAI Functions and GPT will decide the next function to invoke, including its parameters

Getting Started 🚀

First, ensure that you have Node.js and npm installed. This package is built using TypeScript, so you might also want to have the TypeScript compiler installed globally. You can install it using npm:

npm install -g typescript

To install the OpenAI Agent package, run the following command:

npm install openai-agent

Important: As this library uses experimental reflection decorators, add this to your tsconfig.json

"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": true

Setup 🛠️

To interact with OpenAI's GPT Functions and creating agents, you'll need an OpenAI API key and the model ID you want to use. To provide these details, create a .env file in the root of your project:

OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-3.5-turbo-0613
SERPAPI_API_KEY=your_serp_key 

SERPAPI_API_KEY is optional and required only if you use the InternetTools

You'll need to replace your_openai_api_key and your_serp_key with your actual OpenAI API key and the SERP key

Building an Agent 🤖

Creating an agent is as simple as instantiating the OpenAIAgent class. Here's a basic example:

import { OpenAIAgent } from 'openai-agent';

const myAgent = new OpenAIAgent(myApiKey, myModel);

Now your agent is ready to go!

Creating Custom Functions 🔧

The real power of OpenAIAgent Framework comes from its ability to call predefined functions using OpenAIFunction decorator. This lets you create highly interactive and versatile agents.

Here's an example of a function that searches the Internet:

import { OpenAIFunction } from 'openai-agent';

class MyFunctions {
  @OpenAIFunction(
    "Searches the internet using SerpApi to get search results",
    {
      query: {
        type: "string",
        description: "The search query",
        required: true,
      },
    }
  )
  async searchInternet(query: string): Promise<string> {
    // Your search implementation here...
  }
}

Example Usage 📝

Once you've set up your agent and functions, you can put them together like this:

import { OpenAIAgent } from 'openai-agent';

// Initialize your OpenAI Agent
const myAgent = new OpenAIAgent(myApiKey, myModel);


// Provide your functions to the agent
const result = await myAgent.runAgent(
  "search the internet for 'best AI tools'",
  [new MyFunctions()]
);

console.log(result.content);

Crazier Example Usages 👩‍💻

Here are some more examples of how you can use this to create agents with few example function packages:

Agent that can use your Terminal 💻

const agent1 = new OpenAIAgent(process.env.OPENAI_API_KEY, process.env.OPENAI_MODEL);
const osInfoResponse = await agent1.runAgent(
    [{ role: "user", content: "get my os info and write a poem about it" }],  
    [new TerminalFunctions()],5
);
console.log(osInfoResponse?.content);

Agent that can Search the Web 🌐

const agent2= new OpenAIAgent(process.env.OPENAI_API_KEY, process.env.OPENAI_MODEL);
const internetSearchResponse = await agent2.runAgent(
    [{ role: "user", content: 'search internet for latest news on OpenAI functions and get me the titles & links to top 5 articles' }],  
    [new InternetFunctions()], 10
);
console.log(internetSearchResponse?.content);

Agent that can use your Terminal to do what you ask 🛠

const agent3= new OpenAIAgent(process.env.OPENAI_API_KEY, process.env.OPENAI_MODEL);
const taskResponse = await agent3.runAgent(
    [{ role: "user", content: 'you are an expert ai assistant. Ask the user to give you inputs and do what she/he asks for, and do this in a loop, till he types exit' }],  
    [new TerminalFunctions()], 10
);
console.log(taskResponse?.content);

These are just a few examples of what you can do with the OpenAI Agent package! Dive in and explore, and let the AI revolution begin.