Add tool_calling.py and test_tools.py

This commit is contained in:
2024-11-15 20:29:35 -05:00
parent a97f9be2c8
commit d5bdb712e9
2 changed files with 47 additions and 4 deletions
+43
View File
@@ -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)
+4 -4
View File
@@ -4,7 +4,9 @@ import pytest
from pydantic import Field from pydantic import Field
import simplemind as sm 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 = [ MODELS = [
Anthropic, Anthropic,
@@ -22,9 +24,7 @@ def get_weather(
], ],
unit: Annotated[ unit: Annotated[
Literal["celcius", "fahrenheit"], Literal["celcius", "fahrenheit"],
Field( Field(description="The unit of temperature, either 'celsius' or 'fahrenheit'"),
description="The unit of temperature, either 'celsius' or 'fahrenheit'"
),
] = "celcius", ] = "celcius",
): ):
""" """