-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #722 from Mirascope/add-partial-tool-streaming-docs
docs: streaming partial tools with examples
- Loading branch information
Showing
84 changed files
with
2,963 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
examples/learn/tools/partial_tool_streams/anthropic/base_tool/base_message_param.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from mirascope.core import BaseMessageParam, BaseTool, anthropic | ||
from pydantic import Field | ||
|
||
|
||
class GetBookAuthor(BaseTool): | ||
"""Returns the author of the book with the given title.""" | ||
|
||
title: str = Field(..., description="The title of the book.") | ||
|
||
def call(self) -> str: | ||
if self.title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif self.title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[GetBookAuthor], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> list[BaseMessageParam]: | ||
return [BaseMessageParam(role="user", content=f"Who wrote {books}?")] | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
36 changes: 36 additions & 0 deletions
36
examples/learn/tools/partial_tool_streams/anthropic/base_tool/messages.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from mirascope.core import BaseTool, Messages, anthropic | ||
from pydantic import Field | ||
|
||
|
||
class GetBookAuthor(BaseTool): | ||
"""Returns the author of the book with the given title.""" | ||
|
||
title: str = Field(..., description="The title of the book.") | ||
|
||
def call(self) -> str: | ||
if self.title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif self.title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[GetBookAuthor], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> Messages.Type: | ||
return Messages.User(f"Who wrote {books}?") | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
36 changes: 36 additions & 0 deletions
36
examples/learn/tools/partial_tool_streams/anthropic/base_tool/shorthand.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from mirascope.core import BaseTool, anthropic | ||
from pydantic import Field | ||
|
||
|
||
class GetBookAuthor(BaseTool): | ||
"""Returns the author of the book with the given title.""" | ||
|
||
title: str = Field(..., description="The title of the book.") | ||
|
||
def call(self) -> str: | ||
if self.title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif self.title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[GetBookAuthor], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> str: | ||
return f"Who wrote {books}?" | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
36 changes: 36 additions & 0 deletions
36
examples/learn/tools/partial_tool_streams/anthropic/base_tool/string_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from mirascope.core import BaseTool, anthropic, prompt_template | ||
from pydantic import Field | ||
|
||
|
||
class GetBookAuthor(BaseTool): | ||
"""Returns the author of the book with the given title.""" | ||
|
||
title: str = Field(..., description="The title of the book.") | ||
|
||
def call(self) -> str: | ||
if self.title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif self.title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[GetBookAuthor], | ||
stream={"partial_tools": True}, | ||
) | ||
@prompt_template("Who wrote {book}?") | ||
def identify_authors(books: list[str]): ... | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
35 changes: 35 additions & 0 deletions
35
examples/learn/tools/partial_tool_streams/anthropic/function/base_message_param.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from mirascope.core import BaseMessageParam, anthropic | ||
|
||
|
||
def get_book_author(title: str) -> str: | ||
"""Returns the author of the book with the given title | ||
Args: | ||
title: The title of the book. | ||
""" | ||
if title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[get_book_author], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> list[BaseMessageParam]: | ||
return [BaseMessageParam(role="user", content=f"Who wrote {books}?")] | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
35 changes: 35 additions & 0 deletions
35
examples/learn/tools/partial_tool_streams/anthropic/function/messages.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from mirascope.core import Messages, anthropic | ||
|
||
|
||
def get_book_author(title: str) -> str: | ||
"""Returns the author of the book with the given title | ||
Args: | ||
title: The title of the book. | ||
""" | ||
if title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[get_book_author], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> Messages.Type: | ||
return Messages.User(f"Who wrote {books}?") | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
35 changes: 35 additions & 0 deletions
35
examples/learn/tools/partial_tool_streams/anthropic/function/shorthand.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from mirascope.core import anthropic | ||
|
||
|
||
def get_book_author(title: str) -> str: | ||
"""Returns the author of the book with the given title | ||
Args: | ||
title: The title of the book. | ||
""" | ||
if title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[get_book_author], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> str: | ||
return f"Who wrote {books}?" | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
35 changes: 35 additions & 0 deletions
35
examples/learn/tools/partial_tool_streams/anthropic/function/string_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from mirascope.core import anthropic, prompt_template | ||
|
||
|
||
def get_book_author(title: str) -> str: | ||
"""Returns the author of the book with the given title | ||
Args: | ||
title: The title of the book. | ||
""" | ||
if title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@anthropic.call( | ||
"claude-3-5-sonnet-20240620", | ||
tools=[get_book_author], | ||
stream={"partial_tools": True}, | ||
) | ||
@prompt_template("Who wrote {book}?") | ||
def identify_authors(books: list[str]): ... | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
36 changes: 36 additions & 0 deletions
36
examples/learn/tools/partial_tool_streams/azure/base_tool/base_message_param.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from mirascope.core import BaseMessageParam, BaseTool, azure | ||
from pydantic import Field | ||
|
||
|
||
class GetBookAuthor(BaseTool): | ||
"""Returns the author of the book with the given title.""" | ||
|
||
title: str = Field(..., description="The title of the book.") | ||
|
||
def call(self) -> str: | ||
if self.title == "The Name of the Wind": | ||
return "Patrick Rothfuss" | ||
elif self.title == "Mistborn: The Final Empire": | ||
return "Brandon Sanderson" | ||
else: | ||
return "Unknown" | ||
|
||
|
||
@azure.call( | ||
"gpt-4o-mini", | ||
tools=[GetBookAuthor], | ||
stream={"partial_tools": True}, | ||
) | ||
def identify_authors(books: list[str]) -> list[BaseMessageParam]: | ||
return [BaseMessageParam(role="user", content=f"Who wrote {books}?")] | ||
|
||
|
||
stream = identify_authors(["The Name of the Wind", "Mistborn: The Final Empire"]) | ||
for chunk, tool in stream: | ||
if tool: | ||
if tool.delta is not None: # partial tool | ||
print(tool.delta) | ||
else: | ||
print(tool.call()) | ||
else: | ||
print(chunk.content, end="", flush=True) |
Oops, something went wrong.