Skip to content
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

[Refactor]: Core/API Split #1506

Merged
merged 19 commits into from
Jan 5, 2024
Merged

Conversation

dave-gray101
Copy link
Collaborator

@dave-gray101 dave-gray101 commented Dec 29, 2023

I've ran into circular reference problems a few times while attempting larger projects within LocalAI - so I decided it was time for a major refactor to try and flatten out our dependency hierarchy into a more orderly chain. This way, we can more easily add functionality to the system!

In addition to the refactoring to change up the references, I also refactored the codebase to have a strict split between the HTTP/endpoint code and the actual LocalAI functionality.

There is also a bare stub of the MQTT functionality at this point - while it looks useless, it's actually quite important - it is there to help test for circular references while I refactor!

Below I've pasted a copy of the directory structure info I've added to the devdocs, since it also explains the refactoring here.

Directory Structure of this Repo

The core repository is broken up into the following primary chunks:

  • /backend: gRPC protobuf specification and gRPC backends. Subfolders for each language.
  • /core: golang sourcecode for the core LocalAI application. Broken down below.
  • /docs: localai.io website that you are reading now
  • /examples: example code integrating LocalAI to other projects and/or developer samples and tools
  • /internal: here be dragons. Don't touch this, it's used for automatic versioning.
  • /models: No code here! This is where models are installed!
  • /pkg: golang sourcecode that is intended to be reusable or at least widely imported across LocalAI. Broken down below
  • /prompt-templates: No code here! This is where example prompt templates were historically stored. Somewhat obsolete these days, model-galleries tend to replace manually creating these?
  • /tests: Does what it says on the tin. Please write tests and put them here when you do.

The core folder is broken down further:

  • /core/backend: code that interacts with a gRPC backend to perform AI tasks.
  • /core/http: code specifically related to the REST server
  • /core/http/endpoints: Has two subdirectories, openai and localai for binding the respective endpoints to the correct backend or service.
  • /core/mqtt: core specifically related to the MQTT server. Stub for now. Coming soon!
  • /core/services: code implementing functionality performed by local-ai itself, rather than delegated to a backend.
  • /core/startup: code related specifically to application startup of local-ai. Potentially to be refactored to become a part of /core/services at a later date, or not.

The pkg folder is broken down further:

  • /pkg/assets: Currently contains a single function related to extracting files from archives. Potentially to be refactored to become a part of /core/utils at a later date?
  • /pkg/datamodel: Contains the data types and definitions used by the LocalAI project. Imported widely!
  • /pkg/gallery: Code related to interacting with a model-gallery
  • /pkg/grammar: Code related to BNF / functions for LLM
  • /pkg/grpc: base classes and interfaces for gRPC backends to implement
  • /pkg/langchain: langchain related code in golang
  • /pkg/model: Code related to loading and initializing a model and creating the appropriate gRPC backend.
  • /pkg/stablediffusion: Code related to stablediffusion in golang.
  • /pkg/utils: Every real programmer knows what they are going to find in here... it's our junk drawer of utility functions.

Copy link

netlify bot commented Dec 29, 2023

Deploy Preview for localai canceled.

Name Link
🔨 Latest commit ec8890c
🔍 Latest deploy log https://app.netlify.com/sites/localai/deploys/6597ad58c61c4f0008e6fe33

@dave-gray101 dave-gray101 marked this pull request as ready for review December 29, 2023 21:59
@dave-gray101 dave-gray101 changed the title DRAFT: Refactor Core/API [Refactor]: Core/API Split Dec 29, 2023
@dave-gray101 dave-gray101 requested a review from mudler December 29, 2023 22:00
@dave-gray101 dave-gray101 enabled auto-merge (squash) December 29, 2023 23:36
@mudler
Copy link
Owner

mudler commented Dec 30, 2023

please stand-by @dave-gray101 the PR is quite big so will take some time for me to review it, but will try to have a look at it asap to avoid merge conflicts

@mudler
Copy link
Owner

mudler commented Dec 30, 2023

@dave-gray101 had to cut 2.3.1 with a couple of bugfixes so we can safely merge this too. Although now there are conflicts (sorry!)

@dave-gray101
Copy link
Collaborator Author

@mudler Pr has been updated, merged, and passes CI if now is a more clear merge window. Thanks!

As LocalAI is designed to make use of multiple, independent model galleries, those are maintained seperately. The following public model galleries are available for use:

* [go-skynet/model-gallery](https://github.com/go-skynet/model-gallery) - The original gallery, the `golang` huggingface scraper ran into limits and was largely retired, so this now holds handmade yaml configs
* [dave-gray101/model-gallery](https://github.com/dave-gray101/model-gallery) - An automated gallery designed to track HuggingFace uploads and produce best-effort automatically generated configurations for LocalAI. It is designed to produce one LocalAI gallery per repository on HuggingFace.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a great addition too, I don't want that to go down unnoticed, maybe also add it in the README? https://github.com/mudler/LocalAI#-community-and-integrations

core/backend/image.go Outdated Show resolved Hide resolved
pkg/datamodel/openai.go Outdated Show resolved Hide resolved
@@ -183,54 +179,3 @@ func InstallModel(basePath, nameOverride string, config *Config, configOverrides

return nil
}

type progressWriter struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to understand where you moved the progressWriter, the diff is quite huge for Github hand hard to follow up

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

processWriter got moved down into pkg/utils/uri.go since unless I made a merging error somewhere along the way, it's only used within uri.go's DownloadFile function

@mudler
Copy link
Owner

mudler commented Jan 4, 2024

ok I give it a first-pass, but even if the changes are refactors/moving functions around, github makes quite difficult to read the diff, hang on and will check it once more, but it's looking in the good direction up to now. I'll have a look later at it again for a second pass

@dave-gray101 dave-gray101 merged commit ab7b4d5 into mudler:master Jan 5, 2024
26 checks passed
@mudler
Copy link
Owner

mudler commented Jan 5, 2024

@dave-gray101 I think it's missing #1522

@mudler
Copy link
Owner

mudler commented Jan 5, 2024

re-adding it back in #1532

@mudler
Copy link
Owner

mudler commented Jan 5, 2024

A closer look made me realize this changeset skipped entirely tests as moving things to core would not trigger test suites:

$(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --label-filter="!gpt4all && !llama && !llama-gguf" --flake-attempts 5 --fail-fast -v -r ./api ./pkg

I'm trying to re-add it in #1532, but if many failures are getting in the way we might need to revert it

@mudler
Copy link
Owner

mudler commented Jan 5, 2024

also parts of c1888a8 are missing

mudler added a commit that referenced this pull request Jan 5, 2024
mudler added a commit that referenced this pull request Jan 5, 2024
Revert "[Refactor]: Core/API Split (#1506)"

This reverts commit ab7b4d5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants