Introducing NPi: A Quick Guide
NPi, or the Natural-language Programming Interface, is an innovative open-source platform designed to enhance AI agents' capabilities by enabling them to use tools and take actions in a virtual environment. Pronounced as "N π", this platform aims to facilitate the interaction of AI with various software tools and applications seamlessly. This guide will walk you through what NPi is, how to get started with it, and what you can achieve by using it.
What is NPi?
NPi stands as a frontier in natural language processing and AI tooling, providing Tool-use APIs. These APIs empower AI agents to "act" within digital ecosystems, using tools much like humans do. By bridging the gap between AI models and software applications, NPi enables more dynamic and responsive AI interactions.
Getting Started
How to Install NPi
Installing NPi is straightforward. If you have Python installed, you can get started with just a simple command:
pip install npiai
Your First Tool: A Quick Example
To demonstrate the potential of NPi, imagine you want to create a tool to calculate the nth Fibonacci number. Here is a step-by-step guide to creating your first NPi tool:
-
Create a Python File: Start by making a file named
main.py
. -
Insert the Following Code:
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: 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, 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())
-
Run Your Tool: Execute the file with the command:
python main.py
You will see the result of your function call, returning the Fibonacci number you're interested in, formatted using OpenAI's function calling protocol.
Exploring the Potential of NPi
Once you've successfully built and tested your first tool, you might wonder what the next steps are. Here's a few suggestions:
- Read the Documentation: Familiarize yourself with all the possibilities NPi offers here.
- Explore More Examples: Check out other examples and see how you can further integrate NPi into your projects here.
- Look Forward to NPi Cloud (coming soon): This feature promises to expand the capabilities of NPi's technology further.
Community and Support
Join the vibrant NPi community on Discord to connect with other developers and collaborate on improving NPi.
License
The NPi platform is offered under the Apache License 2.0, ensuring broad compatibility and freedom for developers to innovate.
With NPi, AI agents are set to become more proficient and versatile in their interactions with software tools, creating opportunities for developing smarter, more intuitive AI solutions. Dive in and start creating with NPi today! 🎉