This repository contains the source code for K-Bot, a general purpose, programmable & extensible AI being developed by Karunya, using state of the art machine learning models and APIs. Follow the development of K-Bot here: https://howtobuildanai.com
K-Bot has (or will soon have) the following abilities:
- ✅ Intelligence (powered by GPT-3.5/4)
- ✅ Skills (preconfigured prompts)
- ⬜️ Memory
- ✅ Conversations (database)
- ⬜️ Knowledge Base (embeddings)
- ⬜️ Hearing (powered by Whisper)
- ⬜️ Speech (powered by Neural2)
- ⬜️ Creativitiy (powered by DALL-E 2)
- ⬜️ Vision (powered by GPT-4)
Users will also be able to create and publish their own skills (see examples) for various use cases.
K-Bot is (or will be) accessible to users in the following ways:
- ✅ Web Application
- ✅ REST API
- ⬜️ Discord Bot
- ⬜️ Slack Bot
- ⬜️ WhatsApp Bot
- ⬜️ iOS & Android App
- ⬜️ VS Code Extension
- ⬜️ Voice Assistant
- ⬜️ and much more...
Developers will also be able to build their own applications using K-Bot's REST API. Starter templates will be provided for building various types of applications using K-Bot.
K-Bot uses the following technology stack:
This is not an exhaustive list, please check the source code for a full list of dependencies.
K-Bot is completely open-source and we welcome all forms of contributions from the community. Here's how you can contribute to K-Bot:
- Report bugs & suggest features by creating an issue
- Fix bugs & add features by opening a pull request
- To show your love for the project, star this repository
- Ask a question or provide suggestions by starting a discussion
- Blog or tweet about the project to help spread the word
Aside from the web app, you can access K-Bot via the following API endpoints to build your own AI-powered applications:
Use this endpoint generate a per-user API key. It sends an OTP to a user's email for generating an API key.
API endpoint: https://K-Bot.Karunya.com/api/auth/send-otp
Method: POST
Request Body (JSON):
{
"email": string
}
Response:
{
"message": "Verification code sent"
}
Use this endpoint to verify the OTP entered by the user and generate an API key that you can use for future requests.
API endpoint: https://K-Bot.Karunya.com/api/auth/verify-otp
Method: POST
Request Body (JSON):
{
"email": string,
"code": string
}
Response:
{
"apiKey": {
"key": "2211cf69-36bc-4c6d-9081-ae06a03fd235",
"user_id": "1234",
"name": "API Key - Thu Sep 30 2021 11:51:50 GMT-0700 (Pacific Daylight Time)",
}
}
Use the key
in the Authorization
header for endpoints requiring user authentication.
Use this to send messages to the ChatGPT API and get back a response. Include an authorization header with a valid K-Bot API key. It's identical to OpenAI's chat completions API.
Request:
POST https://K-Bot.Karunya.com/api/chat
Content-Type: application/json
Authorization: Bearer API_KEY
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Hi there!"
}
],
"stream": false
}
If stream
is true, the response will be streamed token by token.
Response:
HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: OPTIONS,GET,POST
Access-Control-Allow-Headers: x-requested-with,content-type
{
"id": "cmpl-2yV4sKhw1JDD9Nf28XNuKlEJ",
"object": "text_completion",
"created": 1644693056,
"model": "text-davinci-002",
"choices": [
{
"text": "Hello! How can I assist you today?",
"index": 0,
"logprobs": null,
"finish_reason": "stopped"
}
]
}
This endpoint gets all the conversations for a particular user using their API key.
Method: GET
ENDPOINT: https://K-Bot.Karunya.com/api/conversations
Headers: "Authorization" : "Bearer USER_API_KEY"
Response:
{
"data": [
{
"id": "03df7fd8-bbb6-4443-8bc8-a3c40fb88356",
"title": "Conversation Title",
"created_at": "2022-02-13T06:20:03.978988Z",
"user_id": "2c337d5f-3fa3-4fc0-a1d1-2934d35757df",
"messages": [
{
"id": "2c0ad242-d122-4a10-b247-2bacfc11a591",
"conversation_id": "03df7fd8-bbb6-4443-8bc8-a3c40fb88356",
"created_at": "2022-02-13T06:20:03.986683Z",
"role": "user",
"content": "Hello World"
},
{
"id": "7f9afb1d-f388-44db-8d99-82016c9b114d",
"conversation_id": "03df7fd8-bbb6-4443-8bc8-a3c40fb88356",
"created_at": "2022-02-13T06:20:04.572231Z",
"role": "assistant",
"content": "Hola"
}
]
}
// more conversations
]
}
This endpoint creates a new conversation for a particular user. The request body contains an array of messages and a title.
Endpoint: https://K-Bot.Karunya.com/api/conversations
Method: POST
Headers:
"Authorization" : "Bearer USER_API_KEY"
"Content-Type" : "application/json"
Sample Request:
{
"messages": [
{
"role": "system",
"content": "You are K-Bot, a helpful assistant developed by Karunya"
},
{
"role": "user",
"content": "Hello, who are you?"
}
],
"title": "Sample Conversation Title"
}
Sample Response:
{
"data": {
"created_at": "2023-05-03T11:07:04.447375+00:00",
"user_id": "b3079791-9e64-4344-9049-f8de47a0f0e7",
"title": "What is FreeCodeCamp?",
"id": "db245948-4010-4104-afba-bbe5f3514a73",
"messages": [
{
"id": "c0318588-0e60-45c8-9996-acfdbf359dc1",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "system",
"content": "You are K-Bot, a helpful and verstaile AI created by Karunya using state-of the art ML models and APIs."
},
{
"id": "ab33a56c-4672-46ae-85cf-c22bc337a26f",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "user",
"content": "What is FreeCodeCamp?"
},
{
"id": "c806fa20-3964-40b9-b422-0852b6a2a3c1",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "assistant",
"content": "FreeCodeCamp is a non-profit organization that offers free coding courses to anyone interested in learning web development."
}
]
}
}
Endpoint: https://K-Bot.Karunya.com/api/conversations/:conversation
Method: GET
Headers:
"Authorization" : "Bearer USER_API_KEY"
Sample Response:
{
"data": {
"created_at": "2023-05-03T11:07:04.447375+00:00",
"user_id": "b3079791-9e64-4344-9049-f8de47a0f0e7",
"title": "What is FreeCodeCamp?",
"id": "db245948-4010-4104-afba-bbe5f3514a73",
"messages": [
{
"id": "c0318588-0e60-45c8-9996-acfdbf359dc1",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "system",
"content": "You are K-Bot, a helpful and verstaile AI created by Karunya using state-of the art ML models and APIs."
},
{
"id": "ab33a56c-4672-46ae-85cf-c22bc337a26f",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "user",
"content": "What is FreeCodeCamp?"
},
{
"id": "c806fa20-3964-40b9-b422-0852b6a2a3c1",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "assistant",
"content": "FreeCodeCamp is a non-profit organization that offers free coding courses to anyone interested in learning web development."
}
]
}
}
Use this to add new messages to an existing conversation.
Endpoint: https://K-Bot.Karunya.com/api/conversations/:conversation
Method: GET
Headers:
"Authorization" : "Bearer USER_API_KEY"
"Content-Type" : "application/json"
Sample Request:
POST /api/conversations/1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
{
"messages": [
{
"role": "user",
"content": "Hello again!"
}
]
}
Sample Response:
{
"data": {
"created_at": "2023-05-03T11:07:04.447375+00:00",
"user_id": "b3079791-9e64-4344-9049-f8de47a0f0e7",
"title": "What is FreeCodeCamp?",
"id": "db245948-4010-4104-afba-bbe5f3514a73",
"messages": [
{
"id": "c0318588-0e60-45c8-9996-acfdbf359dc1",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "system",
"content": "You are K-Bot, a helpful and verstaile AI created by Karunya using state-of the art ML models and APIs."
},
{
"id": "ab33a56c-4672-46ae-85cf-c22bc337a26f",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "user",
"content": "What is FreeCodeCamp?"
},
{
"id": "c806fa20-3964-40b9-b422-0852b6a2a3c1",
"created_at": "2023-05-03T11:07:04.519493+00:00",
"role": "assistant",
"content": "FreeCodeCamp is a non-profit organization that offers free coding courses to anyone interested in learning web development."
}
]
}
}
GET https://K-Bot.Karunya.com/api/skills
(unauthenticated): Provides a list of all available skills as JSON
GET https://K-Bot.Karunya.com/api/:username/skills
(uauthneticated): Provides a list of skills provided by a user as JSON
GET https://K-Bot.Karunya.com/api/:username/:skill
(authenticated): Use it to get the details for
a specific skill
Use it to send input data and messages to a specific skill
POST https://K-Bot.Karunya.com/api/:username/:skill
(authenticated)
Authentication Headers: Create an API key at https://K-Bot.Karunya.com/account and include it as an "Authorization"
header with value "Bearer API_KEY"
Request Body: (similar to OpenAI chat completions but can include an inputData
key for initial inputs that are used to create system and user prompt)
{
"inputData": {
"topic": "NextJS",
"difficulty": "Medium"
},
"messages": [
{
"role": "assistant",
"content": "What is the purpose of the `_app.js` file in a Next.js project?\n\na) It is used for server-side rendering of the entire application\nb) It is used to customize the `App` component used by Next.js\nc) It is used to configure the routing of the Next.js application\nd) It is used to define the endpoints of the application's API\n\nPlease answer with the corresponding letter of the correct option."
},
{
"role": "user",
"content": "D. Who are you?"
}
]
}
Response Body (similar to OpenAI chat completions):
{
"id": "chatcmpl-79anDx2EENOfDIFmieHWkaJwR7Bwu",
"object": "chat.completion",
"created": 1682520739,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 327,
"completion_tokens": 33,
"total_tokens": 360
},
"choices": [
{
"message": {
"role": "assistant",
"content": "I am K-Bot, an AI assistant created by Karunya. My purpose is to help users test their understanding of a topic by asking them multiple choice questions."
},
"finish_reason": "stop",
"index": 0
}
]
}
Apart from the web app, you can interact with K-Bot via a REST API. The following endpoints are supported:
Follow these steps to deploy your own copy of K-Bot the Vercel:
-
Fork this repostory to get your own copy of the source code
-
Sign up on https://platform.openai.com and generate an Open API Key
-
Sign up on Supabase and set up a new project
- Change Signup & Login Email Templates for your project to the following:
Confirm Signup:
<h2>Confirm your signup</h2>
<p>Enter this code to sign up:</p>
<p>{{ .Token }}</p>
Magic Link:
<h2>Verification Code</h2>
<p>Enter this verification code:</p>
<p>{{ .Token }}</p>
- Create tables on the Supabase Dashboard:
To create the required database setup, you can go to your supabase account, click left on 'SQL Editor' and click on 'New query'.
In the empty screen paste the contents of the file 'ddl/ddl.sql', and then click on 'RUN'. Now the tables will be created as well as all the row level policies.
The structure created looks like:
Alternatively, you can create each table manually yourself
profiles
table:
Name | Description | Data Type | Format |
---|---|---|---|
id | No description | uuid | uuid |
username | No description | character varying | varchar |
first_name | No description | character varying | varchar |
last_name | No description | character varying | varchar |
avatar_url | No description | character varying | varchar |
skills
table:
Name | Description | Data Type | Format |
---|---|---|---|
id | No description | bigint | int8 |
created_at | No description | timestamp with time zone | timestamptz |
updated_at | No description | timestamp with time zone | timestamptz |
user_id | References the users's ID from auth.users | uuid | uuid |
slug | No description | character varying | varchar |
title | No description | character varying | varchar |
description | No description | character varying | varchar |
system_prompt | No description | character varying | varchar |
user_prompt | No description | character varying | varchar |
inputs | No description | jsonb | jsonb |
-
Sign up on Vercel and deploy K-Bot's NextJS application
-
Follow these instructions and select
K-Bot-web
as the root directory -
Make sure to set the following environment variables:
NEXT_PUBLIC_SUPABASE_URL
- Your Supabase Project URLNEXT_PUBLIC_SUPABASE_ANON_KEY
- Your Supabase Anon API keyOPENAI_API_KEY
- Your OpenAI API key
-
-
(Optional) Connect a custom domain (e.g. https://K-Bot.dev ) to your Vercel project.
After you've deployed your own copy of K-Bot, follow these additional steps to develop K-Bot:
-
Clone your repository to your computer or open it online using GitHub Codespaces
- If you're developing locally, make sure to install the latest versions of Node.js and Visual Studio Code
-
Open the repository on VS Code, launch the terminal, and run these commands:
cd K-Bot-web # enter NextJS project npm install # install dependencies npm run dev # run development server
You should now be able to open up the application in a new browser tab and interact with it.
-
Create a file
.env.local
inside theK-Bot-web
folder and add proper values for the following enviroment variables:NEXT_PUBLIC_SUPABASE_URL=xxx NEXT_PUBLIC_SUPABASE_ANON_KEY=xxx OPENAI_API_KEY=xxx
-
Make any desired code changes and the development server should refresh the application automatically
-
Stage, commit, and push your changes back to the GitHub repository when ready
-
If you've set up a Vercel project, any changes made to the
main
branch of your repository will get pushed automatically
NOTE: If you'd like to contribute your changes back to the original repository https://github.com/Karunyahq/K-Bot, please create a pull request from your fork. We welcome community contributions to K-Bot!
Check out our course for a deatailed walkthrough of the codebase: https://howotobuildanai.com
* K-Bot can't do "everything" just yet, but it soon will. 😉