From a92abe9d895004da642df210c5b8fefc7a6d635e Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Mon, 4 Nov 2024 23:57:08 +0000 Subject: [PATCH] lark_grammar support --- Cargo.lock | 1 + llgtrt/src/routes/completions.rs | 5 +++++ llgtrt/src/routes/openai.rs | 1 + llguidance | 2 +- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 0f089ed..cb77744 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1402,6 +1402,7 @@ dependencies = [ "instant", "jsonschema", "lazy_static", + "regex-syntax", "rustc-hash 2.0.0", "serde", "serde_json", diff --git a/llgtrt/src/routes/completions.rs b/llgtrt/src/routes/completions.rs index 0e4b535..cc1ee20 100644 --- a/llgtrt/src/routes/completions.rs +++ b/llgtrt/src/routes/completions.rs @@ -8,6 +8,7 @@ use axum::response::{IntoResponse, Response}; use axum::Json; use futures_core::Stream; use llguidance_parser::api::{RegexSpec, TopLevelGrammar}; +use llguidance_parser::lark::{lark_to_llguidance, parse_lark}; use llguidance_parser::{Constraint, GrammarBuilder, JsonCompileOptions}; use serde_json::{json, Value}; use std::fmt::Display; @@ -174,6 +175,10 @@ fn llg_grammar(params: &CommonCreateParams) -> Result> { log::debug!("using strict JSON schema"); json_to_llg(schema)? } + Some(ResponseFormat::LarkGrammar { lark_grammar }) => { + log::debug!("using Lark grammar"); + lark_to_llguidance(parse_lark(lark_grammar)?)? + } _ => return Ok(None), }; Ok(Some(grm)) diff --git a/llgtrt/src/routes/openai.rs b/llgtrt/src/routes/openai.rs index 84481b8..e4ae225 100644 --- a/llgtrt/src/routes/openai.rs +++ b/llgtrt/src/routes/openai.rs @@ -277,6 +277,7 @@ pub enum ResponseFormat { Text, JsonObject, JsonSchema { json_schema: JsonSchemaOptions }, + LarkGrammar { lark_grammar: String }, Llguidance { grammar: TopLevelGrammar }, } diff --git a/llguidance b/llguidance index fc950ea..7663176 160000 --- a/llguidance +++ b/llguidance @@ -1 +1 @@ -Subproject commit fc950eae8f34ae70d48e772c0e57b398ff7d960a +Subproject commit 7663176a55490790521e4e4892b59f8f0a2680b1