Giter Site home page Giter Site logo

ttpss930141011 / yuwen-yorozuya-linebot Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 2.5 MB

A LineBot implemented using LangChain, featuring conversation isolation and memory, and implemented using clean architecture for practice.

Home Page: https://line.me/R/ti/p/%40553flmei

License: MIT License

Python 100.00%
ai-assistant flask langchain langchain-python line-sdk linebot python zeabur clean-architecture

yuwen-yorozuya-linebot's Introduction


Yuwen yorozuya LineBot

◦ Building a customize AI-assistant Linebot for everyone.

◦ Clean Architecture provides a testable, maintainable, and scalable app.

OpenAI OpenAI Python Pytest LINE Flask

GitHub top language Repo coverage GitHub code size in bytes GitHub commit activity GitHub license

🏠 Yorozuya

"Yorozuya" is a fictional establishment in the Japanese manga and anime series "Gintama", operated by the protagonist, Gintoki Sakata. Yorozuya is a business that handles various tasks and requests, ranging from finding lost items to resolving disputes.

The project was named "Yorozuya" because it was hoped that the LineBot would make it easier for Line users to access OpenAPI to solve problems, and also to commemorate the end of Gintama.

Yorozuya

📒 Table of Contents

📍 Overview

The yuwen-yorozuya-linebot project is a feature-rich chat interface built on the LINE platform that enables seamless and interactive communication. This project has undergone a significant transformation, adopting the principles of Clean Architecture, which has not only enhanced its maintainability but also unlocked several key benefits.

  • Cleaner Code: The separation of concerns and modularization result in cleaner, more organized code, making it easier to read and maintain.

  • Testability: With well-defined interfaces and dependency injection, unit testing becomes straightforward, ensuring the reliability of the codebase.

  • Scalability: The architecture's modular nature allows for easy expansion and addition of new features, making the project adaptable to future requirements.

  • Maintainability: Clean Architecture promotes code maintainability, reducing the risk of technical debt and simplifying future updates and enhancements.

✨ Feature

yuwen-yorozuya-linebot serves as a dynamic chat agent chain with an array of essential features including:

OpenAI Function Calling

In addition to its core features, this project implements OpenAI Function Calling. This means that developers can encapsulate desired services as functions and incorporate them into the chatbot through the LangChain agent. We can implement this in src\infrastructure\tools\.

Language Model

The project leverages the LangChain framework, a powerful tool designed to simplify the integration of language models into applications. Specifically, LangChain is used in this project to seamlessly connect with the OpenAI Language Model (LLM), allowing developers to focus on application development without getting bogged down in the complexities of language processing.

Chat Memory

The ability to remember and recall previous conversations, providing a personalized user experience. Default is PostgreSQL. You can use your memory repositorios by implementing the BaseChatMemory interface.

System Messages

You can custom your LineBot's personality by setting the CHATBOT_DESCRIPTION in .env file.

🚀 Getting Started

✔️ Prerequisites

Before you begin, ensure that you have the following prerequisites installed:

  1. Setup LINE Channel to get CHANNEL_SECRET and CHANNEL_ACCESS_TOKEN

    ref: Day 12:帶著聊天機器人來到 LINE

  2. Get SerpAPI Token

    ref: SerpAPI

  3. Get OpenAI Token

    ref: OpenAI-API-keys

  4. Setup PostgreSQL and get connect string

    You can use some PostgreSQL provider like: ElephantSQL, Vercel Postgre...etc. I recommand Zeabur for a web-hosting PostgreSQL because you can create a project and run the LineBot and PostgreSQL in the same platform. So fast! So easy! So convenient!

  5. Setup environment variables by creating a .env file
    CHANNEL_SECRET=
    CHANNEL_ACCESS_TOKEN=
    OPENAI_API_KEY=
    PORT=
    SERPAPI_API_KEY=
    SQLALCHEMY_DATABASE_URI=
    CHATBOT_DESCRIPTION=
    CHATBOT_LANGUAGE=
    

📦 Installation

  1. Clone the yuwen-yorozuya-linebot repository:
git clone https://github.com/ttpss930141011/yuwen-yorozuya-linebot.git
  1. Change to the project directory:
cd yuwen-yorozuya-linebot
  1. Install the dependencies:
Poetry install

🧪 Running Tests

pytest

🎮 Using yuwen-yorozuya-linebot

python app.py

🚀 Deployment

For local

  1. Use ngrok

    ngrok http {PORT}
    
  2. Then put the forwarding url to LineBot Messaging API Webhook url settings.

For Zeabur

  1. New project

  2. New services to add PostgreSQL and Git repository

  3. Public the networking

  4. Setup environment variables


👨‍🏫 Demo

Keeping the Memory Alive Search information on the internet Group assistant
demo1 demo2 group

📝 Note

Clean Architecture Transformation

In the quest for code quality, maintainability, and scalability, the yuwen-yorozuya-linebot project has embraced Clean Architecture principles. Here's how Clean Architecture has influenced this project's development:

  • Flask + LINE SDK: The project continues to utilize Flask and the LINE SDK as its core technologies, but the key transformation lies in the architecture.

  • Blueprints as Handlers: In the Clean Architecture paradigm, message handlers are treated as blueprints. This approach decouples the handling of messages from the application's core logic, making the codebase more modular and maintainable.

  • Controller and Usecase Separation: The traditional controller and use case layers have been restructured, promoting a separation of concerns. Controllers are responsible for handling incoming requests and orchestrating the flow, while use cases encapsulate the application's business logic.

  • Dependency Injection: Extensive use of dependency injection enhances code cleanliness, testability, and extensibility. Dependencies are injected into components, reducing tight coupling and simplifying unit testing.

Final

I am used to implement 3-tier architecture in my project. Accidentally, I heard a sharing by a senior software engineer in development community. He was talking about what is the adventages he felt when he migrated the application to the another application from other company.

3-tier architecture Clean architecture
3-tier architecture Clean architecture

I started to study what is the Clean Architectre by reading two books "Clean Architecture: A Craftsman's Guide to Software Structure and Design" and "Architecture Patterns with Python: Enabling Test-Driven Development, Domain-Driven Design, and Event-Driven Microservices".

Although the project I made in formar job didn't have too complicated dependency like "Big Ball of yarn", I can imagine it will be annoying if I gonna change infrustructure or framework one day. In the project, I wrote many interfaces to make dependency to easily inject. I think a large number of dependency inversions is one of the important aspects why Clean Architecture is attractive.

Finally, I will continue to delve deeper into Clean Architecture, especially with regards to the implementation of entities and the definition of value objects. I'm not yet entirely familiar with these concepts in the context of this project, so my plan is to further study and explore them in greater detail.


🔜 Next

The following is todo-list on LineBot:

  • Implement muting and unmuting functions
  • Invoke LineBot by calling a key word.(it's too noisy in group.)
  • Give YouTube link and return video summary
  • Give the file and start the document conversation

📄 License

This project is licensed under the MIT License. See the LICENSE file for additional info.

yuwen-yorozuya-linebot's People

Contributors

ttpss930141011 avatar

Stargazers

 avatar Christopher Nelson avatar DoubleZ avatar  avatar Eliza Chen avatar

Watchers

James Cloos avatar  avatar

Forkers

renovate-bot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.