8000 bin: add a script for generating the api client · abetlen/llama-cpp-python@698d043 · GitHub
[go: up one dir, main page]

Skip to content

Commit 698d043

Browse files
committed
bin: add a script for generating the api client
I tried a bunch of generators - they all had issues. Some generated thousands of lines of unintelligible code. Others generated code that had errors, failed because of our spec or made weird assumptions about library structure. Others I didn't even bother with since they seemed abandoned. I eventually settled on https://github.com/Azure/autorest by Microsoft. Its got some things going for it: - It works! - Its tested and documented - It gives decent configuration options for the generator - It allows for decent customization by users and consumers of the client - It can (eventually) support streaming responses from the server - The only dependency is docker - Its actually supported by a large organization / number of developers - It could be trivially extended to generate clients for multiple languages besides python The downsides: - Its still generating a lot of code that isn't very idiomatic python (but its still reasonable to follow) - It depends on some Microsoft / Azure libraries for basic things instead... they're probably fine but its still a bit weird to be using msrest and azure-core instead of like... pydantic and requests. - The docker container is slow to start since it always downloads the latest version of autorest Overall the pros outweigh the cons IMO... Going to go forward with this.
1 parent d45071d commit 698d043

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

bin/generate-api-client

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
set -e
3+
4+
cd $(dirname "$0")/../
5+
6+
CLIENT_PACKAGE_NAME=llama_cpp.client
7+
CLIENT_TMP_DIR=.tmp/llama-cpp-openapi-client
8+
CLIENT_FINAL_DIR=llama_cpp/client
9+
10+
# Ensure we're starting from a clean slate
11+
rm -rf .tmp $CLIENT_TMP_DIR $CLIENT_FINAL_DIR
12+
13+
# Pull the latest version of the API's OpenAPI spec
14+
./bin/save-openapi-json
15+
OPENAPI_JSON_PATH=.tmp/llama_server_openapi.json
16+
17+
# Generate the client
18+
19+
# Version tolerant: this is apparently their newer, more pythonic generator that is more tolerant to changes in the API
20+
VERSION_TOLERANT=true
21+
22+
# Python3 only: we don't need to support python2
23+
PYTHON3_ONLY=true
24+
25+
# Low level client: I think this is yet another client generator (???) - turn it off for now
26+
LOW_LEVEL_CLIENT=false
27+
28+
# Models: IMO the client it totally pointless without generating models (which is disabled by default) - so enable it
29+
# The only models that are supported are for msrest
30+
MODELS_MODE=msrest
31+
32+
# Client-side validation: also something disabled by default - i'm going to turn this off for now because i think there might be some problems with return types in the API
33+
CLIENT_SIDE_VALIDATION=false
34+
35+
# Setup.py: the client lives in llama_cpp/client, don't generate a setup.py or anything else to support a standalone package
36+
BASIC_SETUP_PY=false
37+
38+
# RXun autorest
39+
docker run --rm \
40+
-v $PWD:/local \
41+
-w /local \
42+
azsdkengsys.azurecr.io/azuresdk/autorest-python \
43+
autorest \
44+
--python \
45+
--namespace=$CLIENT_PACKAGE_NAME \
46+
--package-name=llama-cpp-api-client \
47+
--package-version=0.0.1 \
48+
--basic-setup-py=$BASIC_SETUP_PY \
49+
--version-tolerant=$VERSION_TOLERANT \
50+
--python3-only=$PYTHON3_ONLY \
51+
--low-level-client=$LOW_LEVEL_CLIENT \
52+
--models-mode=$MODELS_MODE \
53+
--client-side-validation=$CLIENT_SIDE_VALIDATION \
54+
--combine-operation-files=false \
55+
--clear-output-folder=true \
56+
--no-namespace-folders=true \
57+
--input-file=/local/$OPENAPI_JSON_PATH \
58+
--output-folder=/local/$CLIENT_TMP_DIR
59+
60+
# Move the client to the final location
61+
cp -r $CLIENT_TMP_DIR $CLIENT_FINAL_DIR
62+
63+
# Do some cleanup with black, isort and autoflake
64+
black $CLIENT_FINAL_DIR
65+
isort $CLIENT_FINAL_DIR
66+
autoflake -r --in-place --remove-all-unused-imports --remove-unused-variables $CLIENT_FINAL_DIR

0 commit comments

Comments
 (0)
0