| Title: | Chat and FIM with 'Codestral' |
|---|---|
| Description: | Create an addin in 'Rstudio' to do fill-in-the-middle (FIM) and chat with latest Mistral AI models for coding, 'Codestral' and 'Codestral Mamba'. For more details about 'Mistral AI API': <https://docs.mistral.ai/getting-started/quickstart/> and <https://docs.mistral.ai/api/>. For more details about 'Codestral' model: <https://mistral.ai/news/codestral>; about 'Codestral Mamba': <https://mistral.ai/news/codestral-mamba>. |
| Authors: | Marc Grossouvre [aut, cre], URBS company [cph, fnd] |
| Maintainer: | Marc Grossouvre <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.2.9000 |
| Built: | 2026-05-23 08:19:46 UTC |
| Source: | https://github.com/urbs-dev/codestral |
Allow codestral to detect package environment
allow_detect_package(x = TRUE)allow_detect_package(x = TRUE)
x |
A boolean indicating that the user allows the detection of a
package environment. Defaulted to |
If set to TRUE, when codestral is used in a folder with a DESCRIPTION
file, all R files in the current folder and its subfolders are included
in the prompt.
0 invisible.
This function completes a given prompt using the Codestral API. It supports different models for fill-in-the-middle, chat with Codestral, and chat with Codestral Mamba. The function relies on environment variables for some parameters.
codestral( prompt, suffix = "", path = NULL, mistral_apikey = Sys.getenv(x = "R_MISTRAL_APIKEY"), codestral_apikey = Sys.getenv(x = "R_CODESTRAL_APIKEY"), fim_model = Sys.getenv(x = "R_CODESTRAL_FIM_MODEL"), chat_model = Sys.getenv(x = "R_CODESTRAL_CHAT_MODEL"), mamba_model = Sys.getenv(x = "R_MAMBA_CHAT_MODEL"), temperature = as.integer(Sys.getenv(x = "R_CODESTRAL_TEMPERATURE")), max_tokens_FIM = Sys.getenv(x = "R_CODESTRAL_MAX_TOKENS_FIM"), max_tokens_chat = Sys.getenv(x = "R_CODESTRAL_MAX_TOKENS_CHAT"), role_content = Sys.getenv(x = "R_CODESTRAL_ROLE_CONTENT") )codestral( prompt, suffix = "", path = NULL, mistral_apikey = Sys.getenv(x = "R_MISTRAL_APIKEY"), codestral_apikey = Sys.getenv(x = "R_CODESTRAL_APIKEY"), fim_model = Sys.getenv(x = "R_CODESTRAL_FIM_MODEL"), chat_model = Sys.getenv(x = "R_CODESTRAL_CHAT_MODEL"), mamba_model = Sys.getenv(x = "R_MAMBA_CHAT_MODEL"), temperature = as.integer(Sys.getenv(x = "R_CODESTRAL_TEMPERATURE")), max_tokens_FIM = Sys.getenv(x = "R_CODESTRAL_MAX_TOKENS_FIM"), max_tokens_chat = Sys.getenv(x = "R_CODESTRAL_MAX_TOKENS_CHAT"), role_content = Sys.getenv(x = "R_CODESTRAL_ROLE_CONTENT") )
prompt |
The prompt to complete. |
suffix |
The suffix to use. Defaults to an empty string. |
path |
The path to the current file. Defaults to |
mistral_apikey, codestral_apikey
|
The API keys to use for accessing
Codestral Mamba and Codestral. Default to the value of the
|
fim_model |
The model to use for fill-in-the-middle. Defaults to the
value of the |
chat_model |
The model to use for chat with Codestral. Defaults to the
value of the |
mamba_model |
The model to use for chat with Codestral Mamba. Defaults to the
value of the |
temperature |
The temperature to use. Defaults to the value of the
|
max_tokens_FIM, max_tokens_chat
|
Integers giving the maximum number of
tokens to generate for FIM and chat. Defaults to the value of the
|
role_content |
The role content to use. Defaults to the value of the
|
A character string containing the completed text.
Create environment variables for operationg FIM and chat.
codestral_init( mistral_apikey = Sys.getenv(x = "R_MISTRAL_APIKEY"), codestral_apikey = Sys.getenv(x = "R_CODESTRAL_APIKEY"), fim_model = "codestral-latest", chat_model = "codestral-latest", mamba_model = "open-codestral-mamba", temperature = 0, max_tokens_FIM = 100, max_tokens_chat = "", role_content = NULL )codestral_init( mistral_apikey = Sys.getenv(x = "R_MISTRAL_APIKEY"), codestral_apikey = Sys.getenv(x = "R_CODESTRAL_APIKEY"), fim_model = "codestral-latest", chat_model = "codestral-latest", mamba_model = "open-codestral-mamba", temperature = 0, max_tokens_FIM = 100, max_tokens_chat = "", role_content = NULL )
mistral_apikey, codestral_apikey
|
The API keys to use for accessing
Codestral Mamba and Codestral. Default to the value of the
|
fim_model |
A string giving the model to use for FIM. |
chat_model |
A string giving the model to use for Codestral chat. |
mamba_model |
A string giving the model to use for Codestral Mamba chat. |
temperature |
An integer giving the temperature to use. |
max_tokens_FIM, max_tokens_chat
|
Integers giving the maximum number of tokens to generate for each of these operations. |
role_content |
A role to assign to the system Default is "You write programs in R language only. You adopt a proper coding approach by strictly naming all the functions' parameters when calling any function with named parameters even when calling nested functions, by being straighforward in your answers." |
The most important paremeters here are the ..._apikey parameters
without which the Mistral AI API can not be used.
To start with, beginners may keep default values for other parameters. It
seems sound to use the latest models of each type. However with time, the
user may be willing to customize temperature, max_tokens_FIM, max_tokens_chat and
role_content for his/her own needs.
This function creates the following environment variables using Sys.setenv():
R_MISTRAL_APIKEY
R_CODESTRAL_APIKEY
R_CODESTRAL_FIM_MODEL
R_CODESTRAL_CHAT_MODEL
R_CODESTRAL_MAMBA_MODEL
R_CODESTRAL_TEMPERATURE
R_CODESTRAL_MAX_TOKENS_FIM
R_CODESTRAL_MAX_TOKENS_CHAT
R_CODESTRAL_DEBUG
R_CODESTRAL_DETECT_PACKAGE
R_CODESTRAL_ROLE_CONTENT
Invisible 0.
Analyses a prompt to re-buid the dialog
compile_dialog(prompt)compile_dialog(prompt)
prompt |
The prompt to analyse. A vector of strings. |
A list with the chatter (Codestral or Codestral Mamba) and the dialog in a data.frame whith columns role and content.
This function splits the current script into two parts: the part before the cursor and the part after the cursor.
complete_current_script()complete_current_script()
A character vector containing the two parts of the script.
Set debug mode
debug_mode(debug = TRUE)debug_mode(debug = TRUE)
debug |
A logical value. If |
When the debug mode is activated, the function codestral() will
print the request body and the response body.
Invisible 0.
Detect whether the working directory is that of a package
detect_package()detect_package()
TRUE if the working directory is that of a package, FALSE otherwise
Endpoints for the Codestral API.
ENDPOINTSENDPOINTS
A named list with elements chat and completion.
Read and include files in a prompt
include_file(prompt, anyFile)include_file(prompt, anyFile)
prompt |
A vector of strings. |
anyFile |
A boolean of the same length of prompt indicating that an instruction |
If anyFile[i] is TRUE then the sequence of characters following the instruction "ff:" in prompt[i] is read until the next space or the end of the string. This extracted string is assumed to be a file name. This file is looked for in the current working directory or any of its sub-directories. Once detected, the file is read with readLines() and this content is inserted in prompt between prompt[i-1] and prompt[i+1]. Note that prompt[i] is therefore deleted.
The result is returned.
A vector of strings containing prompt augmented by the files refered to in the original prompt.
This function includes R files from a package in the prompt when a package is detected. It handles the inclusion of file contents and path-specific exclusions.
include_package_files(prompt, path)include_package_files(prompt, path)
prompt |
The current prompt to which package files should be added. |
path |
Current file path to exclude from the included files. |
The modified prompt with package file contents included.
This function inserts a Codestral FIM into the current script.
insert_addin()insert_addin()
0 (invisible).
This function returns a data frame with the file names and paths of all R files in the current directory.
inventory_Rfiles()inventory_Rfiles()
A data frame with the file names and paths of all R files in the current directory.
inventory_Rfiles()inventory_Rfiles()