Lugh

 view release on metacpan or  search on metacpan

lib/Lugh/Prompt.xs  view on Meta::CPAN

        int add_gen, add_bos;
        parse_apply_args(ax, items, 1, &messages, &add_gen, &add_bos);
        
        RETVAL = do_apply_format(fmt_name, messages, add_gen, add_bos);
        SvREFCNT_dec((SV*)messages);
    OUTPUT:
        RETVAL

# Format a single message
SV*
format_message(self, role, content)
    SV* self
    SV* role
    SV* content
    CODE:
        if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) {
            croak("format_message called on invalid object");
        }
        HV* hv = (HV*)SvRV(self);
        SV** fmt_sv = hv_fetch(hv, "format", 6, 0);
        const char* fmt_name = (fmt_sv && SvOK(*fmt_sv)) ? SvPV_nolen(*fmt_sv) : "chatml";
        
        const PromptFormat* fmt = find_format(fmt_name);
        if (!fmt) {
            croak("Unknown prompt format: %s", fmt_name);
        }
        
        const char* role_str = SvPOK(role) ? SvPV_nolen(role) : "user";
        const char* content_str = SvPOK(content) ? SvPV_nolen(content) : "";
        
        StrBuf buf;
        strbuf_init(&buf);
        
        if (strcmp(role_str, "system") == 0) {
            strbuf_append(&buf, fmt->system_prefix);
            strbuf_append(&buf, content_str);
            strbuf_append(&buf, fmt->system_suffix);
        }
        else if (strcmp(role_str, "user") == 0) {
            strbuf_append(&buf, fmt->user_prefix);
            strbuf_append(&buf, content_str);
            strbuf_append(&buf, fmt->user_suffix);
        }
        else if (strcmp(role_str, "assistant") == 0) {
            strbuf_append(&buf, fmt->assistant_prefix);
            strbuf_append(&buf, content_str);
            strbuf_append(&buf, fmt->assistant_suffix);
        }
        else {
            strbuf_append(&buf, content_str);
        }
        
        RETVAL = newSVpv(buf.data, buf.len);
        strbuf_free(&buf);
    OUTPUT:
        RETVAL

# Return list of available format names
void
available_formats(...)
    PPCODE:
        int i;
        for (i = 0; FORMATS[i].name != NULL; i++) {
            mXPUSHs(newSVpv(FORMATS[i].name, 0));
        }

# Get format name for architecture
SV*
format_for_architecture(...)
    CODE:
        const char* arch_str = NULL;
        /* Handle both class method and function call */
        if (items >= 2) {
            arch_str = SvPOK(ST(1)) ? SvPV_nolen(ST(1)) : NULL;
        } else if (items == 1) {
            arch_str = SvPOK(ST(0)) ? SvPV_nolen(ST(0)) : NULL;
        }
        const char* fmt = format_for_arch(arch_str);
        RETVAL = newSVpv(fmt, 0);
    OUTPUT:
        RETVAL

# Check if format exists
int
has_format(...)
    CODE:
        const char* name_str = NULL;
        if (items >= 2) {
            name_str = SvPOK(ST(1)) ? SvPV_nolen(ST(1)) : NULL;
        } else if (items == 1) {
            name_str = SvPOK(ST(0)) ? SvPV_nolen(ST(0)) : NULL;
        }
        RETVAL = find_format(name_str) != NULL;
    OUTPUT:
        RETVAL

# Get format info as hash
SV*
get_format(...)
    CODE:
        const char* name_str = NULL;
        if (items >= 2) {
            name_str = SvPOK(ST(1)) ? SvPV_nolen(ST(1)) : NULL;
        } else if (items == 1) {
            name_str = SvPOK(ST(0)) ? SvPV_nolen(ST(0)) : NULL;
        }
        const PromptFormat* fmt = find_format(name_str);
        if (!fmt) {
            RETVAL = &PL_sv_undef;
        } else {
            HV* hv = newHV();
            hv_store(hv, "name", 4, newSVpv(fmt->name, 0), 0);
            hv_store(hv, "bos_token", 9, newSVpv(fmt->bos_token, 0), 0);
            hv_store(hv, "eos_token", 9, newSVpv(fmt->eos_token, 0), 0);
            hv_store(hv, "system_prefix", 13, newSVpv(fmt->system_prefix, 0), 0);
            hv_store(hv, "system_suffix", 13, newSVpv(fmt->system_suffix, 0), 0);
            hv_store(hv, "user_prefix", 11, newSVpv(fmt->user_prefix, 0), 0);
            hv_store(hv, "user_suffix", 11, newSVpv(fmt->user_suffix, 0), 0);
            hv_store(hv, "assistant_prefix", 16, newSVpv(fmt->assistant_prefix, 0), 0);
            hv_store(hv, "assistant_suffix", 16, newSVpv(fmt->assistant_suffix, 0), 0);
            hv_store(hv, "generation_prompt", 17, newSVpv(fmt->generation_prompt, 0), 0);



( run in 1.321 second using v1.01-cache-2.11-cpan-5511b514fd6 )