Skip to content
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

Add example of using a LangChain tool and fetching data, summarizing, and doing NER and sentiment analysis #225

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions docs/patterns/tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ task = cf.Task(
instructions="Report the history of rolls",
tools=[roll_die],
)

task.run()
```

Expand Down Expand Up @@ -121,6 +122,8 @@ ControlFlow supports the following types of tools:
- Asynchronous Python functions (they are automatically run in an asynchronous context)
- LangChain tools

### Regular Python Functions

In most cases, you can use regular Python functions as tools without modification. However, if you need to customize how a function is converted to a tool, you can use the `@tool` decorator to override the inferred name and description. For example, this function has no docstring, so we use the decorator to provide a custom description:

```python
Expand All @@ -131,6 +134,32 @@ def get_weather(location: str) -> dict:
#Implementation details...
```

### LangChain Tools

LangChain has many [pre-built tools](https://python.langchain.com/v0.2/docs/integrations/tools/) that you can leverage. For example, here's how to get recent data from the web with DuckDuckGo.

Install dependencies with your preferred package manager:

<CodeGroup>
```bash pip
# ControlFlow requires Python 3.9 or greater
pip install -U langchain-community, duckduckgo-search
```

```bash uv
# ControlFlow requires Python 3.9 or greater
uv pip install -U langchain-community, duckduckgo-search
```
</CodeGroup>

Then import the tool for use.

```python
import controlflow as cf
from langchain_community.tools import DuckDuckGoSearchRun
```


## Using Tools

Tools can be provided either to tasks or agents. When a tool is provided to a task, any agent working on that task will have access to the tool. When a tool is provided to an agent, the agent can use the tool in any task it is assigned to.
Expand All @@ -139,7 +168,6 @@ Tools can be provided either to tasks or agents. When a tool is provided to a ta

You should provide tools to tasks when you know that a task will require specific capabilities to be completed successfully. For example, if a task requires access to a database or an external API, you can provide a tool that handles the interaction with that system:


```python
import controlflow as cf

Expand Down Expand Up @@ -183,6 +211,16 @@ agent = cf.Agent(

Using Pydantic models for tool return types helps ensure that the data returned by the tool is properly structured and validated.

To use the LangChain tool you imported earlier, provide it to a task like this:

```python
agent = cf.Agent(
name="Timely agent",
description="An AI agent that knows current events",
tools=[DuckDuckGoSearchRun()],
)
```

## Debugging Tools

### Verbose Logging
Expand Down Expand Up @@ -225,4 +263,4 @@ Tools are particularly useful in scenarios where:

By providing appropriate tools, you can significantly enhance the problem-solving capabilities of your AI agents and create more powerful and flexible workflows.

Remember that while tools are powerful, they should be used judiciously. Provide only the tools that are necessary for the task at hand to avoid overwhelming the agent with too many options.
While tools are powerful, they should be used judiciously. Provide only the tools that are necessary for the task at hand to avoid overwhelming the agent with too many options.
39 changes: 39 additions & 0 deletions examples/business_headline_sentiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# uv pip install langchain-community, duckduckgo-search

import controlflow as cf
from langchain_community.tools import DuckDuckGoSearchRun


summarizer = cf.Agent(
name="Headline Summarizer",
description="An AI agent that fetches and summarizes current events",
tools=[DuckDuckGoSearchRun()],
)

extractor = cf.Agent(
name="Entity Extractor",
description="An AI agent that does named entity recognition",
)


@cf.flow
def get_headlines():

summarizer_task = cf.Task(
"Retrieve and summarize today's two top business headlines",
agent=summarizer,
result_type=list[str],
)

extractor_task = cf.Task(
"Extract any fortune 500 companies mentioned in the headlines and whether the sentiment is positive, neutral, or negative",
agent=extractor,
depends_on=[summarizer_task],
)

return summarizer_task, extractor_task


if __name__ == "__main__":
headlines, entity_sentiment = get_headlines()
print(headlines, entity_sentiment)
Loading