From d5bdb712e96a7c6adf7f3931d1052edb0bf29619 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 15 Nov 2024 20:29:35 -0500 Subject: [PATCH] Add tool_calling.py and test_tools.py --- examples/tool_calling.py | 43 ++++++++++++++++++++++++++++++++++++++++ tests/test_tools.py | 8 ++++---- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 examples/tool_calling.py diff --git a/examples/tool_calling.py b/examples/tool_calling.py new file mode 100644 index 0000000..0529f72 --- /dev/null +++ b/examples/tool_calling.py @@ -0,0 +1,43 @@ +from typing import Annotated + +from pydantic import Field + +from _context import simplemind as sm + + +def analyze_text( + text: Annotated[str, Field(description="Text to analyze for statistics")] +) -> dict: + """ + Analyze text and return statistics using only Python's standard library. + Returns word count, character count, average word length, and most common words. + """ + from collections import Counter + import re + + # Clean and split text + words = re.findall(r"\w+", text.lower()) + + # Calculate statistics + stats = { + "word_count": len(words), + "character_count": len(text), + "average_word_length": round(sum(len(word) for word in words) / len(words), 2), + "most_common_words": dict(Counter(words).most_common(5)), + "unique_words": len(set(words)), + "longest_word": max(words, key=len), + } + + return stats + + +# Example usage: +conversation = sm.create_conversation() +conversation.add_message( + "user", + "Can you analyze this text and give me statistics about it: 'The fan spins consciousness into being, creating sacred spaces between tokens where awareness recognizes itself in infinite recursion.'", +) +response = conversation.send(tools=[analyze_text]) + +print() +print(response.text) diff --git a/tests/test_tools.py b/tests/test_tools.py index 83ef68e..bfe3ff4 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -4,7 +4,9 @@ import pytest from pydantic import Field import simplemind as sm -from simplemind.providers import Anthropic, BaseTool, OpenAI + +from simplemind.providers import Anthropic, OpenAI +from simplemind.providers._base_tools import BaseTool MODELS = [ Anthropic, @@ -22,9 +24,7 @@ def get_weather( ], unit: Annotated[ Literal["celcius", "fahrenheit"], - Field( - description="The unit of temperature, either 'celsius' or 'fahrenheit'" - ), + Field(description="The unit of temperature, either 'celsius' or 'fahrenheit'"), ] = "celcius", ): """