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

[Prototype] Self-hosted CodeLlama LLM for code autocompletion #576

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

Conversation

senwang86
Copy link
Collaborator

@senwang86 senwang86 commented Nov 4, 2023

Summary

This pr provides a solution to use CodeLlama for self-hosted code autocompletion.

  • A standalone LLM is required to run separately and expose a RESTful API to access. The most straight-forward way is to use llama.cpp, in which you can host the LLM either on a Mac laptop or a GPU-installed machine.
    • Please follow the instructions of llama.cpp to run the LLM locally or in a cloud, also feel free to let me know if a detailed tutorial is needed.
  • A Sidebar setting is added to enable/disable the copilot
  • tRPC seems not very well support multiple providers in React, ref, thus I put the Route in api/src/server, initially we talked about to create an standalone server service to the copilot-related services.
  • There are a couple of follow-up patches to add, e.g., vite bug fix, monitoring the copilot service connection status, similar to the sync statue in the top of Sidebar, adding infilling mode in addition to autocomplete mode. This PR also aims to collect some early feedback regarding the overall design and architecture.

Test

  1. First, enable the RESTful API on the llama.cpp, assuming the IP address is x.x.x.x, port is 9090
  2. On the local machine, open the terminal,
    • cd codepod/api/
    • pnpm dev --copilotIP x.x.x.x --copilotPort 9090
  • Note that, the screenshot below intends to the demonstrate the functionality, the quality of the auto-completion might be low due to the 4-bit quantized llama-7b model.

copilot

@senwang86 senwang86 marked this pull request as ready for review November 4, 2023 04:21
@senwang86 senwang86 requested a review from lihebi November 4, 2023 04:22
@lihebi
Copy link
Collaborator

lihebi commented Nov 5, 2023

  • tRPC seems not very well support multiple providers in React, ref,

I also came across multiple providers the other day, and it is well supported: trpc#3049. I have implemented multiple providers in https://github.com/codepod-io/codepod-cloud/pull/11. Related code:

https://github.com/codepod-io/codepod-cloud/blob/113f4f7ca3656d6db2296bb32a64dc8ae3ae3342/ui/src/lib/trpc.ts#L9-L16

@lihebi
Copy link
Collaborator

lihebi commented Nov 5, 2023

With that said, it could actually be better and simpler to leave it in api/ routers, so that the frontend always has one API to talk to. We can let the api/ forward the request to the actual LLM service internally through tRPC or gRPC.

Copy link
Collaborator

@lihebi lihebi left a comment

Choose a reason for hiding this comment

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

Thanks, Sen! It works well. I left some minor comments in the code.

One issue is the automatic completion by InlineCompletionsProvider is not very responsiveness. Sometimes it is fired, and sometimes it is not. How about using a shortcut to trigger it manually, and disable the automatic triggering?

@@ -59,6 +68,8 @@ export async function startServer({ port, repoDir }) {
});

http_server.listen({ port }, () => {
console.log(`🚀 Server ready at http://localhost:${port}`);
console.log(
`🚀 Server ready at http://localhost:${port}, LLM Copilot is hosted at ${copilotIpAddress}:${copilotPort}`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd revert this print information, because people may choose to run CodePod without copilot server, and this info is misleading. Just be silent should be fine.

} else {
remoteUrl = `${window.location.hostname}:${window.location.port}`;
}
export const trpcProxyClient = createTRPCProxyClient<AppRouter>({
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have a trpc client in App.tsx. You can access the client in llamaInlineCompletionProvider like this:

// MyMonaco.tsx
function MyMonaco() {
   ...
   const { client } = trpc.useUtils();
   const llamaCompletionProvider = new llamaInlineCompletionProvider(
        id,
        editor,
        client
      );
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

A second thought: since the copilot is already a REST API, and we are not going to further customize it or add authentication to this Desktop app, let's directly call the REST API in the frontend.

The trpc is preferred in the cloud app.

@lihebi
Copy link
Collaborator

lihebi commented Nov 7, 2023

Also, there's an uncaught exception in the console for canceled API call. I'd like to catch it and display a canceling message to keep the console clean.

Screenshot 2023-11-07 at 11 50 33 AM

@lihebi
Copy link
Collaborator

lihebi commented Nov 7, 2023

monitoring the copilot service connection status

This isn't that critical. We can assume that the service is up.

adding infilling mode in addition to autocomplete mode

This is quite important. It is quite often that we edit code in the middle.

@senwang86
Copy link
Collaborator Author

After the discussion, we decide to leave this PR as a reference to integrate the self-hosted copilot and address the comments in the codepod-cloud repo.

@senwang86 senwang86 changed the title [Feature] Self-hosted CodeLlama LLM for code autocompletion [Prototype] Self-hosted CodeLlama LLM for code autocompletion Nov 17, 2023
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