From 8d04820089e01aded53b5ff49ad1be79da00e9d1 Mon Sep 17 00:00:00 2001 From: Jason Liu Date: Sat, 23 Dec 2023 12:27:31 -0500 Subject: [PATCH] nit: update union --- docs/concepts/union.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/docs/concepts/union.md b/docs/concepts/union.md index d8fb074..40e1824 100644 --- a/docs/concepts/union.md +++ b/docs/concepts/union.md @@ -1,7 +1,29 @@ -!!! warning "This page is a work in progress" +Pydantic models also support `Union` types, which are used to represent a value that can be one of several types. -You can use `Union` types to write *agents* that can dynamically choose actions - by choosing an output class. -For example, in a search and lookup function, the LLM can determine whether to execute another search, lookup -or other action, or finish the task with an answer. +While many libraries support multiple function calls, and tool calls support multiple returns, the goal is to provide only one way to do things. + +## Unions for Multiple Types + +You can use `Union` types to write _agents_ that can dynamically choose actions - by choosing an output class. For example, in a search and lookup function, the LLM can determine whether to execute another search, lookup or other action. + +```python +class Search(BaseModel): + query: str + + def execute(self): + return ... + +class Lookup(BaseModel): + key: str + + def execute(self): + return ... + +class Action(BaseModel): + action: Union[Search, Lookup] + + def execute(self): + return self.action.execute() +``` See 'examples/union/run.py' for a working example.