The API is available at api.{domain_name} and the frontend is available at {domain_name}.
This project leverages machine learning to analyze user messages, detect social or psychological problems, and recommend relevant projects based on those problems. It uses embeddings to match problems to solutions and solutions to projects, ultimately generating a helpful and empathetic response.
The frontend is a Next.js application located in the frontend directory. To run it locally, you can use the following command:
docker-compose up --buildThe frontend will be available at http://localhost:3000.
The message reply section on the process-message page now supports Markdown rendering, allowing for richer text formatting in the displayed replies. This was implemented using react-markdown and remark-gfm for GitHub Flavored Markdown support.
A new page has been added at app/messages/[id]/page.tsx to display individual message details, including the original message and its associated reply. The reply text on this page also supports Markdown rendering for enhanced readability.
- Python 3.8 or higher
- Node.js 18 or higher
- SQLite database
- OpenAI API Key (for LangChain integration)
- Required Python libraries (specified in
requirements.txt)
-
Clone the repository:
git clone <your-repository-url> cd <your-repository-directory>
-
Set up your environment variables:
Create a
.envfile by copying the.env.examplefile and filling in the required values forOPENAI_API_KEY,TELEGRAM_BOT_TOKEN, and database credentials. -
Build and run the application using Docker:
docker-compose up --build
This command will build the Docker image, start the database, run the database migrations, and launch the API and bot services.
-
projects: Stores information about projects that can potentially solve the detected problems.project_id: Unique identifier for the project.name: Name of the project.description: Description of the project.created_at: Date and time when the project was added.website: URL of the project’s website.contact_email: Email for contacting the project.
-
problems: Stores the problems detected from user messages.problem_id: Unique identifier for the problem.name: The name of the problem.context: The context or explanation for the problem.created_at: Date and time when the problem was added.is_processed: Flag indicating whether the problem has been processed or not.
-
solutions: Stores potential solutions that can address the problems.solution_id: Unique identifier for the solution.name: Name of the solution.context: The context or description of the solution.created_at: Date and time when the solution was added.
-
problems_solutions: Stores matches between problems and solutions, including similarity scores.problem_id: Foreign key linking to theproblemstable.solution_id: Foreign key linking to thesolutionstable.similarity_score: The similarity score between the problem and the solution.
-
projects_solutions: Stores matches between projects and solutions, including similarity scores.project_id: Foreign key linking to theprojectstable.solution_id: Foreign key linking to thesolutionstable.similarity_score: The similarity score between the project and the solution.
-
messages: Stores incoming user messages.message_id: Unique identifier for the message.user_id: The user who sent the message.user_username: Username of the user.chat_title: The title of the chat.text: The content of the message.
-
responses: Stores generated responses to the user’s messages.response_id: Unique identifier for the response.message_id: The message the response corresponds to.text: The generated response.created_at: Date and time when the response was generated.
-
message_projects: Links messages to the recommended projects.message_id: Foreign key linking to themessagestable.project_id: Foreign key linking to theprojectstable.
The system operates as follows:
- The incoming message is analyzed using a pre-trained model (LangChain + OpenAI) to detect social or psychological problems mentioned in the message.
- These problems are stored in the
problemstable.
- Each problem, solution, and project is represented by an embedding (vector).
- For each unprocessed problem, the script computes an embedding, compares it with the embeddings of solutions, and stores the top
ksolutions in theproblems_solutionstable. - Similarly, the script computes embeddings for projects and matches them to solutions, storing the top
kprojects in theprojects_solutionstable.
- Once the problems and projects are matched, the script generates a response using LangChain's OpenAI model.
- The generated response is stored in the
responsestable, which is associated with the original user message.
To initialize the system and compute all embeddings for solutions, projects, and their relationships, run:
python3 cli.py initAfter running the first-time setup (init), you can run the program again with a new message. The program will detect problems from the message, match them to solutions and projects, and generate a response.
To insert a new message, detect problems, match solutions and projects, and generate a response, for example, use:
python3 cli.py run "Я б хотіла, щоб в нашому місті було менше безпритульних тварин." \
--user-id 2345 \
--username "kasia2000" \
--chat-title "our_city"-
Insert the Message: The text of the message is inserted into the
messagestable. -
Detect Problems: The message is analyzed to detect problems (e.g., societal issues) using a pre-trained model.
-
Match Solutions: The problems are matched to potential solutions based on embeddings.
-
Match Projects: The matched solutions are further used to find relevant projects.
-
Generate Response: A response is generated based on the problems and projects, and stored in the
responsestable.
This command can be run multiple times with different messages. Each time, the problems, solutions, and projects will be matched and stored accordingly.
The project includes a Telegram bot that provides an interactive interface for users to access the system. The bot listens for messages that mention it and processes them using the same pipeline as the CLI interface.
- Responds to
/startcommand with a welcome message and usage instructions - Processes messages when the bot is mentioned (e.g., "@bot_name your message")
- Uses the same problem detection, solution matching, and response generation pipeline
-
Set up your Telegram Bot Token:
export TELEGRAM_BOT_TOKEN="your_telegram_bot_token"
-
Run the bot:
python3 -m src.telegram.bot
The bot will start listening for messages and process them using the project's main pipeline. Users can interact with the bot by mentioning it in their messages.