[docs]classOutlines(LLM):"""LLM wrapper for the Outlines library."""client:Any=None# :meta private:model:str"""Identifier for the model to use with Outlines. The model identifier should be a string specifying: - A Hugging Face model name (e.g., "meta-llama/Llama-2-7b-chat-hf") - A local path to a model - For GGUF models, the format is "repo_id/file_name" (e.g., "TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf") Examples: - "TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf" - "meta-llama/Llama-2-7b-chat-hf" """backend:Literal["llamacpp","transformers","transformers_vision","vllm","mlxlm"]="transformers""""Specifies the backend to use for the model. Supported backends are: - "llamacpp": For GGUF models using llama.cpp - "transformers": For Hugging Face Transformers models (default) - "transformers_vision": For vision-language models (e.g., LLaVA) - "vllm": For models using the vLLM library - "mlxlm": For models using the MLX framework Note: Ensure you have the necessary dependencies installed for the chosen backend. The system will attempt to import required packages and may raise an ImportError if they are not available. """max_tokens:int=256"""The maximum number of tokens to generate."""stop:Optional[List[str]]=None"""A list of strings to stop generation when encountered."""streaming:bool=True"""Whether to stream the results, token by token."""regex:Optional[str]=Noner"""Regular expression for structured generation. If provided, Outlines will guarantee that the generated text matches this regex. This can be useful for generating structured outputs like IP addresses, dates, etc. Example: (valid IP address) regex = r"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)" Note: Computing the regex index can take some time, so it's recommended to reuse the same regex for multiple generations if possible. For more details, see: https://dottxt-ai.github.io/outlines/reference/generation/regex/ """type_constraints:Optional[Union[type,str]]=None"""Type constraints for structured generation. Restricts the output to valid Python types. Supported types include: int, float, bool, datetime.date, datetime.time, datetime.datetime. Example: type_constraints = int For more details, see: https://dottxt-ai.github.io/outlines/reference/generation/format/ """json_schema:Optional[Union[BaseModel,Dict,Callable]]=None"""Pydantic model, JSON Schema, or callable (function signature) for structured JSON generation. Outlines can generate JSON output that follows a specified structure, which is useful for: 1. Parsing the answer (e.g., with Pydantic), storing it, or returning it to a user. 2. Calling a function with the result. You can provide: - A Pydantic model - A JSON Schema (as a Dict) - A callable (function signature) The generated JSON will adhere to the specified structure. For more details, see: https://dottxt-ai.github.io/outlines/reference/generation/json/ """grammar:Optional[str]=None"""Context-free grammar for structured generation. If provided, Outlines will generate text that adheres to the specified grammar. The grammar should be defined in EBNF format. This can be useful for generating structured outputs like mathematical expressions, programming languages, or custom domain-specific languages. Example: grammar = ''' ?start: expression ?expression: term (("+" | "-") term)* ?term: factor (("*" | "/") factor)* ?factor: NUMBER | "-" factor | "(" expression ")" %import common.NUMBER ''' Note: Grammar-based generation is currently experimental and may have performance limitations. It uses greedy generation to mitigate these issues. For more details and examples, see: https://dottxt-ai.github.io/outlines/reference/generation/cfg/ """custom_generator:Optional[Any]=None"""Set your own outlines generator object to override the default behavior."""model_kwargs:Dict[str,Any]=Field(default_factory=dict)"""Additional parameters to pass to the underlying model. Example: model_kwargs = {"temperature": 0.8, "seed": 42} """@model_validator(mode="after")defvalidate_environment(self)->"Outlines":"""Validate that outlines is installed and create a model instance."""num_constraints=sum([bool(self.regex),bool(self.type_constraints),bool(self.json_schema),bool(self.grammar),])ifnum_constraints>1:raiseValueError("Either none or exactly one of regex, type_constraints, ""json_schema, or grammar can be provided.")returnself.build_client()
[docs]defbuild_client(self)->"Outlines":try:importoutlines.modelsasmodelsexceptImportError:raiseImportError("Could not import the Outlines library. ""Please install it with `pip install outlines`.")defcheck_packages_installed(packages:List[Union[str,Tuple[str,str]]],)->None:missing_packages=[pkgifisinstance(pkg,str)elsepkg[0]forpkginpackagesifimportlib.util.find_spec(pkg[1]ifisinstance(pkg,tuple)elsepkg)isNone]ifmissing_packages:raiseImportError(# todo this is displaying wrongf"Missing packages: {', '.join(missing_packages)}. ""You can install them with:\n\n"f" pip install {' '.join(missing_packages)}")ifself.backend=="llamacpp":if".gguf"inself.model:creator,repo_name,file_name=self.model.split("/",2)repo_id=f"{creator}/{repo_name}"else:# todo add auto-file-selection if no file is givenraiseValueError("GGUF file_name must be provided for llama.cpp.")check_packages_installed([("llama-cpp-python","llama_cpp")])self.client=models.llamacpp(repo_id,file_name,**self.model_kwargs)elifself.backend=="transformers":check_packages_installed(["transformers","torch","datasets"])self.client=models.transformers(self.model,**self.model_kwargs)elifself.backend=="transformers_vision":check_packages_installed(["transformers","datasets","torchvision","PIL","flash_attn",])fromtransformersimportLlavaNextForConditionalGenerationifnothasattr(models,"transformers_vision"):raiseValueError("transformers_vision backend is not supported, ""please install the correct outlines version.")self.client=models.transformers_vision(self.model,model_class=LlavaNextForConditionalGeneration,**self.model_kwargs,)elifself.backend=="vllm":ifplatform.system()=="Darwin":raiseValueError("vLLM backend is not supported on macOS.")check_packages_installed(["vllm"])self.client=models.vllm(self.model,**self.model_kwargs)elifself.backend=="mlxlm":check_packages_installed(["mlx"])self.client=models.mlxlm(self.model,**self.model_kwargs)else:raiseValueError(f"Unsupported backend: {self.backend}")returnself
@propertydef_llm_type(self)->str:return"outlines"@propertydef_default_params(self)->Dict[str,Any]:return{"max_tokens":self.max_tokens,"stop_at":self.stop,**self.model_kwargs,}@propertydef_identifying_params(self)->Dict[str,Any]:return{"model":self.model,"backend":self.backend,"regex":self.regex,"type_constraints":self.type_constraints,"json_schema":self.json_schema,"grammar":self.grammar,**self._default_params,}@propertydef_generator(self)->Any:fromoutlinesimportgenerateifself.custom_generator:returnself.custom_generatorifself.regex:returngenerate.regex(self.client,regex_str=self.regex)ifself.type_constraints:returngenerate.format(self.client,python_type=self.type_constraints)ifself.json_schema:returngenerate.json(self.client,schema_object=self.json_schema)ifself.grammar:returngenerate.cfg(self.client,cfg_str=self.grammar)returngenerate.text(self.client)def_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:params={**self._default_params,**kwargs}ifstop:params["stop_at"]=stopresponse=""ifself.streaming:forchunkinself._stream(prompt=prompt,stop=params["stop_at"],run_manager=run_manager,**params,):response+=chunk.textelse:response=self._generator(prompt,**params)returnresponsedef_stream(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->Iterator[GenerationChunk]:params={**self._default_params,**kwargs}ifstop:params["stop_at"]=stopfortokeninself._generator.stream(prompt,**params):ifrun_manager:run_manager.on_llm_new_token(token)yieldGenerationChunk(text=token)@propertydeftokenizer(self)->Any:"""Access the tokenizer for the underlying model. .encode() to tokenize text. .decode() to convert tokens back to text. """ifhasattr(self.client,"tokenizer"):returnself.client.tokenizerraiseValueError("Tokenizer not found")