Skip to content

Commit 29d3fd4

Browse files
authored
Merge pull request #60 from vacmar01/main
add `structured` function for structured output
2 parents b759709 + 11ff31d commit 29d3fd4

File tree

3 files changed

+202
-36
lines changed

3 files changed

+202
-36
lines changed

lisette/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
'lisette.core.patch_litellm': ('core.html#patch_litellm', 'lisette/core.py'),
5252
'lisette.core.random_tool_id': ('core.html#random_tool_id', 'lisette/core.py'),
5353
'lisette.core.remove_cache_ckpts': ('core.html#remove_cache_ckpts', 'lisette/core.py'),
54-
'lisette.core.stream_with_complete': ('core.html#stream_with_complete', 'lisette/core.py')},
54+
'lisette.core.stream_with_complete': ('core.html#stream_with_complete', 'lisette/core.py'),
55+
'lisette.core.structured': ('core.html#structured', 'lisette/core.py')},
5556
'lisette.usage': { 'lisette.usage.LisetteUsageLogger': ('usage.html#lisetteusagelogger', 'lisette/usage.py'),
5657
'lisette.usage.LisetteUsageLogger.__init__': ('usage.html#lisetteusagelogger.__init__', 'lisette/usage.py'),
5758
'lisette.usage.LisetteUsageLogger._log_usage': ( 'usage.html#lisetteusagelogger._log_usage',

lisette/core.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# %% auto 0
66
__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',
88
'cite_footnotes', 'Chat', 'random_tool_id', 'mk_tc', 'mk_tc_req', 'mk_tc_result', 'mk_tc_results',
99
'astream_with_complete', 'AsyncChat', 'mk_tr_details', 'AsyncStreamFormatter', 'adisplay_stream']
1010

@@ -213,6 +213,28 @@ def _lite_call_func(tc,ns,raise_on_err=True):
213213
else: res = str(res)
214214
return {"tool_call_id": tc.id, "role": "tool", "name": tc.function.name, "content": res}
215215

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+
216238
# %% ../nbs/00_core.ipynb
217239
def _has_search(m):
218240
i = get_model_info(m)

0 commit comments

Comments
 (0)