My first AI Agent

This notebook will guide you to create your first AI Agent.

Get your API Key and include the key in the request header as X-Library-Key to authenticate your requests.

Step 1: Set up your environment

Set up Environment

import requests
import json

key = "your-key-here"
domain = "https://api.ailibrary.ai"
headers = {
    'Content-Type': 'application/json',
    'X-Library-Key': key
}

Step 2: Create Agent

Set up Environment


url = f"{domain}/v1/agent/create"
payload = json.dumps({
  "title": "My First AI Agent",
  "knowledge_search": "true"
})
response = requests.request("POST", url, headers=headers, data=payload)
knowledge_id = json.loads(response.text)["knowledge_id"]
namespace = json.loads(response.text)["namespace"]
print("namespace", namespace)
print("knowledge_id", knowledge_id)
print(json.dumps(json.loads(response.text), indent=4))


Step 3: Add files

Add text or PDF files to share knowledge with your agent. You can use any publicly accessible links.

Add files


url = f"{domain}/v1/knowledgebase/{knowledgeId}"
payload = json.dumps({
  "type": "docs",
  "options": {
    "urls": [
      "https://assets.ctfassets.net/416ywc1laqmd/7oJfDqN7x2Bjj3bXiP3WHR/5866c87cd89c15987bbb7211bfc3f3cc/Update_11_17_23.pdf"
    ]
  },
  "meta": {
    "folder": "home"
  }
})
response = requests.request("PUT", url, headers=headers, data=payload)
print(json.dumps(json.loads(response.text), indent=4))

Step 4: Check Status

Check the status of the files. It can take anywhere between 30 seconds to several hours for processing files - depends on the file type. PDF and text files are the fastest.

If the file is processing, this will return processing

Once the file is processed, this will return available

Check Status


url = f"{domain}/v1/knowledgebase/{knowledgeId}/status"
response = requests.request("GET", url, headers=headers)
print(json.dumps(json.loads(response.text), indent=4))

Step 5: Chat

Chat responses are streaming by default. Three types of objects are returned for each query

ObjectDescriptionType
chat.completion.knowledgethis contains the details of sourcearray
chat.completion.latencylatency of responsestring
chat.completion.chunkresponse chunkstring

Chat

url = f"{domain}/v1/agent/{namespace}/chat"
payload = json.dumps({
  "session_id": "niusjndckj23",
  "messages": [
    { "role": "assistant", "content": "how are you?" },
    { "role": "user", "content": "doing good. thank you." }
  ]
})

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

Step 6: Chat UI

You can also chat with the agent using the chat UI.

Replace the namespace with your agent's namespace.

https://www.ailibrary.ai/agent/namespace/chat

Add this to your website/ product

Option 1:

You can embed the chat UI in your website or application using the following code.

<iframe src="https://www.ailibrary.ai/agent/{namespace}/chat" title="My First AI Agent" height="600px" width="100%" ></iframe>

Option 2:

Clone the Chat UI GitHub Repo and make it your own.

Next Steps

Add more files Repeat Step 3. Remember, you can add multiple files at once.

Was this page helpful?