diff --git a/llama_cpp/_internals.py b/llama_cpp/_internals.py index b45d34b2c4..5c4c0c3e6f 100644 --- a/llama_cpp/_internals.py +++ b/llama_cpp/_internals.py @@ -298,20 +298,24 @@ def kv_cache_clear(self): llama_cpp.llama_memory_clear(self.memory, True) def kv_cache_seq_rm(self, seq_id: int, p0: int, p1: int) -> bool: - assert self.memory is not None, "Memory is not initialized" + if self.memory is None: + return True seq_id = seq_id if seq_id >= 0 else 0 return llama_cpp.llama_memory_seq_rm(self.memory, seq_id, p0, p1) def kv_cache_seq_cp(self, seq_id_src: int, seq_id_dst: int, p0: int, p1: int): - assert self.memory is not None, "Memory is not initialized" + if self.memory is None: + return llama_cpp.llama_memory_seq_cp(self.memory, seq_id_src, seq_id_dst, p0, p1) def kv_cache_seq_keep(self, seq_id: int): - assert self.memory is not None, "Memory is not initialized" + if self.memory is None: + return llama_cpp.llama_memory_seq_keep(self.memory, seq_id) def kv_cache_seq_shift(self, seq_id: int, p0: int, p1: int, shift: int): - assert self.memory is not None, "Memory is not initialized" + if self.memory is None: + return llama_cpp.llama_memory_seq_add(self.memory, seq_id, p0, p1, shift) def get_state_size(self) -> int: diff --git a/llama_cpp/llama.py b/llama_cpp/llama.py index 14e2f8500f..a0022fbcb4 100644 --- a/llama_cpp/llama.py +++ b/llama_cpp/llama.py @@ -2140,6 +2140,278 @@ def create_chat_completion_openai_v1( "You can install it with `pip install openai`." ) + def create_system_one( + self, + state: Union[str, Dict[str, Any], List[Any]], + questions: Dict[str, SystemOneQuestion], + temperature: Optional[float] = None, + model: Optional[str] = None, + ) -> SystemOneResponse: + """Evaluate a state and questions using a decision model in a single forward pass. + + Args: + state: The input context or state (text string or JSON object/list). + questions: Dictionary mapping question names to typed question specs. + temperature: Softmax temperature for normalizing probabilities. If None, + uses calibrated temperature from model metadata if available. + model: Optional model name for the response payload. + + Returns: + A SystemOneResponse containing calibrated probabilities and choices. + """ + model_name: str = model if model is not None else self.model_path + + # 1. Look for a "systemone" chat template in model metadata + systemone_template = None + try: + tmpl_bytes = llama_cpp.llama_model_chat_template( + self._model.model, b"systemone" + ) + if tmpl_bytes: + systemone_template = tmpl_bytes.decode("utf-8") + except Exception: + pass + + if ( + systemone_template is None + and "tokenizer.chat_template.systemone" in self.metadata + ): + systemone_template = self.metadata["tokenizer.chat_template.systemone"] + + jinja_template = None + if systemone_template: + import jinja2 + from jinja2.sandbox import ImmutableSandboxedEnvironment + + def _tojson(val: Any, indent: Optional[int] = None) -> str: + return json.dumps(val, indent=indent) + + env = ImmutableSandboxedEnvironment( + loader=jinja2.BaseLoader(), + trim_blocks=True, + lstrip_blocks=True, + ) + env.filters["tojson"] = _tojson + jinja_template = env.from_string(systemone_template) + + total_input_tokens = 0 + answers: Dict[str, SystemOneAnswer] = {} + + def _get_candidate_logits(logits_arr: np.ndarray, candidates: List[str]) -> List[float]: + bare_list: List[float] = [] + space_list: List[float] = [] + for c in candidates: + b_tok = self.tokenize(c.encode("utf-8"), add_bos=False) + s_tok = self.tokenize(f" {c}".encode("utf-8"), add_bos=False) + bare_list.append(float(logits_arr[b_tok[0]]) if b_tok else float("-inf")) + space_list.append(float(logits_arr[s_tok[0]]) if s_tok else float("-inf")) + + # Use the token format (bare vs space-prefixed) that has higher overall probability mass + valid_bare = [x for x in bare_list if x != float("-inf")] + valid_space = [x for x in space_list if x != float("-inf")] + mean_bare = sum(valid_bare) / len(valid_bare) if valid_bare else float("-inf") + mean_space = sum(valid_space) / len(valid_space) if valid_space else float("-inf") + return space_list if mean_space > mean_bare else bare_list + + def _get_candidate_logit(logits_arr: np.ndarray, text: str) -> float: + cand_logits = [] + bare_toks = self.tokenize(text.encode("utf-8"), add_bos=False) + if bare_toks: + cand_logits.append(float(logits_arr[bare_toks[0]])) + space_toks = self.tokenize(f" {text}".encode("utf-8"), add_bos=False) + if space_toks: + cand_logits.append(float(logits_arr[space_toks[0]])) + return max(cand_logits) if cand_logits else float("-inf") + + def _decode_prompt(tokens: Sequence[int]): + self._ctx.kv_cache_clear() + self._batch.reset() + self._batch.set_batch( + batch=tokens, + n_past=0, + logits_all=False, + ) + self._ctx.decode(self._batch) + + for q_name, q_spec in questions.items(): + q_type = q_spec.get("type", "choice") + + # Determine temperature + if temperature is not None: + temp = max(temperature, 1e-4) + else: + meta_temp = self.metadata.get(f"lfm3.decision.temperature.{q_type}") or self.metadata.get(f"decision.temperature.{q_type}") + temp = float(meta_temp) if meta_temp is not None else 1.0 + temp = max(temp, 1e-4) + + if q_type == "choice": + criteria = q_spec.get("criteria", {}) + choice_keys = list(criteria.keys()) + options = [] + for idx, k in enumerate(choice_keys): + label = chr(65 + idx) if idx < 26 else str(idx) + desc = criteria[k] + options.append({"label": label, "key": k, "description": desc}) + + if jinja_template is not None: + formatted_prompt = jinja_template.render( + images=[], + state=state, + instructions=q_spec.get("instructions", ""), + type="choice", + options=options, + ) + else: + lines = [f"State:\n{state}\n\nQuestion:\n{q_spec.get('instructions', '')}\n\nOptions:"] + for o in options: + lines.append(f"{o['label']} {o['description']}") + lines.append("\nReply with the option code only.") + formatted_prompt = "\n".join(lines) + + tokens = self.tokenize(formatted_prompt.encode("utf-8"), add_bos=False, special=True) + total_input_tokens += len(tokens) + _decode_prompt(tokens) + + logits = llama_cpp.llama_get_logits(self._ctx.ctx) + logits_arr = np.ctypeslib.as_array(logits, shape=(self.n_vocab(),)) + + label_logits = _get_candidate_logits(logits_arr, [o["label"] for o in options]) + max_l = max(label_logits) if label_logits else 0.0 + exp_logits = [np.exp((l - max_l) / temp) for l in label_logits] + sum_exp = sum(exp_logits) if sum(exp_logits) > 0 else 1.0 + probs = {o["key"]: float(e / sum_exp) for o, e in zip(options, exp_logits)} + + best_choice = max(probs.items(), key=lambda x: x[1])[0] if probs else "" + confidence = probs.get(best_choice, 0.0) + + # For multi-choice, pick all options above threshold (>= 0.15) + is_multiple = bool(q_spec.get("multiple", False)) + selected_choices = [k for k, p in probs.items() if p >= 0.15] + if not selected_choices and best_choice: + selected_choices = [best_choice] + + ans_dict: SystemOneChoiceAnswer = { + "type": "choice", + "choice": best_choice, + "confidence": round(confidence, 4), + "probabilities": {k: round(v, 4) for k, v in probs.items()}, + } + if is_multiple: + ans_dict["choices"] = selected_choices + + answers[q_name] = ans_dict + + elif q_type == "score": + criteria_list = q_spec.get("criteria", []) + n_levels = len(criteria_list) + options = [{"label": str(i), "key": str(i), "description": desc} for i, desc in enumerate(criteria_list)] + + if jinja_template is not None: + formatted_prompt = jinja_template.render( + images=[], + state=state, + instructions=q_spec.get("instructions", ""), + type="score", + options=options, + ) + else: + lines = [f"State:\n{state}\n\nQuestion:\n{q_spec.get('instructions', '')}\n\n"] + for o in options: + lines.append(f"{o['key']} {o['description']}") + lines.append(f"\nReply with a single digit 0-{n_levels - 1} only.") + formatted_prompt = "\n".join(lines) + + tokens = self.tokenize(formatted_prompt.encode("utf-8"), add_bos=False, special=True) + total_input_tokens += len(tokens) + _decode_prompt(tokens) + + logits = llama_cpp.llama_get_logits(self._ctx.ctx) + logits_arr = np.ctypeslib.as_array(logits, shape=(self.n_vocab(),)) + + score_logits = _get_candidate_logits(logits_arr, [str(i) for i in range(n_levels)]) + max_l = max(score_logits) if score_logits else 0.0 + exp_logits = [np.exp((l - max_l) / temp) for l in score_logits] + sum_exp = sum(exp_logits) if sum(exp_logits) > 0 else 1.0 + probs_dict = {str(i): float(e / sum_exp) for i, e in enumerate(exp_logits)} + + # Calculate median score level matching Liquid AI / server-decision spec + total_prob = sum(probs_dict.values()) or 1.0 + mass = 0.0 + median_idx = n_levels - 1 + for i in range(n_levels): + mass += probs_dict[str(i)] / total_prob + if mass >= 0.5: + median_idx = i + break + + max_conf = max(probs_dict.values()) if probs_dict else 0.0 + top_idx_str = max(probs_dict.items(), key=lambda x: x[1])[0] if probs_dict else "0" + top_idx = int(top_idx_str) + top_label = criteria_list[median_idx] if median_idx < len(criteria_list) else "" + legend = {str(i): crit for i, crit in enumerate(criteria_list)} + + answers[q_name] = { + "type": "score", + "score": float(median_idx), + "level": median_idx, + "label": top_label, + "confidence": round(max_conf, 4), + "legend": legend, + "probabilities": {k: round(v, 4) for k, v in probs_dict.items()}, + } + + elif q_type == "noul": + criteria = q_spec.get("criteria", {}) + options = [ + {"label": "true", "key": "true", "description": criteria.get("true", None)}, + {"label": "false", "key": "false", "description": criteria.get("false", None)}, + ] + + if jinja_template is not None: + formatted_prompt = jinja_template.render( + images=[], + state=state, + instructions=q_spec.get("instructions", ""), + type="noul", + options=options, + ) + else: + lines = [f"State:\n{state}\n\nQuestion:\n{q_spec.get('instructions', '')}\n\nReply with yes or no only."] + formatted_prompt = "\n".join(lines) + + tokens = self.tokenize(formatted_prompt.encode("utf-8"), add_bos=False, special=True) + total_input_tokens += len(tokens) + _decode_prompt(tokens) + + logits = llama_cpp.llama_get_logits(self._ctx.ctx) + logits_arr = np.ctypeslib.as_array(logits, shape=(self.n_vocab(),)) + + l_yes = max(_get_candidate_logit(logits_arr, "yes"), _get_candidate_logit(logits_arr, "Yes")) + l_no = max(_get_candidate_logit(logits_arr, "no"), _get_candidate_logit(logits_arr, "No")) + + max_l = max(l_yes, l_no) + e_yes = np.exp((l_yes - max_l) / temp) + e_no = np.exp((l_no - max_l) / temp) + p_yes = float(e_yes / (e_yes + e_no)) + + answers[q_name] = { + "type": "noul", + "noul": round(p_yes, 4), + } + + return { + "model": model_name, + "answers": answers, + "usage": { + "input_tokens": total_input_tokens, + "output_tokens": 0, + }, + } + + # Alias to match HuggingFace Transformers model.system_one(state, questions) convention + system_one = create_system_one + + def __getstate__(self): return dict( model_path=self.model_path, diff --git a/llama_cpp/llama_types.py b/llama_cpp/llama_types.py index f647822ff5..2015ac58af 100644 --- a/llama_cpp/llama_types.py +++ b/llama_cpp/llama_types.py @@ -314,3 +314,70 @@ class ChatCompletionNamedToolChoice(TypedDict): ChatCompletionStreamResponse = CreateChatCompletionStreamResponse ChatCompletionResponseFunction = ChatCompletionFunction ChatCompletionFunctionCall = ChatCompletionResponseFunctionCall + + +# System One / Decision Model types + + +class SystemOneChoiceQuestion(TypedDict): + type: Literal["choice"] + instructions: str + criteria: Dict[str, str] + multiple: NotRequired[bool] + + +class SystemOneScoreQuestion(TypedDict): + type: Literal["score"] + instructions: str + criteria: List[str] + + +class SystemOneNoulQuestion(TypedDict): + type: Literal["noul"] + instructions: str + criteria: NotRequired[Dict[str, str]] + + +SystemOneQuestion = Union[ + SystemOneChoiceQuestion, SystemOneScoreQuestion, SystemOneNoulQuestion +] + + +class SystemOneChoiceAnswer(TypedDict): + type: Literal["choice"] + choice: str + choices: NotRequired[List[str]] + confidence: float + probabilities: Dict[str, float] + + +class SystemOneScoreAnswer(TypedDict): + type: Literal["score"] + score: float + level: NotRequired[int] + label: NotRequired[str] + confidence: float + probabilities: Dict[str, float] + legend: NotRequired[Dict[str, str]] + + +class SystemOneNoulAnswer(TypedDict): + type: Literal["noul"] + noul: float + + +SystemOneAnswer = Union[ + SystemOneChoiceAnswer, SystemOneScoreAnswer, SystemOneNoulAnswer +] + + +class SystemOneUsage(TypedDict): + input_tokens: int + output_tokens: int + + +class SystemOneResponse(TypedDict): + model: str + answers: Dict[str, SystemOneAnswer] + usage: SystemOneUsage + diff --git a/llama_cpp/server/app.py b/llama_cpp/server/app.py index f776fe159c..ce8722837d 100644 --- a/llama_cpp/server/app.py +++ b/llama_cpp/server/app.py @@ -41,6 +41,7 @@ TokenizeInputCountResponse, DetokenizeInputRequest, DetokenizeInputResponse, + SystemOneRequest, ) from llama_cpp.server.errors import RouteErrorHandler @@ -373,6 +374,23 @@ async def create_embedding( ) +@router.post( + "/v1/systemone", + summary="System One Decision Evaluation", + dependencies=[Depends(authenticate)], + response_model=llama_cpp.SystemOneResponse, +) +async def create_system_one( + request: SystemOneRequest, + llama_proxy: LlamaProxy = Depends(get_llama_proxy), +): + return await run_in_threadpool( + llama_proxy(request.model).create_system_one, + **request.model_dump(), + ) + + + @router.post( "/v1/chat/completions", summary="Chat", diff --git a/llama_cpp/server/types.py b/llama_cpp/server/types.py index eda2b92801..61220b68f2 100644 --- a/llama_cpp/server/types.py +++ b/llama_cpp/server/types.py @@ -314,3 +314,40 @@ class DetokenizeInputResponse(BaseModel): model_config = { "json_schema_extra": {"example": {"text": "How many tokens in this query?"}} } + + +class SystemOneRequest(BaseModel): + model: Optional[str] = model_field + state: Union[str, Dict[str, Any], List[Any]] = Field( + description="The context or input state to evaluate." + ) + questions: Dict[str, Dict[str, Any]] = Field( + description="Dictionary mapping question keys to typed questions." + ) + temperature: Optional[float] = Field( + default=1.0, + description="Softmax temperature for scaling decision logits.", + ) + + model_config = { + "json_schema_extra": { + "example": { + "state": "The user reported an error on the checkout page.", + "questions": { + "is_bug": { + "type": "noul", + "instructions": "Is the user reporting a bug?", + }, + "team": { + "type": "choice", + "instructions": "Which department should handle this?", + "criteria": { + "billing": "Payment and checkout issues", + "support": "General questions", + }, + }, + }, + } + } + } + diff --git a/tests/test_system_one.py b/tests/test_system_one.py new file mode 100644 index 0000000000..5d338213e6 --- /dev/null +++ b/tests/test_system_one.py @@ -0,0 +1,72 @@ +import json +from typing import Dict +from llama_cpp.llama_types import ( + SystemOneQuestion, + SystemOneResponse, +) + + +def test_system_one_types(): + questions: Dict[str, SystemOneQuestion] = { + "intent": { + "type": "choice", + "instructions": "Identify the primary topic.", + "criteria": { + "shipping": "Lost, delayed, or missing packages", + "refund": "Requesting money back", + }, + }, + "is_urgent": { + "type": "noul", + "instructions": "Is this urgent?", + }, + "severity": { + "type": "score", + "instructions": "Rate severity 0 to 2.", + "criteria": ["Low", "Medium", "High"], + }, + } + + payload = json.dumps({"state": "My order is delayed", "questions": questions}) + loaded = json.loads(payload) + assert loaded["questions"]["intent"]["type"] == "choice" + assert loaded["questions"]["is_urgent"]["type"] == "noul" + assert loaded["questions"]["severity"]["type"] == "score" + + +def test_system_one_response_structure(): + response: SystemOneResponse = { + "model": "d1-Omni-2B-GGUF", + "answers": { + "intent": { + "type": "choice", + "choice": "shipping", + "confidence": 0.94, + "probabilities": {"shipping": 0.94, "refund": 0.06}, + }, + "is_urgent": { + "type": "noul", + "noul": 0.82, + }, + "severity": { + "type": "score", + "score": 1.2, + "confidence": 0.7, + "legend": {"0": "Low", "1": "Medium", "2": "High"}, + "probabilities": {"0": 0.1, "1": 0.6, "2": 0.3}, + }, + }, + "usage": { + "input_tokens": 85, + "output_tokens": 0, + }, + } + assert response["answers"]["intent"]["choice"] == "shipping" + assert response["answers"]["is_urgent"]["noul"] == 0.82 + assert response["usage"]["output_tokens"] == 0 + + +if __name__ == "__main__": + test_system_one_types() + test_system_one_response_structure() + print("All System One unit tests passed successfully!")