mirror of
https://github.com/kennethreitz/simplemind.git
synced 2026-06-05 06:46:18 +00:00
Add tool_calling.py and test_tools.py
This commit is contained in:
@@ -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
@@ -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",
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user