Skip to main content

Quick Start

Installation

Make sure you have python 3.11 or later installed. We recommend creating and activating a python virtual environment for your project before proceeding.

Install Aiser, our open source package for serving AI resources:

pip install aiser

Adding Boilerplate Code

Create a file called app.py and add the following code:

from aiser import RestAiServer

if __name__ == '__main__':
server = RestAiServer(
agents=[],
knowledge_bases=[],
port=5000
)
server.run()

This code creates and starts a server.

  • agents is the list of agents that the server will be serving. We will create and add an agent here in just a moment.
  • knowledge_bases is the list of knowledge bases that the server will be serving. We will create and add a knowledge base here in just a moment.
  • port is the port that the server will be listening on.

Creating an Agent

You can skip this section if you only want to create a knowledge base.

In the same file as above, insert the following code:

import typing
from aiser import Agent
from aiser.models import ChatMessage
import asyncio


class AgentExample(Agent):
async def reply(self, messages: typing.List[ChatMessage]) -> typing.AsyncGenerator[ChatMessage, None]:
reply_message = "This is an example of a reply from an agent"
for character in reply_message:
yield ChatMessage(text_content=character)
await asyncio.sleep(0.1)

Here we are creating a class that inherits from Agent and we are implementing the reply method. For demo purposes, we are using a simple asyncio.sleep to simulate the typical delay when generating a reply. You can replace the body of this method with your own code that generates a reply. Make sure to yield a ChatMessage object for each small amount of text you want to stream to the user.

Create an instance of this class and add it to the agents parameter we mentioned above:

# Your previous code here ...
agents=[AgentExample(agent_id = "YOUR_AGENT_ID")],
# Your previous code here ...

Notice that we needed to specify an agent_id when creating the agent. If you are creating an agent for penlight.ai, then you will see this ID after you create the API-based agent on the website. You will need to copy it from there to your code.

Creating a Knowledge Base

You can skip this section if you only want to create an agent.

In the same file as above, insert the following code:

import typing
from aiser import KnowledgeBase, SemanticSearchResult

class KnowledgeBaseExample(KnowledgeBase):
def perform_semantic_search(
self, query_text: str, desired_number_of_results: int
) -> typing.List[SemanticSearchResult]:
result_example = SemanticSearchResult(
content="This is an example of a semantic search result",
score=0.5,
)
return [result_example for _ in range(desired_number_of_results)]

Here we are creating a class that inherits from KnowledgeBase and we are implementing the perform_semantic_search method. You can replace the body of this method with your own code that performs semantic search. Make sure to return a list of SemanticSearchResult objects.

Create an instance of this class and add it to the knowledge_bases parameter we mentioned above:

# Your previous code here ...
knowledge_bases=[KnowledgeBaseExample(knowledge_base_id = 'YOUR_KB_ID')]
# Your previous code here ...

Notice that we needed to specify a knowledge_base_id when creating the knowledge base. If you are creating a knowledge base for penlight.ai, then you will see this ID after you create the API-based knowledge base on the website. You will need to copy it from there to your code.

Running the Server

Set the AISER_ENVIRONMENT environment variable to Development.

Do this for local development and not in production. Please see the note below for more details.

On Mac/Linux, you can do this by running:

export AISER_ENVIRONMENT=Development

On Windows, you can do this by running:

set AISER_ENVIRONMENT=Development

Then run the server:

python app.py

Now on your browser, open the URL http://127.0.0.1:5000/docs to see the Swagger UI. You can use this UI to test your newly created knowledge base or agent.

IMPORTANT NOTE: When you set the AISER_ENVIRONMENT environment variable to Development, the server will skip authentication and authorization checks. If you do this in production then anyone on the internet can access your knowledge base or agent. So only do this for local development/testing purposes.

Deploying to Penlight AI

You first need access to the Penlight AI Developer Program. The program currently costs $25 per year and it allows you to publish both AI agents and knowledge bases on the marketplace. If you are interested, please contact us and tell us you want to join the Developer Program.

If you already have access to the Penlight AI Developer Program, then you can create an "API-Based Agent" or an "API-Based Knowledge Base". After that, click on "Publish" and follow the on screen instructions.

If you face any problems, please feel free to contact us.