|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use anyhow::anyhow; |
| 4 | +use mistralrs::{self, TextMessageRole}; |
| 5 | +use serde::Serialize; |
| 6 | + |
| 7 | +use crate::base::json_schema::ToJsonSchema; |
| 8 | +use crate::ops::sdk::*; |
| 9 | + |
| 10 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 11 | +pub struct MistralModelSpec { |
| 12 | + model_id: String, |
| 13 | + isq_type: mistralrs::IsqType, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | +pub struct Spec { |
| 18 | + model: MistralModelSpec, |
| 19 | + output_type: EnrichedValueType, |
| 20 | + instructions: Option<String>, |
| 21 | +} |
| 22 | + |
| 23 | +struct Executor { |
| 24 | + model: mistralrs::Model, |
| 25 | + output_type: EnrichedValueType, |
| 26 | + request_base: mistralrs::RequestBuilder, |
| 27 | +} |
| 28 | + |
| 29 | +fn get_system_message(instructions: &Option<String>) -> String { |
| 30 | + let mut message = |
| 31 | + "You are a helpful assistant that extracts structured information from text. \ |
| 32 | +Your task is to analyze the input text and output valid JSON that matches the specified schema. \ |
| 33 | +Be precise and only include information that is explicitly stated in the text. \ |
| 34 | +Output only the JSON without any additional messages or explanations." |
| 35 | + .to_string(); |
| 36 | + |
| 37 | + if let Some(custom_instructions) = instructions { |
| 38 | + message.push_str("\n\n"); |
| 39 | + message.push_str(custom_instructions); |
| 40 | + } |
| 41 | + |
| 42 | + message |
| 43 | +} |
| 44 | + |
| 45 | +impl Executor { |
| 46 | + async fn new(spec: Spec) -> Result<Self> { |
| 47 | + let model = mistralrs::TextModelBuilder::new(spec.model.model_id) |
| 48 | + .with_isq(spec.model.isq_type) |
| 49 | + .with_logging() |
| 50 | + .with_paged_attn(|| mistralrs::PagedAttentionMetaBuilder::default().build())? |
| 51 | + .build() |
| 52 | + .await?; |
| 53 | + let request_base = mistralrs::RequestBuilder::new() |
| 54 | + .set_constraint(mistralrs::Constraint::JsonSchema(serde_json::to_value( |
| 55 | + spec.output_type.to_json_schema(), |
| 56 | + )?)) |
| 57 | + .set_deterministic_sampler() |
| 58 | + .add_message( |
| 59 | + TextMessageRole::System, |
| 60 | + get_system_message(&spec.instructions), |
| 61 | + ); |
| 62 | + Ok(Self { |
| 63 | + model, |
| 64 | + output_type: spec.output_type, |
| 65 | + request_base, |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[async_trait] |
| 71 | +impl SimpleFunctionExecutor for Executor { |
| 72 | + fn behavior_version(&self) -> Option<u32> { |
| 73 | + Some(1) |
| 74 | + } |
| 75 | + |
| 76 | + fn enable_cache(&self) -> bool { |
| 77 | + true |
| 78 | + } |
| 79 | + |
| 80 | + async fn evaluate(&self, input: Vec<Value>) -> Result<Value> { |
| 81 | + let text = input.iter().next().unwrap().as_str()?; |
| 82 | + let request = self |
| 83 | + .request_base |
| 84 | + .clone() |
| 85 | + .add_message(TextMessageRole::User, text); |
| 86 | + let response = self.model.send_chat_request(request).await?; |
| 87 | + let response_text = response.choices[0] |
| 88 | + .message |
| 89 | + .content |
| 90 | + .as_ref() |
| 91 | + .ok_or_else(|| anyhow!("No content in response"))?; |
| 92 | + let json_value: serde_json::Value = serde_json::from_str(response_text)?; |
| 93 | + let value = Value::from_json(json_value, &self.output_type.typ)?; |
| 94 | + Ok(value) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +pub struct Factory; |
| 99 | + |
| 100 | +#[async_trait] |
| 101 | +impl SimpleFunctionFactoryBase for Factory { |
| 102 | + type Spec = Spec; |
| 103 | + |
| 104 | + fn name(&self) -> &str { |
| 105 | + "ExtractByMistral" |
| 106 | + } |
| 107 | + |
| 108 | + fn get_output_schema( |
| 109 | + &self, |
| 110 | + spec: &Spec, |
| 111 | + input_schema: &Vec<OpArgSchema>, |
| 112 | + _context: &FlowInstanceContext, |
| 113 | + ) -> Result<EnrichedValueType> { |
| 114 | + match &expect_input_1(input_schema)?.value_type.typ { |
| 115 | + ValueType::Basic(BasicValueType::Str) => {} |
| 116 | + t => { |
| 117 | + api_bail!("Expect String as input type, got {}", t) |
| 118 | + } |
| 119 | + } |
| 120 | + Ok(spec.output_type.clone()) |
| 121 | + } |
| 122 | + |
| 123 | + async fn build_executor( |
| 124 | + self: Arc<Self>, |
| 125 | + spec: Spec, |
| 126 | + _input_schema: Vec<OpArgSchema>, |
| 127 | + _context: Arc<FlowInstanceContext>, |
| 128 | + ) -> Result<Box<dyn SimpleFunctionExecutor>> { |
| 129 | + Ok(Box::new(Executor::new(spec).await?)) |
| 130 | + } |
| 131 | +} |
0 commit comments