This project is a Flask-based API for interacting with a conversational AI waifu character. It provides endpoints for user management, dialog management, and server status checks.
This project provides a backend API for a conversational AI. It allows users to interact with the AI, manage their dialog history, and check the server status. The API is designed to be flexible and extensible, allowing for future integration with different AI models and front-end applications.
Clone the repository:
bash
git clone <repository_url>
cd waifu-chat-api
2. Install dependencies:
bash
pip install -r requirements.txt --user
You may create a requirements.txt by running:
bash
pip freeze > requirements.txt
Set environment variables:
The API relies on environment variables for configuration. You can set these variables directly in your terminal or, preferably, using a .env file.
DATABASE_FILE: Path to the SQLite database file (default: dialogs.db). This file stores user dialog history.MODEL_URL: URL of the AI model endpoint (default: http://localhost:80/path/). This is the address where the API sends requests to generate responses.DEFAULT_RESPONSE: The default response message when the AI model is unavailable or returns an error (default: "The AI model is currently unavailable. Please try again later.").DEFAULT_GENRE: The default genre for the conversation (default: "Romance").Example using a .env file (recommended):
Create a .env file in the project root:
DATABASE_FILE=dialogs.db
MODEL_URL=http://localhost:80/path/
DEFAULT_RESPONSE="I'm sorry, I'm having trouble connecting to the AI model."
DEFAULT_GENRE="Fantasy"
2. Install python-dotenv:
bash
pip install python-dotenv --user
3. Add the following lines to the top of src/waifuapi.py to load the environment variables:
python
from dotenv import load_dotenv
load_dotenv()
Run the application:
bash
python src/waifuapi.py
The default port is 5000. You can specify a different port by setting the PORT environment variable.
To run the application in different environments:
pytest (see the Testing section below).Note: The API uses the current-user header to identify the user making the request. This header is used to distinguish between different WaifuAPI users, allowing each user to have their own set of users and dialog history. If this header is not provided, the API defaults to a generic user ID.
The API provides the following endpoints:
/v1/user/id/<user_id> (PUT): Create a user.user_id (string, required): The ID of the user to create.PUT /v1/user/id/test_userjson
{"user_id": "test_user"}/v1/user/id/<user_id> (GET): Check if a user exists.user_id (string, required): The ID of the user to check.GET /v1/user/id/test_userjson
{"user_id": "test_user", "exists": true}json
{"user_id": "test_user", "exists": false}/v1/user/metadata/<user_id> (GET): Get user metadata (last modified datetime and timestamp).user_id (string, required): The ID of the user.GET /v1/user/metadata/test_userjson
{"user_id": "test_user", "last_modified_datetime": "2025-03-01 11:22:33", "last_modified_timestamp": 1740828153}/v1/user/id/<user_id> (DELETE): Delete a user.user_id (string, required): The ID of the user to delete.DELETE /v1/user/id/test_userjson
{"user_id": "test_user"}/v1/user/all/count (GET): Get the total number of users.GET /v1/user/all/countjson
{"user_count": 5}/v1/user/all/id/<int:page> (GET): Get all users, paged by hundreds.page (integer, required): The page number to retrieve (0-indexed).GET /v1/user/all/id/0json
{"page": 0, "users": ["user1", "user2", "user3"]}/v1/user/dialog/<user_id> (DELETE): Reset user dialog.user_id (string, required): The ID of the user.DELETE /v1/user/dialog/test_userjson
{"user_id": "test_user"}/v1/user/dialog/json/<user_id> (GET): Get user dialog in JSON format.user_id (string, required): The ID of the user.GET /v1/user/dialog/json/test_userjson
{"user_id": "test_user", "dialog": [{"index": 0, "name": "User", "message": "Hello"}, {"index": 1, "name": "Waifu", "message": "Hi"}]}/v1/user/dialog/json/<user_id> (PUT): Set user dialog in JSON format.user_id (string, required): The ID of the user.json
{"dialog": [{"index": 0, "name": "User", "message": "Hello"}, {"index": 1, "name": "Waifu", "message": "Hi"}]}PUT /v1/user/dialog/json/test_userjson
{"dialog": [{"index": 0, "name": "User", "message": "Hello"}, {"index": 1, "name": "Waifu", "message": "Hi"}]}json
{"user_id": "test_user"}/v1/user/dialog/str/<user_id> (GET): Get user dialog as a string.user_id (string, required): The ID of the user.GET /v1/user/dialog/str/test_userjson
{"user_id": "test_user", "dialog": "User said: \"Hello\" Waifu said: \"Hi\""}/path (POST): Send a chat message (using form data).message (string, required): The chat message to send.user_id (string, required): The ID of the user sending the message.POST /path message=Hello&user_id=test_userHi/v1/waifu (POST): Send a chat message (using JSON data).user_id (string, required): The ID of the user sending the message.message (string, required): The chat message to send.POST /v1/waifujson
{"user_id": "test_user", "message": "Hello"}json
{"user_id": "test_user", "response": "Hi"}/v1/server/status (GET): Check server status.GET /v1/server/statusjson
{"status": "ok"}The API uses the following HTTP methods:
GET: Used to retrieve data from the server.PUT: Used to create or update data on the server.POST: Used to send data to the server to create or update a resource.DELETE: Used to delete data from the server.The API can be configured using environment variables. The following environment variables are supported:
DATABASE_FILE: Path to the SQLite database file (default: dialogs.db).MODEL_URL: URL of the AI model endpoint (default: http://localhost:80/path/).DEFAULT_RESPONSE: The default response message when the AI model is unavailable or returns an error (default: "The AI model is currently unavailable. Please try again later.").DEFAULT_GENRE: The default genre for the conversation (default: "Romance")."ModuleNotFoundError: No module named 'google.cloud'":
bash
pip install google-cloud-translate --userAPI returns "The AI model is currently unavailable. Please try again later." unexpectedly:
MODEL_URL environment variable and make sure the AI model is running correctly. Also check the DEFAULT_RESPONSE environment variable.The conversation genre is not what I expected:
DEFAULT_GENRE environment variable.