8000 (WIP) Openapi client gen by Stonelinks · Pull Request #144 · abetlen/llama-cpp-python · GitHub
[go: up one dir, main page]

Skip to content

(WIP) Openapi client gen #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Stonelinks
Copy link
Contributor

Putting this up for feedback, but this is the autogenerated openapi client for the server.

There's a lot of commits in here since this branch is based on:

So the changes will be easier to review if / when those make it into master.

But the last 5 or 6 commits are what this PR is ultimately for: infrastructure to autogenerate a client based on the openapi specification.

At a high level here's what these commits do:

  • Implement a script that downloads the openapi.json from the server to a local folder
  • Implement another script that uses this to generate a client via autorest
  • Commits for dependencies for the new client, as well as the client itself
  • Some runnable examples (that work!)

Missing:

  • Tests :)

Maybe the most controversial part of this is the decision to go with autorest when there are other codegen solutions out there for openapi clients. I wrote a long commit message about this (which i'll copy-paste here) but here are my thoughts on the matter:

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

@Stonelinks
Copy link
Contributor Author

Stonelinks#4

@Stonelinks Stonelinks force-pushed the openapi-client-gen branch from e12101d to 4f89e2f Compare May 3, 2023 01:55
@Stonelinks
Copy link
Contributor Author

Fixed up the last commit to include examples that:

  • Synchronously use the openapi client to make requests
  • Asynchronously use the openapi client to make requests
  • Make a streaming request (just uses the autogenerated models for now)

@Stonelinks Stonelinks force-pushed the openapi-client-gen branch 3 times, most recently from 453226d to b2233fe Compare May 10, 2023 00:35
@Stonelinks Stonelinks force-pushed the openapi-client-gen branch from b2233fe to 68f1b57 Compare May 14, 2023 06:50
@Stonelinks Stonelinks force-pushed the openapi-client-gen branch from 68f1b57 to 5dafdab Compare May 22, 2023 03:21
Stonelinks added 11 commits May 22, 2023 13:36
The README says to run `python3 setup.py develop` to build the library, however on a blank install this fails because scikit-build is not installed. This adds it to the dev dependencies so that it is installed.
Its cleaner to keep the virtualenv in the project directory, and
poetry makes it easy to do that via this config.
Give folks options + explicit instructions for installing with poetry or pip.
…y are unsupported but there for API compatibility with openai
This script is used to download the openapi json from the server and
save it to a file.

In the next commit this file is then used by the client to generate
the client code.

This script is meant to be invoked from the root of the repository,
i.e. `./bin/save-openapi-json`.
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.
Result of running ./bin/generate_api_client
Includes examples that:

- Synchronously use the openapi client to make requests
- Asynchronously use the openapi client to make requests
- Make a streaming asynchronous request (just uses the autogenerated models for now)
@Stonelinks Stonelinks force-pushed the openapi-client-gen branch from 5dafdab to 2d6ef25 Compare May 22, 2023 18:36
@gjmulder gjmulder added the enhancement New feature or request label May 23, 2023
@dannysheridan
Copy link
dannysheridan commented May 31, 2023
  • Its still generating a lot of code that isn't very idiomatic python (but its still reasonable to follow)

@Stonelinks I found this too, so I co-founded a company to generate idiomatic client code. Check out Fern as an easy way to pass in your OpenAPI spec and get a Python client that supports for sync and async clients out of the box. Read more about the Python generator

@abetlen abetlen force-pushed the main branch 2 times, most recently from 8c93cf8 to cc0fe43 Compare November 14, 2023 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0