-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
/api/cloud-eval
endpoint (#62)
* add cloud-eval endpoint * oop small typo * fix the uri * should be a yaml * rename file to the correct one and remove unused imports * remove stray q * tweaks: changelog + explanations precede the field --------- Co-authored-by: kraktus <[email protected]>
- Loading branch information
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
from typing import cast | ||
|
||
from ..types import Variant | ||
from .base import BaseClient | ||
from ..types.analysis import PositionEvaluation | ||
|
||
|
||
class Analysis(BaseClient): | ||
"""Client for analysis-related endpoints.""" | ||
|
||
def get_cloud_evaluation( | ||
self, | ||
fen: str, | ||
num_variations: int = 1, | ||
variant: Variant = "standard", | ||
) -> PositionEvaluation: | ||
"""Get the cached evaluation of a position, if available. | ||
Opening positions have more chances of being available. There are about 15 million positions in the database. | ||
Up to 5 variations may be available. Variants are supported. | ||
:param fen: FEN of a position | ||
:param num_variations: number of variations | ||
:param variant: game variant to use | ||
:return: cloud evaluation of a position | ||
""" | ||
path = "/api/cloud-eval" | ||
params = {"fen": fen, "multiPv": num_variations, "variant": variant} | ||
return cast(PositionEvaluation, self._r.get(path=path, params=params)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import List | ||
|
||
from typing_extensions import TypedDict | ||
|
||
|
||
class PrincipleVariation(TypedDict): | ||
moves: str | ||
"""Centipawn (cp) is the unit of measure used in chess as representation of the advantage. A centipawn is 1/100th | ||
of a pawn. This value can be used as an indicator of the quality of play. The fewer centipawns one loses per move, | ||
the stronger the play. | ||
""" | ||
cp: int | ||
|
||
|
||
class PositionEvaluation(TypedDict): | ||
fen: str | ||
knodes: int | ||
depth: int | ||
""" Principle Variation (pv) is a variation composed of the "best" moves (in the opinion of the engine).""" | ||
pvs: List[PrincipleVariation] |
48 changes: 48 additions & 0 deletions
48
tests/clients/cassettes/test_analysis/TestAnalysis.test_get_cloud_evaluation.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- application/json | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
User-Agent: | ||
- python-requests/2.31.0 | ||
method: GET | ||
uri: https://lichess.org/api/cloud-eval?fen=rnbqkbnr%2Fppp1pppp%2F8%2F3pP3%2F8%2F8%2FPPPP1PPP%2FRNBQKBNR+b+KQkq+-+0+2&multiPv=1&variant=standard | ||
response: | ||
body: | ||
string: '{"fen": "rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2","knodes":13683,"depth":22,"pvs":[{"moves":"c8f5 d2d4 e7e6 g1f3 g8e7 c1e3 c7c5 d4c5 e7c6 b1c3","cp":-13}]}' | ||
headers: | ||
Access-Control-Allow-Headers: | ||
- Origin, Authorization, If-Modified-Since, Cache-Control, Content-Type | ||
Access-Control-Allow-Methods: | ||
- OPTIONS, GET, POST, PUT, DELETE | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Connection: | ||
- keep-alive | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Thu, 02 Nov 2023 21:25:59 GMT | ||
Permissions-Policy: | ||
- interest-cohort=() | ||
Server: | ||
- nginx | ||
Strict-Transport-Security: | ||
- max-age=63072000; includeSubDomains; preload | ||
Transfer-Encoding: | ||
- chunked | ||
Vary: | ||
- Origin | ||
X-Frame-Options: | ||
- DENY | ||
content-length: | ||
- '6450' | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
|
||
from berserk import Client | ||
|
||
from berserk.types.analysis import PositionEvaluation | ||
from utils import validate, skip_if_older_3_dot_10 | ||
|
||
|
||
class TestAnalysis: | ||
@skip_if_older_3_dot_10 | ||
@pytest.mark.vcr | ||
def test_get_cloud_evaluation(self): | ||
res = Client().analysis.get_cloud_evaluation( | ||
fen="rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2", | ||
) | ||
validate(PositionEvaluation, res) |