-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT][Add Tool logic to Agent
class
#79
Comments
|
ToolsTo pass the outputs of tools as input to a Language Model (LM), you need to structure your workflow in a way that allows the tools to process data or perform specific operations, and then feed their outputs into the LM. Here's a conceptual walkthrough and an example in Python code to illustrate this process: Conceptual Workflow:
Python Code Example:Let's consider a scenario where you have two tools: one to generate a query and another to process text. You'll then use these tools to interact with an LM. First, define the tools: # Assuming you have the `tool` decorator defined as per your context
@tool
def generate_query(topic: str) -> str:
"""Generate a query based on a topic."""
return f"Tell me about {topic}"
@tool
def process_text(text: str) -> str:
"""Process the given text."""
# Some processing logic
processed_text = text.upper() # Example processing
return processed_text Next, define a function to interact with the LM: def query_lm(llm, prompt: str) -> str:
"""Query the language model with a given prompt."""
response = llm(prompt) # Interacting with the LM
return response Finally, create a workflow that uses these tools and the LM: def main_workflow(llm, topic: str):
# Generate query using the tool
query = generate_query(topic)
# Get response from LM
lm_response = query_lm(llm, query)
# Process the LM's response using another tool
processed_response = process_text(lm_response)
return processed_response
# Example usage
llm = YourLanguageModel() # Replace with your actual LM instance
topic = "Artificial Intelligence"
result = main_workflow(llm, topic)
print(result) Explanation:
Key Points:
|
Flow
classAgent
class
Tool usage script
|
Is this implemented? Can it be closed? |
Stale issue message |
Integrating tools with Flow
Example
Reference
Learn how the worker uses tool: https://github.com/kyegomez/swarms/blob/master/swarms/agents/agent.py
tool
DocumentationHow It Works
Decorator Functionality:
The
tool
decorator can be used in several ways, based on the arguments passed to it. It supports transforming simple functions, asynchronous functions, or objects implementing aRunnable
interface into a tool.Arguments Handling:
*args
: This is a variable argument list allowing different types of inputs (string,Callable
, orRunnable
).return_direct
: If set toTrue
, the tool returns directly without continuing the loop in which it's running.args_schema
: An optional Pydantic model (BaseModel
) for validating input arguments to the tool.infer_schema
: IfTrue
, the tool attempts to infer the argument schema from the function signature.Tool Creation:
Runnable
object, it wraps its invocation methods (ainvoke
for async,invoke
for sync) into a tool.StructuredTool.from_function
ifinfer_schema
isTrue
or creates a basicTool
otherwise.Schema Inference:
infer_schema
isTrue
, the decorator infers the input schema for the tool based on the function's signature.run()
method.Error Handling:
infer_schema
isFalse
, the function must have a docstring to provide a description.Example Usages
Simple Function as a Tool:
Named Tool with Direct Return:
Using Runnable:
Expected Outputs
Use Cases
This kind of tool creation is particularly useful in systems where you need modular, reusable components that can be plugged into different parts of an application, especially in scenarios involving asynchronous operations, agent-based simulations, or complex workflows requiring structured input/output handling.
Upvote & Fund
The text was updated successfully, but these errors were encountered: