|
4 | 4 |
|
5 | 5 | # %% auto 0 |
6 | 6 | __all__ = ['sonn45', 'opus45', 'detls_tag', 're_tools', 'effort', 'patch_litellm', 'remove_cache_ckpts', 'contents', 'mk_msg', |
7 | | - 'fmt2hist', 'mk_msgs', 'stream_with_complete', 'lite_mk_func', 'ToolResponse', 'cite_footnote', |
| 7 | + 'fmt2hist', 'mk_msgs', 'stream_with_complete', 'lite_mk_func', 'ToolResponse', 'structured', 'cite_footnote', |
8 | 8 | 'cite_footnotes', 'Chat', 'random_tool_id', 'mk_tc', 'mk_tc_req', 'mk_tc_result', 'mk_tc_results', |
9 | 9 | 'astream_with_complete', 'AsyncChat', 'mk_tr_details', 'AsyncStreamFormatter', 'adisplay_stream'] |
10 | 10 |
|
@@ -213,6 +213,28 @@ def _lite_call_func(tc,ns,raise_on_err=True): |
213 | 213 | else: res = str(res) |
214 | 214 | return {"tool_call_id": tc.id, "role": "tool", "name": tc.function.name, "content": res} |
215 | 215 |
|
| 216 | +# %% ../nbs/00_core.ipynb |
| 217 | +import json |
| 218 | + |
| 219 | +# %% ../nbs/00_core.ipynb |
| 220 | +from fastcore.meta import delegates |
| 221 | + |
| 222 | +# %% ../nbs/00_core.ipynb |
| 223 | +@delegates(completion) |
| 224 | +def structured( |
| 225 | + m, # The LiteLLM model string |
| 226 | + msgs:list, # A list of messages |
| 227 | + tool, # The tool to be used for creating the structured output. Can also be a class, dataclass or Pydantic model |
| 228 | + **kwargs): |
| 229 | + "Return the value of the tool call (generally used for structured outputs)" |
| 230 | + t = lite_mk_func(tool) |
| 231 | + |
| 232 | + r = completion(m, msgs, tools=[t], tool_choice=t, **kwargs) |
| 233 | + |
| 234 | + args = json.loads(r.choices[0].message.tool_calls[0].function.arguments) |
| 235 | + |
| 236 | + return tool(**args) |
| 237 | + |
216 | 238 | # %% ../nbs/00_core.ipynb |
217 | 239 | def _has_search(m): |
218 | 240 | i = get_model_info(m) |
|
0 commit comments