mirror of
https://github.com/huggingface/text-generation-inference.git
synced 2025-09-11 12:24:53 +00:00
parse string to json
This commit is contained in:
parent
bada345055
commit
19dd0a8fde
@ -1,7 +1,9 @@
|
|||||||
/// Payload validation logic
|
/// Payload validation logic
|
||||||
use crate::validation::ValidationError::{BestOfSampling, BestOfSeed, EmptyInput};
|
use crate::validation::ValidationError::{BestOfSampling, BestOfSeed, EmptyInput};
|
||||||
use crate::{GenerateParameters, GenerateRequest, GrammarType};
|
use crate::{GenerateParameters, GenerateRequest, GrammarType};
|
||||||
|
use jsonschema::{Draft, JSONSchema};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
use serde_json::Value;
|
||||||
use text_generation_client::{
|
use text_generation_client::{
|
||||||
GrammarType as ProtoGrammarType, NextTokenChooserParameters, StoppingCriteriaParameters,
|
GrammarType as ProtoGrammarType, NextTokenChooserParameters, StoppingCriteriaParameters,
|
||||||
};
|
};
|
||||||
@ -313,14 +315,24 @@ impl Validation {
|
|||||||
return Err(ValidationError::Grammar);
|
return Err(ValidationError::Grammar);
|
||||||
}
|
}
|
||||||
match grammar {
|
match grammar {
|
||||||
// currently both are handled the same way since compilation is done in Python
|
|
||||||
GrammarType::Json(json) => {
|
GrammarType::Json(json) => {
|
||||||
// JSONSchema::options()
|
let json = match json {
|
||||||
// .with_draft(Draft::Draft202012)
|
// if value is a string, we need to parse it again to make sure its
|
||||||
// .compile(&json)
|
// a valid json
|
||||||
// .map_err(|e| ValidationError::InvalidGrammar(e.to_string()))?;
|
Value::String(s) => serde_json::from_str(&s)
|
||||||
|
.map_err(|e| ValidationError::InvalidGrammar(e.to_string())),
|
||||||
|
Value::Object(_) => Ok(json),
|
||||||
|
_ => Err(ValidationError::Grammar),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
// Check if the json is a valid JSONSchema
|
||||||
|
JSONSchema::options()
|
||||||
|
.with_draft(Draft::Draft202012)
|
||||||
|
.compile(&json)
|
||||||
|
.map_err(|e| ValidationError::InvalidGrammar(e.to_string()))?;
|
||||||
|
|
||||||
(
|
(
|
||||||
|
// Serialize json to string
|
||||||
serde_json::to_string(&json)
|
serde_json::to_string(&json)
|
||||||
.map_err(|e| ValidationError::InvalidGrammar(e.to_string()))?,
|
.map_err(|e| ValidationError::InvalidGrammar(e.to_string()))?,
|
||||||
ProtoGrammarType::Json.into(),
|
ProtoGrammarType::Json.into(),
|
||||||
@ -497,7 +509,7 @@ pub enum ValidationError {
|
|||||||
Tokenizer(String),
|
Tokenizer(String),
|
||||||
#[error("grammar is not supported")]
|
#[error("grammar is not supported")]
|
||||||
Grammar,
|
Grammar,
|
||||||
#[error("grammar is not a valid JSONSchema: {0}")]
|
#[error("grammar is not valid: {0}")]
|
||||||
InvalidGrammar(String),
|
InvalidGrammar(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user