mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 08:38:30 +00:00
Merge branch 'main' into lg-guardrail-llm
This commit is contained in:
@@ -190,48 +190,6 @@ def my_tool(question: str) -> str:
|
|||||||
return "Result from your custom tool"
|
return "Result from your custom tool"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Structured Tools
|
|
||||||
|
|
||||||
The `StructuredTool` class wraps functions as tools, providing flexibility and validation while reducing boilerplate. It supports custom schemas and dynamic logic for seamless integration of complex functionalities.
|
|
||||||
|
|
||||||
#### Example:
|
|
||||||
Using `StructuredTool.from_function`, you can wrap a function that interacts with an external API or system, providing a structured interface. This enables robust validation and consistent execution, making it easier to integrate complex functionalities into your applications as demonstrated in the following example:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from crewai.tools.structured_tool import CrewStructuredTool
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
# Define the schema for the tool's input using Pydantic
|
|
||||||
class APICallInput(BaseModel):
|
|
||||||
endpoint: str
|
|
||||||
parameters: dict
|
|
||||||
|
|
||||||
# Wrapper function to execute the API call
|
|
||||||
def tool_wrapper(*args, **kwargs):
|
|
||||||
# Here, you would typically call the API using the parameters
|
|
||||||
# For demonstration, we'll return a placeholder string
|
|
||||||
return f"Call the API at {kwargs['endpoint']} with parameters {kwargs['parameters']}"
|
|
||||||
|
|
||||||
# Create and return the structured tool
|
|
||||||
def create_structured_tool():
|
|
||||||
return CrewStructuredTool.from_function(
|
|
||||||
name='Wrapper API',
|
|
||||||
description="A tool to wrap API calls with structured input.",
|
|
||||||
args_schema=APICallInput,
|
|
||||||
func=tool_wrapper,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Example usage
|
|
||||||
structured_tool = create_structured_tool()
|
|
||||||
|
|
||||||
# Execute the tool with structured input
|
|
||||||
result = structured_tool._run(**{
|
|
||||||
"endpoint": "https://example.com/api",
|
|
||||||
"parameters": {"key1": "value1", "key2": "value2"}
|
|
||||||
})
|
|
||||||
print(result) # Output: Call the API at https://example.com/api with parameters {'key1': 'value1', 'key2': 'value2'}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Custom Caching Mechanism
|
### Custom Caching Mechanism
|
||||||
|
|
||||||
<Tip>
|
<Tip>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ dependencies = [
|
|||||||
# Core Dependencies
|
# Core Dependencies
|
||||||
"pydantic>=2.4.2",
|
"pydantic>=2.4.2",
|
||||||
"openai>=1.13.3",
|
"openai>=1.13.3",
|
||||||
"litellm==1.67.2",
|
"litellm==1.67.1",
|
||||||
"instructor>=1.3.3",
|
"instructor>=1.3.3",
|
||||||
# Text Processing
|
# Text Processing
|
||||||
"pdfplumber>=0.11.4",
|
"pdfplumber>=0.11.4",
|
||||||
|
|||||||
Reference in New Issue
Block a user