Show HN: An Open Source project for enhancing AI Agents in taking action

https://github.com/npi-ai/npi

NPI

Warning

NPi is currently under active development and the APIs are subject to change in the future release. It is recommended to use the command line tool to try it out.

NPi is an open-source platform providing Tool-use APIs to empower AI agents with the ability to take action in virtual world!

🛠️Try NPi Online: Try NPi on online Playground (🚧Under Construction).

👀 NPi Example: Highly recommended to check this first - See what you can build with NPi.

🔥 Introducing NPi: Why we build NPi?

📚 NPi Documentation: How to use NPi?

📢 Join our community on Discord: Let's build NPi together 👻 !

NPi (Natural-language Programming Interface), pronounced as "N π", is an open-source platform providing Tool-use APIs to empower AI agents with the ability to operate and interact with a diverse array of software tools and applications.

Installation

One-Minute Quick Start

Let's create a new tool to compute the nth Fibonacci number. Start by crafting a new Python file titled main.py and insert the following snippet:

import os
import json
import asyncio
from openai import OpenAI
from npiai import FunctionTool, function
class MyTool(FunctionTool):
    name = 'Fibonacci'
    description = 'My first NPi tool'
    @function
    def fibonacci(self, n: int) -> int:
        """
        Get the nth Fibonacci number.

        Args:
            n: The index of the Fibonacci number in the sequence.
        """
        if n == 0:
            return 0
        if n == 1:
            return 1
        return self.fibonacci(n - 1) + self.fibonacci(n - 2)
async def main():
    async with MyTool() as tool:
        print(f'The schema of the tool is\n\n {json.dumps(tool.tools, indent=2)}')
        client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
        messages = [
            {
                "role": "user",
                "content": "What's the 10-th fibonacci number?",
            }
        ]
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=messages,
            tools=tool.tools,  # use tool as functions package
            tool_choice="auto",
            max_tokens=4096,
        )
        response_message = response.choices[0].message
        if response_message.tool_calls:
            result = await tool.call(tool_calls=response_message.tool_calls)
            print(f'The result of function\n\n {json.dumps(result, indent=2)}')
if __name__ == "__main__":
    asyncio.run(main())

Now, run the tool:

You will see the function result in OpenAI function calling format:

[
  {
    "role": "tool",
    "name": "fibonacci",
    "tool_call_id": "call_4KItpriZmoGxXgDloI5WOtHm",
    "content": 55
  }
]

content: 55 is the result of function calling, and the schema:

[
  {
    "type": "function",
    "function": {
      "name": "fibonacci",
      "description": "Get the nth Fibonacci number.",
      "parameters": {
        "properties": {
          "n": {
            "description": "The index of the Fibonacci number in the sequence.",
            "type": "integer"
          }
        },
        "required": [
          "n"
        ],
        "type": "object"
      }
    }
  }
]

That's it! You've successfully created and run your first NPi tool. 🎉

Next Steps

License

Apache License 2.0

{
"by": "wenfengwang",
"descendants": 12,
"id": 40237305,
"kids": [
40242178,
40237434,
40263891,
40242188
],
"score": 47,
"time": 1714663306,
"title": "Show HN: An Open Source project for enhancing AI Agents in taking action",
"type": "story",
"url": "https://github.com/npi-ai/npi"
}
{
"author": "npi-ai",
"date": null,
"description": "Action library for AI Agent. Contribute to npi-ai/npi development by creating an account on GitHub.",
"image": "https://opengraph.githubassets.com/7208e5df608304da53a36cf648a9734b51c928ae51e51bb8c0ee821fd21fd22b/npi-ai/npi",
"logo": "https://logo.clearbit.com/github.com",
"publisher": "GitHub",
"title": "GitHub - npi-ai/npi: Action library for AI Agent",
"url": "https://github.com/npi-ai/npi"
}
{
"url": "https://github.com/npi-ai/npi",
"title": "GitHub - npi-ai/npi: Action library for AI Agent",
"description": "NPI WarningNPi is currently under active development and the APIs are subject to change in the future release. It is recommended to use the command line tool to try it out. NPi is an open-source platform...",
"links": [
"https://github.com/npi-ai/npi"
],
"image": "https://opengraph.githubassets.com/7208e5df608304da53a36cf648a9734b51c928ae51e51bb8c0ee821fd21fd22b/npi-ai/npi",
"content": "<div><article><p></p><h2>NPI</h2><a target=\"_blank\" href=\"https://github.com/npi-ai/npi#npi\"></a><p></p>\n<div><p>Warning</p><p>NPi is currently under active development and the APIs are subject to change in the future release. It is recommended\nto use the command line tool to try it out.</p>\n</div>\n<p>NPi is an open-source platform providing <strong><em>Tool-use</em></strong> APIs to empower AI agents with the ability to take action in virtual world!</p>\n<p><a target=\"_blank\" href=\"https://www.npi.ai/playground\">🛠️Try NPi Online</a>: Try NPi on online Playground (🚧Under Construction).</p>\n<p><a target=\"_blank\" href=\"https://docs.npi.ai/examples\">👀 NPi Example</a>: <strong>Highly recommended to check this first</strong> - See what you can build with NPi.</p>\n<p><a target=\"_blank\" href=\"https://docs.npi.ai/blog/introducing-npi\">🔥 Introducing NPi</a>: Why we build NPi?</p>\n<p><a target=\"_blank\" href=\"https://docs.npi.ai/docs\">📚 NPi Documentation</a>: How to use NPi?</p>\n<p><a target=\"_blank\" href=\"https://discord.gg/wdskUcKc\">📢 Join our community on Discord</a>: Let's build NPi together 👻 !</p>\n<p>NPi (<strong>N</strong>atural-language <strong>P</strong>rogramming <strong>I</strong>nterface), pronounced as <strong>\"N π\"</strong>, is an open-source platform providing <strong><em>Tool-use</em></strong> APIs to empower AI agents with the ability to operate and interact with a diverse array of software tools and applications.</p>\n<p></p><h2>Installation</h2><a target=\"_blank\" href=\"https://github.com/npi-ai/npi#installation\"></a><p></p>\n<p></p><h2>One-Minute Quick Start</h2><a target=\"_blank\" href=\"https://github.com/npi-ai/npi#one-minute-quick-start\"></a><p></p>\n<p>Let's create a new tool to compute the nth Fibonacci number. Start by crafting a new Python file titled <code>main.py</code> and insert the following snippet:</p>\n<div><pre><span>import</span> <span>os</span>\n<span>import</span> <span>json</span>\n<span>import</span> <span>asyncio</span>\n<span>from</span> <span>openai</span> <span>import</span> <span>OpenAI</span>\n<span>from</span> <span>npiai</span> <span>import</span> <span>FunctionTool</span>, <span>function</span>\n<span>class</span> <span>MyTool</span>(<span>FunctionTool</span>):\n <span>name</span> <span>=</span> <span>'Fibonacci'</span>\n <span>description</span> <span>=</span> <span>'My first NPi tool'</span>\n <span>@<span>function</span></span>\n <span>def</span> <span>fibonacci</span>(<span>self</span>, <span>n</span>: <span>int</span>) <span>-&gt;</span> <span>int</span>:\n <span>\"\"\"</span>\n<span> Get the nth Fibonacci number.</span>\n<span></span>\n<span> Args:</span>\n<span> n: The index of the Fibonacci number in the sequence.</span>\n<span> \"\"\"</span>\n <span>if</span> <span>n</span> <span>==</span> <span>0</span>:\n <span>return</span> <span>0</span>\n <span>if</span> <span>n</span> <span>==</span> <span>1</span>:\n <span>return</span> <span>1</span>\n <span>return</span> <span>self</span>.<span>fibonacci</span>(<span>n</span> <span>-</span> <span>1</span>) <span>+</span> <span>self</span>.<span>fibonacci</span>(<span>n</span> <span>-</span> <span>2</span>)\n<span>async</span> <span>def</span> <span>main</span>():\n <span>async</span> <span>with</span> <span>MyTool</span>() <span>as</span> <span>tool</span>:\n <span>print</span>(<span>f'The schema of the tool is<span>\\n</span><span>\\n</span> <span><span>{</span><span>json</span>.<span>dumps</span>(<span>tool</span>.<span>tools</span>, <span>indent</span><span>=</span><span>2</span>)<span>}</span></span>'</span>)\n <span>client</span> <span>=</span> <span>OpenAI</span>(<span>api_key</span><span>=</span><span>os</span>.<span>getenv</span>(<span>'OPENAI_API_KEY'</span>))\n <span>messages</span> <span>=</span> [\n {\n <span>\"role\"</span>: <span>\"user\"</span>,\n <span>\"content\"</span>: <span>\"What's the 10-th fibonacci number?\"</span>,\n }\n ]\n <span>response</span> <span>=</span> <span>client</span>.<span>chat</span>.<span>completions</span>.<span>create</span>(\n <span>model</span><span>=</span><span>\"gpt-4o\"</span>,\n <span>messages</span><span>=</span><span>messages</span>,\n <span>tools</span><span>=</span><span>tool</span>.<span>tools</span>, <span># use tool as functions package</span>\n <span>tool_choice</span><span>=</span><span>\"auto\"</span>,\n <span>max_tokens</span><span>=</span><span>4096</span>,\n )\n <span>response_message</span> <span>=</span> <span>response</span>.<span>choices</span>[<span>0</span>].<span>message</span>\n <span>if</span> <span>response_message</span>.<span>tool_calls</span>:\n <span>result</span> <span>=</span> <span>await</span> <span>tool</span>.<span>call</span>(<span>tool_calls</span><span>=</span><span>response_message</span>.<span>tool_calls</span>)\n <span>print</span>(<span>f'The result of function<span>\\n</span><span>\\n</span> <span><span>{</span><span>json</span>.<span>dumps</span>(<span>result</span>, <span>indent</span><span>=</span><span>2</span>)<span>}</span></span>'</span>)\n<span>if</span> <span>__name__</span> <span>==</span> <span>\"__main__\"</span>:\n <span>asyncio</span>.<span>run</span>(<span>main</span>())</pre></div>\n<p>Now, run the tool:</p>\n<p>You will see the function result in <a target=\"_blank\" href=\"https://platform.openai.com/docs/guides/function-calling/function-calling\">OpenAI function calling format</a>:</p>\n<div><pre>[\n {\n <span>\"role\"</span>: <span><span>\"</span>tool<span>\"</span></span>,\n <span>\"name\"</span>: <span><span>\"</span>fibonacci<span>\"</span></span>,\n <span>\"tool_call_id\"</span>: <span><span>\"</span>call_4KItpriZmoGxXgDloI5WOtHm<span>\"</span></span>,\n <span>\"content\"</span>: <span>55</span>\n }\n]</pre></div>\n<p><code>content: 55</code> is the result of function calling, and the schema:</p>\n<div><pre>[\n {\n <span>\"type\"</span>: <span><span>\"</span>function<span>\"</span></span>,\n <span>\"function\"</span>: {\n <span>\"name\"</span>: <span><span>\"</span>fibonacci<span>\"</span></span>,\n <span>\"description\"</span>: <span><span>\"</span>Get the nth Fibonacci number.<span>\"</span></span>,\n <span>\"parameters\"</span>: {\n <span>\"properties\"</span>: {\n <span>\"n\"</span>: {\n <span>\"description\"</span>: <span><span>\"</span>The index of the Fibonacci number in the sequence.<span>\"</span></span>,\n <span>\"type\"</span>: <span><span>\"</span>integer<span>\"</span></span>\n }\n },\n <span>\"required\"</span>: [\n <span><span>\"</span>n<span>\"</span></span>\n ],\n <span>\"type\"</span>: <span><span>\"</span>object<span>\"</span></span>\n }\n }\n }\n]</pre></div>\n<p>That's it! You've successfully created and run your first NPi tool. 🎉</p>\n<p></p><h2>Next Steps</h2><a target=\"_blank\" href=\"https://github.com/npi-ai/npi#next-steps\"></a><p></p>\n<ul>\n<li><a target=\"_blank\" href=\"https://docs.npi.ai/docs\">Read the Documentation</a></li>\n<li><a target=\"_blank\" href=\"https://github.com/npi-ai/npi/blob/main/examples\">Explore More Examples</a></li>\n<li><a target=\"_blank\" href=\"https://github.com/npi-ai/npi#\">NPi Cloud(coming soon)</a></li>\n</ul>\n<p></p><h2>License</h2><a target=\"_blank\" href=\"https://github.com/npi-ai/npi#license\"></a><p></p>\n<p>Apache License 2.0</p>\n</article></div>",
"author": "",
"favicon": "https://github.githubassets.com/favicons/favicon.svg",
"source": "github.com",
"published": "",
"ttr": 84,
"type": "object"
}