| 
1 | 1 | import json  | 
2 |  | -from typing import Optional, Union  | 
 | 2 | +from typing import Dict, List, Optional, Union  | 
3 | 3 | 
 
  | 
4 | 4 | from guardrails.formatters.base_formatter import BaseFormatter  | 
5 | 5 | from guardrails.llm_providers import (  | 
@@ -99,32 +99,48 @@ def wrap_callable(self, llm_callable) -> ArbitraryCallable:  | 
99 | 99 | 
 
  | 
100 | 100 |         if isinstance(llm_callable, HuggingFacePipelineCallable):  | 
101 | 101 |             model = llm_callable.init_kwargs["pipeline"]  | 
102 |  | -            return ArbitraryCallable(  | 
103 |  | -                lambda p: json.dumps(  | 
 | 102 | + | 
 | 103 | +            def fn(  | 
 | 104 | +                prompt: str,  | 
 | 105 | +                *args,  | 
 | 106 | +                instructions: Optional[str] = None,  | 
 | 107 | +                msg_history: Optional[List[Dict[str, str]]] = None,  | 
 | 108 | +                **kwargs,  | 
 | 109 | +            ) -> str:  | 
 | 110 | +                return json.dumps(  | 
104 | 111 |                     Jsonformer(  | 
105 | 112 |                         model=model.model,  | 
106 | 113 |                         tokenizer=model.tokenizer,  | 
107 | 114 |                         json_schema=self.output_schema,  | 
108 |  | -                        prompt=p,  | 
 | 115 | +                        prompt=prompt,  | 
109 | 116 |                     )()  | 
110 | 117 |                 )  | 
111 |  | -            )  | 
 | 118 | + | 
 | 119 | +            return ArbitraryCallable(fn)  | 
112 | 120 |         elif isinstance(llm_callable, HuggingFaceModelCallable):  | 
113 | 121 |             # This will not work because 'model_generate' is the .gen method.  | 
114 | 122 |             # model = self.api.init_kwargs["model_generate"]  | 
115 | 123 |             # Use the __self__ to grab the base mode for passing into JF.  | 
116 | 124 |             model = llm_callable.init_kwargs["model_generate"].__self__  | 
117 | 125 |             tokenizer = llm_callable.init_kwargs["tokenizer"]  | 
118 |  | -            return ArbitraryCallable(  | 
119 |  | -                lambda p: json.dumps(  | 
 | 126 | + | 
 | 127 | +            def fn(  | 
 | 128 | +                prompt: str,  | 
 | 129 | +                *args,  | 
 | 130 | +                instructions: Optional[str] = None,  | 
 | 131 | +                msg_history: Optional[List[Dict[str, str]]] = None,  | 
 | 132 | +                **kwargs,  | 
 | 133 | +            ) -> str:  | 
 | 134 | +                return json.dumps(  | 
120 | 135 |                     Jsonformer(  | 
121 | 136 |                         model=model,  | 
122 | 137 |                         tokenizer=tokenizer,  | 
123 | 138 |                         json_schema=self.output_schema,  | 
124 |  | -                        prompt=p,  | 
 | 139 | +                        prompt=prompt,  | 
125 | 140 |                     )()  | 
126 | 141 |                 )  | 
127 |  | -            )  | 
 | 142 | + | 
 | 143 | +            return ArbitraryCallable(fn)  | 
128 | 144 |         else:  | 
129 | 145 |             raise ValueError(  | 
130 | 146 |                 "JsonFormatter can only be used with HuggingFace*Callable."  | 
 | 
0 commit comments