Skip to content

add chatml template? #130

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

Closed
bruceunx opened this issue Mar 4, 2024 · 4 comments
Closed

add chatml template? #130

bruceunx opened this issue Mar 4, 2024 · 4 comments
Labels
🪄 enhancement additions to the software ❓ question

Comments

@bruceunx
Copy link
Contributor

bruceunx commented Mar 4, 2024

hello, do you have any plan to add chatml template in this crate?

@MarcusDunn
Copy link
Contributor

I'm not sure about chatML specifically, but there is work on adding chat templates going on in #127

@MarcusDunn MarcusDunn added 🪄 enhancement additions to the software ❓ question labels Mar 4, 2024
@bruceunx
Copy link
Contributor Author

I implement a script to parse chat template from gguf model, and then use crate minijinja to transform messages to prompts

    let chat_template: String = unsafe {
        // longest known template is about 1200 bytes from llama.cpp
        let chat_temp = CString::new(Vec::<u8>::with_capacity(2048))?;
        let chat_ptr = chat_temp.into_raw();
        let chat_name = CString::new("tokenizer.chat_template")?;
        llama_cpp_sys_2::llama_model_meta_val_str(
            model.model.as_ptr(),
            chat_name.as_ptr(),
            chat_ptr,
            250,
        );
        CString::from_raw(chat_ptr).to_str()?.to_string()
    };

@bruceunx
Copy link
Contributor Author

    let mut env = Environment::new();
    env.add_template("chat_temp", &chat_template)?;

    let temp_eng = env.get_template("chat_temp")?;

    let mut messages: Vec<HashMap<&str, &str>> = Vec::new();

    let mut content1: HashMap<&str, &str> = HashMap::new();

    content1.insert("role", "system");
    content1.insert("content", "you are a helpful AI assistant");

    messages.push(content1);

    let mut content2: HashMap<&str, &str> = HashMap::new();

    content2.insert("role", "user");
    content2.insert("content", "what is your name");

    messages.push(content2);

    let prompt = temp_eng.render(context!(messages => messages, add_generation_prompt => true))?;

transform messages to prompt

@MarcusDunn
Copy link
Contributor

completed in #127 and released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪄 enhancement additions to the software ❓ question
Projects
None yet
Development

No branches or pull requests

2 participants