Using docker in China 2024
Docker has been blocked in China since June 6, 2024. In this post, I will cover how to use docker in China.
Build Your Own AI Chatbot Client with lobe-chat
lobe-chat is a open-source project for build an AI client. It supports multiple AI providers, such as OpenAI, Claude 3, Gemini and more. It offers several useful features, including Local Large Language Model (LLM) support, Model Visual Recognition, TTS & STT Voice Conversation, Text to Image Generation, Plugin System (Function Calling), Agent Market (GPTs), Progressive Web App (PWA), Mobile Device Adaptation, and Custom Themes.
How to Deploy
Deploying with Docker
# Always pull the latest Docker image before running |
Deploying to Vercel
You can also fork the lobe-chat project and deploy it to Vercel.
Setting Up lobe-chat
Required Settings
The API key is a required property that must be set.
If you set the OPENAI_API_KEY environment variable when you start the project, you can use the chatbot application directly. lobe-chat will not show an error or prompt you to set an API key. If you want to authenticate users, you can set the ACCESS_CODE environment variable.
If you don’t set the environment variables OPENAI_API_KEY and ACCESS_CODE when you start the project, lobe-chat will show an error on the web page and prompt you to set an API key. You can also set an API key in the settings page before using the chatbot.
Optional Settings
Set Default Agent
Model Settings
- Model: Choose your preferred language model, such as GPT-4.
Set an API proxy
If you need to use the OpenAI service through a proxy, you can configure the proxy address using the OPENAI_PROXY_URL
environment variable:
-e OPENAI_PROXY_URL=https://my-api-proxy.com/v1 |
If you want to use a localhost proxy
-e OPENAI_PROXY_URL=http://localhost:18080/v1 \ |
or
# connect to proxy Docker container |
Build Your Own AI Chatbot Client with ChatGPT-Next-Web
ChatGPT-Next-Web is an open-source project for building an AI chatbot client. This project is designed to be cross-platform, allowing it to be used on various operating systems. It currently can be used as a web or PWA application, or as a desktop application on Linux, Windows, or macOS. Additionally, it supports several AI providers, including OpenAI and Google AI.
How ChatGPT-Next-Web Works
ChatGPT-Next-Web manages your API keys locally in the browser. When you send a message in the chat box, ChatGPT-Next-Web will, based on your settings, send a request to the AI provider and render the response message.
How to Deploy
Deploying with Docker
# Always pull the latest Docker image before running |
Deploying to Vercel
You can also fork the ChatGPT-Next-Web project and deploy it to Vercel.
Setting Up ChatGPT-Next-Web
Click the settings button in the lower left corner to open the settings.
Required Settings
OpenAI API Key
Before using ChatGPT-Next-Web, you must set your OpenAI API Key in the Settings -> Custom Endpoint -> OpenAI API Key section.
Optional Settings
OpenAI Endpoint
If you have a self-deployed AI service API, you can set the value to something like http://localhost:18080.
Model
You can set your preferred model, such as gpt-4-0125-preview
.
Others
Self-deployed AI services
You can use the copilot-gpt4-service
to build a self-deployed AI service. To start an AI service, run the following command:
docker run -d \ |
or
docker network create chatgpt |
OpenAI Proxy
openai-scf-proxy: Use Tencent Cloud Serverless to set up OpenAI proxy in one minute.
Gradle Basics
Gradle is a build automation tool for multi-language software development. It controls the development process in the tasks of compilation and packaging to testing, deployment, and publishing.
In this post, I will introduce the basic use of Gradle. This post is based on Gradle 8.6 and Kotlin DSL.
Initialize a Gradle Project
You can run the following commands to initialize a Java project with Gradle:
$ gradle init --use-defaults --type java-application |
or
$ gradle init \ |
If you wan to create a Spring Boot application, you can use spring initializr.
Configurations
There are two configuration files in Gradle: build.gradle
and settings.gradle
. They are both important configuration files in a Gradle project, but they serve different purposes.
build.gradle
is a script file that defines the configuration of a project. It’s written in the Groovy or Kotlin programming languages, and it specifies how tasks are executed, dependencies are managed, and artifacts are built. This file typically resides in the root directory of your project.
settings.gradle
is focused on configuring the structure of a multi-project build and managing the relationships between different projects within it.
Plugins
You can add plugins to configuration file build.gradle.kts
like this:
plugins { |
Gradle core plugins:
java
: Provides support for building any type of Java project.application
: Provides support for building JVM-based, runnable applications.
Spring Boot plugins:
org.springframework.boot
: Spring Boot Gradle Pluginio.spring.dependency-management
: A Gradle plugin that provides Maven-like dependency management functionality. It will control the versions of your project’s direct and transitive dependencies.
More plugins:
Setting Properties
The following properties are the common properties for Java projects.
group = "com.example" |
java { |
application { |
Repositories
A Repository is a source for 3rd party libraries.
repositories { |
Declare dependencies
You can declare dependencies in build.gradle.kts
like this
dependencies { |
In Gradle, dependencies can be classified into several types based on where they come from and how they are managed. Here are the main dependency types:
- Compile Dependencies:
- These are dependencies required for compiling and building your project. They typically include libraries and frameworks that your code directly depends on to compile successfully.
- Dependencies declared with
compile
are visible to all modules, including downstream consumers. This means that If Module A has a compile dependency on a library, and Module B depends on Module A, then Module B also has access to that library transitively. However, this also exposes the implementation details of Module A to Module B, potentially causing coupling between modules. In Gradle 3.4 and later,compile
is deprecated in favor ofimplementation
.
- Implementation Dependencies:
- Introduced in Gradle 3.4, these dependencies are similar to compile dependencies but have a more restricted visibility.
- They are not exposed to downstream consumers of your library or module. This allows for better encapsulation and prevents leaking implementation details. This means that if Module A has an implementation dependency on a library, Module B, depending on Module A, does not have access to that library transitively. This enhances encapsulation and modularity by hiding implementation details of a module from its consumers. It allows for better dependency management and reduces coupling between modules in multi-module projects.
- Runtime Dependencies: Dependencies that are only required at runtime, not for compilation. They are needed to execute your application but not to build it.
- Test Dependencies: Dependencies required for testing your code. These include testing frameworks, libraries, and utilities used in unit tests, integration tests, or other testing scenarios.
- Optional Dependencies: Dependencies that are not strictly required for your project to function but are nice to have. Gradle does not include optional dependencies by default, but you can specify them if needed.
Tasks
tasks.withType<Test> { |
Run Tasks
To list all the available tasks in the project:
$ gradle tasks |
Build Java
Before building a Java project, ensure that the java
plugin is added to the configuration file build.gradle.kts
.
plugins { |
Running the following command to build the project
$ gradle build |
Run Java main class
To run a Java project, ensure that the application
plugin and the mainClass
configuration are added to the configuration file build.gradle.kts
. The application
plugin makes code runnable.
plugins { |
Running the following command to run the main method of a Java project:
$ gradle run |
Gradle Wrapper
The Gradle Wrapper is the preferred way of starting a Gradle build. It consists of a batch script for Windows and a shell script for OS X and Linux. These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system.
The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary. As a result, developers can get up and running with a Gradle project quickly.
Gradle Wrapper files:
gradle/wrapper/gradle-wrapper.jar
: The Wrapper JAR file containing code for downloading the Gradle distribution.gradle/wrapper/gradle-wrapper.properties
: A properties file responsible for configuring the Wrapper runtime behavior e.g. the Gradle version compatible with this version.gradlew
,gradlew.bat
: A shell script and a Windows batch script for executing the build with the Wrapper.
If the project you are working on does not contain those Wrapper files, you can generate them.
$ gradle wrapper |
Run tasks with gradlew:
$ ./gradlew tasks |
References
[1] Building Java Projects with Gradle
Configuring Logback with Spring Boot
Logback is a popular logging framework for Java applications, designed as a successor to the well-known Apache Log4j framework. It’s known for its flexibility, performance, and configurability. Logback is extensively used in enterprise-level Java applications for logging events and messages.
In this post, I will cover various aspects of using Logback with Spring Boot.
Dependencies
Before we can use Logback in a Spring Boot application, we need to add its library dependencies to the project.
<dependency> |
It contains ch.qos.logback:logback-classic
and org.slf4j:slf4j-api
Logback Configuration Files
Spring Boot projects use logback-spring.xml
or logback.xml
in the resources
directory as the Logback configuration file by default.
Priority of the Logback default configuration file: logback.xml
> logback-spring.xml
.
If you want to use a custom filename. You can specify the log configuration file path in application.xml
or application.yml
. For example:
logging: |
Logback Basic Configurations
Property
You can define some properties that can be referenced in strings. Common properties: log message pattern, log file path, etc.
<configuration> |
Appender
Appenders define the destination and formatting (optional) for log messages.
Types of Logback Appenders:
- ConsoleAppender: Writes log messages to the console window (standard output or error).
- FileAppender: Writes log messages to a specified file.
- RollingFileAppender: Similar to FileAppender, but it creates new log files based on size or time intervals, preventing a single file from growing too large.
- SocketAppender: Sends log messages over a network socket to a remote logging server.
- SMTPAppender: Sends log messages as email notifications.
ConsoleAppender
<configuration> |
FileAppender
<configuration> |
RollingFileAppender
<configuration> |
RollingPolicy
A rollingPolicy is a component attached to specific appenders that dictates how and when log files are automatically managed, primarily focusing on file size and archiving. Its primary function is to prevent log files from becoming excessively large, improving manageability and performance.
Purpose:
- Prevents large log files: By periodically rolling over (rotating) log files, you avoid single files growing too large, which can be cumbersome to manage and slow down access.
- Archiving logs: Rolling policies can archive rolled-over log files, allowing you to retain historical logs for analysis or auditing purposes.
Functionality:
- Triggers rollover: Based on the defined policy, the rollingPolicy determines when to create a new log file and potentially archive the existing one. Common triggers include exceeding a certain file size or reaching a specific time interval (e.g., daily, weekly).
- Defines archive format: The policy can specify how archived log files are named and organized. This helps maintain a clear structure for historical logs.
Benefits of using rollingPolicy:
- Manageability: Keeps log files at a manageable size, making them easier to handle and access.
- Performance: Prevents performance issues associated with excessively large files.
- Archiving: Allows you to retain historical logs for later use.
Common types of rollingPolicy in Logback:
- SizeBasedTriggeringPolicy: Rolls over the log file when it reaches a specific size limit (e.g., 10 MB).
- TimeBasedRollingPolicy: Rolls over the log file based on a time interval (e.g., daily, weekly, monthly).
- SizeAndTimeBasedRollingPolicy: Combines size and time-based triggers, offering more control over rolling behavior.
TimeBasedRollingPolicy
<configuration> |
SizeAndTimeBasedRollingPolicy
<configuration> |
Filter
A filter attached to an appender allows you to control which log events are ultimately written to the defined destination (file, console, etc.) by the appender.
Commonly used filters:
- ThresholdFilter: This filter allows log events whose level is greater than or equal to the specified level to pass through. For example, if you set the threshold to INFO, then only log events with level INFO, WARN, ERROR, and FATAL will pass through.
- LevelFilter: Similar to ThresholdFilter, but it allows more fine-grained control. You can specify both the level to match and whether to accept or deny log events at that level.
Filter only INFO level log messages.
<configuration> |
Filter level greater than INFO
<configuration> |
Logger
A logger in logback.xml
represents a category or source for log messages within your application.
There are two types of logger tags in Logback: <root>
and <logger>
. They have hierarchical relationships. All <logger>
are <root>
child logger. Loggers can inherit their parent logger’s configurations. <root>
represents the top level in the logger hierarchy which receives all package log messages. <logger>
receives log messages from a specified package.
<configuration> |
<root>
: It receive all package log messages.level="INFO"
: define the default logger level to INFO for all loggers.<appender-ref>
: Send messages to CONSOLE and ROLLING_FILE appender.
<logger>
name="com.taogen
: It receive thecom.taogen
package log messages.level="DEBUG"
: It overrides the logger level to DEBUG.additivity="false"
: If the message has been sent to a appender by its parent logger, current logger will not send the message to the same appender again.<appender-ref>
: Send message to CONSOLE and ROLLING_FILE appender.
Using Logback
package com.taogen.commons.boot.mybatisplus; |
@Slf4j
is a Lombok annotation that automatically creates a private static final field named log
of type org.slf4j.Logger
. This log
field is initialized with an instance of the SLF4J logger for the current class.
private static Logger log = LoggerFactory.getLogger(LogTest.class); |
The commonly used Logback levels (in order of increasing severity):
- TRACE: Captures the most detailed information.
- DEBUG: general application events and progress.
- INFO: general application events and progress.
- WARN: potential problems that might not cause immediate failures.
- ERROR: errors that prevent the program from functioning correctly.
Relationships between Logger object and <logger>
in logback.xml
<logging>
defined inlogback.xml
usually uses a package path as its name. Otherwise, use a custom name.- If you use Logback to print log messages in Java code, first, you need to pass a class or string to
LoggerFactory.getLogger()
method to get a Logger object, then call logger’s methods, such asdebug()
. - If the Logger object is obtained through a class, Logback looks for
<logger>
fromlogback.xml
using the object’s package or parent package path. If the Logger object is obtained through a string, Logback uses the string to find a custom<logger>
fromlogback.xml
.
More Configurations
Custom Loggers
You can create a custom logger by setting a name instead of using a package path as its name.
<configuration> |
Output log messages:
2024-03-07 09:20:43 [main] INFO my-custom-log - Hello! |
- my-custom-log: logger name.
Note that if you use a custom logger, you can’t get class information from the log message.
Configurations for Different Environments
Using <springProfile>
<configuration> |
Dynamically set the log configuration file path
You can dynamically set the log configuration file path in application.yml
. Different spring boot environments use different log configuration files.
application.yml
logging: |
logback-dev.xml
<configuration> |
logback-prod.xml
<configuration> |
A Complete Example
Goals
- Properties
- log patterns, log directory and log filename.
- Appenders
- Colorful log pattern for console appender.
- Console and RollingFile appenders.
- Time based rolling policy. Roll over daily, Keep 30 days’ worth of logs.
- Filter log messages in appenders. Separate INFO, ERROR log messages.
- Loggers
- Setting loggers of the root and the base package of project.
- Using custom loggers.
- Support multiple spring boot environments.
<?xml version="1.0" encoding="UTF-8"?> |
Software Technical Documentation Best Practices
Principles
- Oriented toward novices. It is assumed that most of the readers of the document are novices. This way you will write more understandable, in-depth, detailed, and readable.
Structure
- Overall logic: what, why, how, when, where.
- Try to break out into detailed subdirectories. You can quickly locate what you want to see.
Details
- The steps should be clear. Label steps 1, 2, and 3.
- Try to add links to nouns that you can give links to. E.g. official website, explanation of specialized terms.
- The code field is to be marked with a code, e.g.
code
. - Use tables as much as possible for structured information.
- Try to use pictures where you can illustrate to make a clearer and more visual illustration. Don’t mind the hassle. It is more visual and easier to read. For example: UML, flow chart.
- Give a link to the reference content at the end.
Others
- After writing, read it through at least once. Timely detection and revision of some statement errors, incoherence; inaccuracy and lack of clarity of expression, and so on.
Develop Chart Statistics APIs in a Spring Boot Project
Write Statistical SQLs
Before write the code, you can write statistical SQLs first. Because the core of statistical APIs are SQLs. As the saying goes, “first, solve the problem, then, write the code”.
Define Parameter and Response VOs
VO (value object) is typically used for data transfer between business layers and only contains data.
Parameter VOs
@Data |
Response VOs
@Data |
Write the APIs
@RestController |
Integrating Third Party APIs in a Spring Boot Application
Test APIs in Your Local Environment
Before integrating third-party APIs in your web application, it’s better to test the APIs with an API tool like Postman to ensure the APIs work properly in your local environment. It can also let you get familiar with the APIs.
Add API Configurations to Your Project
There are some common configurations of third-party APIs you need to add to your project. For example, authorization information (appId, appSecret), API URL prefix, and so on.
Add Configurations
Add the configurations to your spring boot configuration file application.yaml
:
thirdParty: |
Define the Configuration Bean
Add a spring bean to receive the configurations:
YourThirdPartyConfig.java
@Configuration |
You can access the configurations by @Autowired
it
@Autowired |
String appId = yourThirdPartyConfig.appId; |
Define Response Code
public class YourThirdPartyErrorCode { |
Define API URLs
public class YourThirdPartyApiUrl { |
Define Base Service
@RequiredArgsConstructor |
Get and Cache Access Token
@Service |
Write Code
Define the API Interfaces
Here we use the example of the social login API service to demonstrate.
public interface SocialLoginService { |
Why use JSONObject as parameters and return types of methods of the interface?
Different third-party API service platforms have different data structures. The parameters and return types of methods need to be extensible.
Define Constants
Keys for API parameters
public class Auth0Key { |
API request URIs
public class Auth0Url { |
Write the API Implementing Class
@Service |
Common third-party APIs and their class names
API Service | Interface Name | Implementation Name |
---|---|---|
Cloud Object Storage | COSService | AwsCOSService AzureCOSService |
Push Notification APIs | PushNotificationService | OneSignalPushService |
Social Media Login APIs | SocialLoginService | Auth0SocialLoginService FirebaseSocialLoginService |
Write Parameter Builders and Result Handlers
Parameter builders
public interface SocialLoginParamBuilder { |
@Component |
Result handlers
public interface SocialLoginResultHandler { |
@Component |
Use Your Interface
@Autowired |
JSONObject params = auth0SocialLoginParamBuilder.getFunc1Param(entity); |