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

Use a chat model instead of LLM for codestral completion #31

Merged
merged 5 commits into from
Feb 10, 2025

Conversation

brichet
Copy link
Collaborator

@brichet brichet commented Jan 28, 2025

Langchainjs warns against using the text completion model, and advises using the chat model instead, which may be more up-to-date (see caution at https://js.langchain.com/docs/integrations/llms/).

This PR replace the text completion model by a chat model in codestral completion.

@brichet brichet added the enhancement New feature or request label Jan 28, 2025
@brichet
Copy link
Collaborator Author

brichet commented Jan 28, 2025

The completion works better when using a MistralAI model designed for code completion, like codestral-latest.

*/
const REQUEST_TIMEOUT = 3000;
const DEFAULT_PROMPT = `
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we can now update this PR to use the default prompts from #28?

@brichet brichet force-pushed the codestral_completion_chat_model branch from 945ae53 to c6884b4 Compare February 6, 2025 14:53
@jtpio
Copy link
Member

jtpio commented Feb 6, 2025

Testing this locally, completions appear to be quite fast, although the response often includes code block delimiters:

image

Over on #27 there is some logic to not discard responses that may include the code block delimiters, but instead process the string to keep the suggestion without the backticks and language name.

So maybe we could isolate that to some util functions so it can be reused across providers.

@jtpio
Copy link
Member

jtpio commented Feb 6, 2025

We can also improve the default completion prompt to mention that responses should be formatted without code block delimiters.

@brichet
Copy link
Collaborator Author

brichet commented Feb 7, 2025

We can also improve the default completion prompt to mention that responses should be formatted without code block delimiters.

I did some tests with these kind of prompts and it doesn't work, looks like it doesn't care of these instructions.
But it worked better on my side with the codestral-latest model, is it the one you tested ?

@jtpio
Copy link
Member

jtpio commented Feb 7, 2025

But it worked better on my side with the codestral-latest model, is it the one you tested ?

Yeah strangely it seems to be returning code blocks quite often?

jupyterlite-ai-codestral-codeblocks.mov

Network response:

{
    "object": "chat.completion",
    "created": 1738938116,
    "model": "codestral-latest",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "tool_calls": null,
                "content": "```python\ndf = pd.DataFrame({\n    'A': [1, 2, 3],\n    'B': [4, 5, 6]\n})\n```"
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 88,
        "total_tokens": 125,
        "completion_tokens": 37
    }
}

@jtpio
Copy link
Member

jtpio commented Feb 7, 2025

Playing around with it a bit more locally, adding more instructions to the prompt like this one seems to be helping a bit:

/**
* The initial prompt to use for the completion.
* Add extra instructions to get better results.
*/
const CUSTOM_SYSTEM_PROMPT = `${COMPLETION_SYSTEM_PROMPT}
Only give raw strings back, do not format the response using backticks!
The output should be a single string, and should correspond to what a human users
would write.
Do not include the prompt in the output, only the string that should be appended to the current input.
`;

But there still seems to be some issues with completions being a little bit "off" by a few characters?

jupyterlite-ai-codestral-test-2.mov

@brichet
Copy link
Collaborator Author

brichet commented Feb 7, 2025

Playing around with it a bit more locally, adding more instructions to the prompt like this one seems to be helping a bit

Thanks for digging into it. Feel free to push in the branch with your changes. Otherwise I can do it.

@jtpio
Copy link
Member

jtpio commented Feb 10, 2025

I pushed a change to improve the default prompt used for completions, so it can also be useful for other providers.

@jtpio
Copy link
Member

jtpio commented Feb 10, 2025

@brichet if this looks good to you, happy to merge as it is right now.

@brichet brichet marked this pull request as ready for review February 10, 2025 10:49
@brichet
Copy link
Collaborator Author

brichet commented Feb 10, 2025

@brichet if this looks good to you, happy to merge as it is right now.

Let's merge it, thank you for the prompt update.

I still have some unexpected suggestions from MistralAI , but it may be more an issue with the model.

Peek.2025-02-10.11-51.webm

@brichet brichet merged commit 4207dd1 into jupyterlite:main Feb 10, 2025
7 checks passed
@brichet brichet deleted the codestral_completion_chat_model branch February 10, 2025 10:53
@jtpio jtpio mentioned this pull request Feb 10, 2025
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.

2 participants