Mind-Sports-Games / playstrategy-bot

A bridge between the playstrategy api and engines
GNU Affero General Public License v3.0
4 stars 2 forks source link

lichess-bot

Python Build Python Test

A bridge between Lichess Bot API and bots.

How to Install

Mac/Linux:

Windows:

Lichess OAuth

Setup Engine

Within the file config.yml:

As an optional convenience, there is a folder named engines within the lichess-bot folder where you can copy your engine and all the files it needs. This is the default executable location in the config.yml.default file.

Engine Configuration

Besides the above, there are many possible options within config.yml for configuring the engine for use with lichess-bot.

option name Debug Log File type string default option name Contempt type spin default 24 min -100 max 100 option name Analysis Contempt type combo default Both var Off var White var Black var Both option name Threads type spin default 1 min 1 max 512 option name Hash type spin default 16 min 1 max 33554432 option name Clear Hash type button option name Ponder type check default false option name MultiPV type spin default 1 min 1 max 500 option name Skill Level type spin default 20 min 0 max 20 option name Move Overhead type spin default 10 min 0 max 5000 option name Slow Mover type spin default 100 min 10 max 1000 option name nodestime type spin default 0 min 0 max 10000 option name UCI_Chess960 type check default false option name UCI_AnalyseMode type check default false option name UCI_LimitStrength type check default false option name UCI_Elo type spin default 1350 min 1350 max 2850 option name UCI_ShowWDL type check default false option name SyzygyPath type string default option name SyzygyProbeDepth type spin default 1 min 1 max 100 option name Syzygy50MoveRule type check default true option name SyzygyProbeLimit type spin default 7 min 0 max 7 option name Use NNUE type check default true option name EvalFile type string default nn-62ef826d1a6d.nnue uciok

Any of the names following `option name` can be listed in `uci_options` in order to configure the Stockfish engine.
```yml
  uci_options:
    Move Overhead: 100
    Skill Level: 10

The exceptions to this are the options uci_chess960, uci_variant, multipv, and ponder. These will be handled by lichess-bot after a game starts and should not be listed in config.yml. Also, if an option is listed under uci_options that is not in the list printed by the engine, it will cause an error when the engine starts because the engine won't understand the option. The word after type indicates the expected type of the options: string for a text string, spin for a numeric value, check for a boolean True/False value.

One last option is go_commands. Beneath this option, arguments to the UCI go command can be passed. For example,

  go_commands:
    nodes: 1
    depth: 5
    movetime: 1000

will append nodes 1 depth 5 movetime 1000 to the command to start thinking of a move: go startpos e2e4 e7e5 ....

One last option is go_commands. Beneath this option, commands prior to the go command can be passed. For example,

  go_commands:
    depth: 5

will precede the go command to start thinking with sd 5. The other go_commands list above for UCI engines (nodes and movetime) are not valid for XBoard engines and will detrimentally affect their time control.

Lichess Upgrade to Bot Account

WARNING: This is irreversible. Read more about upgrading to bot account.

To Run

After activating the virtual environment created in the installation steps (the source line for Linux and Macs or the activate script for Windows), run

python3 lichess-bot.py

The working directory for the engine execution will be the lichess-bot directory. If your engine requires files located elsewhere, make sure they are specified by absolute path or copy the files to an appropriate location inside the lichess-bot directory.

To output more information (including your engine's thinking output and debugging information), the -v option can be passed to lichess-bot:

python3 lichess-bot.py -v

To Quit

LeelaChessZero: Mac/Linux

LeelaChessZero: Windows CPU 2021

LeelaChessZero: Docker container

Use https://github.com/vochicong/lc0-nvidia-docker to easily run lc0 and lichess-bot inside a Docker container.

Creating a homemade bot

As an alternative to creating an entire chess engine and implementing one of the communication protocols (UCI or XBoard), a bot can also be created by writing a single class with a single method. The search() method in this new class takes the current board and the game clock as arguments and should return a move based on whatever criteria the coder desires.

Steps to create a homemade bot:

  1. Do all the steps in the How to Install
  2. In the config.yml, change the engine protocol to homemade
  3. Create a class in some file that extends MinimalEngine (in strategies.py).
    • Look at the strategies.py file to see some examples.
    • If you don't know what to implement, look at the EngineWrapper or UCIEngine class.
      • You don't have to create your own engine, even though it's an "EngineWrapper" class.
        The examples just implement search.
  4. In the config.yml, change the name from engine_name to the name of your class

    • In this case, you could change it to:

      name: "RandomMove"

Tips & Tricks

[Service] Environment="PYTHONUNBUFFERED=1" ExecStart=/usr/bin/python3 /home/thibault/lichess-bot/lichess-bot.py WorkingDirectory=/home/thibault/lichess-bot/ User=thibault Group=thibault Restart=always

[Install] WantedBy=multi-user.target



# Acknowledgements
Thanks to the Lichess team, especially T. Alexander Lystad and Thibault Duplessis for working with the LeelaChessZero team to get this API up. Thanks to the [Niklas Fiekas](https://github.com/niklasf) and his [python-chess](https://github.com/niklasf/python-chess) code which allows engine communication seamlessly.

# License
lichess-bot is licensed under the AGPLv3 (or any later version at your option). Check out the [LICENSE file](/LICENSE) for the full text.