Notebooks
d
deepset
Newsletter Agent

Newsletter Agent

agentic-aiagenticagentsgenaiAIhaystack-cookbookgenai-usecaseshaystack-ainotebooksPythonragai-tools

πŸ—žοΈ Newsletter Sending Agent with Haystack Tools

πŸ§‘β€πŸ³ Demo by Stefano Fiorucci (X, LinkedIn) and Tuana Celik(X, LinkedIn)

In this recipe, we will build a newsletter sending agent with 3 tools:

  • A tool that fetches the top stories from Hacker News
  • A tool that creates newsletters for a particular audience
  • A tool that can send emails (with Gmail)

This notebook is updated after Haystack 2.9.0. Experimental features in the old version of this notebook are merged into Haystack core package.

πŸ“Ί Watch Along

Install dependencies

Install the latest versions of haystack-ai and trafilatura

[ ]

Importing Features

In this demo, we are using Haystack's latest features: Tool, ToolInvoker with extended ChatMessage and OpenAIChatGenerator.

[ ]

Hacker News Fetcher Tool

In a previous article and recipe, we had shown how you can create a custom component for Haystack called the HackerNewsFetcher.

Here, we are doing something very similar, but instead we are creating a function and using that as a Tool instead.

πŸ“š Hacker News Summaries with Custom Components

This tool expects top_k as input, and returns that many of the current top stories on Hacker News πŸš€

[3]
[4]

Newsletter generation Pipeline and Tool

For the Newsletter gnereation tool, we will be creating a Haystack pipeline, and making our pipeline itself a tool.

Our tool will expect the following inputs:

  • articles: Content to base the newsletter off of
  • target_people: The audience we want to target, for example "engineers" may be our target audience
  • n_words: The number of words we want to limit our newsletter to
[5]
Enter your OpenAI API key: Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·
[ ]
[ ]

Send Email Tool

Here, we are creating a Gmail tool. You login with your gmail account, allowing the final Agent to send emails from your email, to others.

⚠️ Note: To be able to use the gmail too, you have to create an app password for your Gmail account, which will be the sender. You can delete this after.

To configure our email Tool, you have to provide the following information about the sender email account πŸ‘‡

[ ]

Next, we create a Tool that expects the following input:

  • receiver: The email address that we want to send an email to
  • body: The body of the email
  • subject: The subject line for the email.
[ ]
[ ]

Newsletter Sending Chat Agent

Now, we build a Newsletter creating chat agent which we can use to ask for newsletters, as well as sending them to given email addresses.

[ ]


waiting for input (type 'exit' or 'quit' to stop)
πŸ§‘: What is the top HN article now?
βŒ› iterating...

 TOOL CALL:
	call_aOluHPdSMAGosryayVwNxOvL
	hacker_news_fetcher
	top_k: 1
βŒ› iterating...

 TOOL CALL:
	call_G6Z10LdGwJgdqxspgIWwvsnl
	hacker_news_fetcher
	top_k: 1
βŒ› iterating...
πŸ€–: It appears that I'm facing an issue retrieving data from Hacker News, as the response does not contain the actual article information. Unfortunately, I'm unable to provide the top article at this moment. You might want to check the Hacker News website directly for the latest articles.


waiting for input (type 'exit' or 'quit' to stop)
πŸ§‘: What's the top 2 HN articles?
βŒ› iterating...

 TOOL CALL:
	call_SWI39GuYw579wRDhQVs02WUu
	hacker_news_fetcher
	top_k: 2
βŒ› iterating...
πŸ€–: It seems I'm encountering difficulties retrieving the top articles from Hacker News properly. However, based on the partial information I received, one of the articles is:

1. **Adobe's new image rotation tool** - "Project Turnable" lets users fully rotate 2D vectors. This tool was showcased at Adobe's annual MAX conference as part of their "Sneaks" segment, where engineers present innovative ideas that may or may not be fully developed.

Unfortunately, the first article did not provide relevant content. For the most accurate and complete information on the top Hacker News articles, I recommend checking the Hacker News website directly.


waiting for input (type 'exit' or 'quit' to stop)
πŸ§‘: Create a newsletter targeted at engineers based on this article. No more than 100 words.
βŒ› iterating...

 TOOL CALL:
	call_duVE2eBKkCe3wpJOuq6NEJte
	newsletter_generator
	articles: ["Adobe's new image rotation tool is one of the mo...
	target_people: engineers
	n_words: 100
βŒ› iterating...
πŸ€–: **Engineering Whirlwind**  
*Issue #42: AI Innovations Turned Up to Eleven*

Hello, Innovators!

Dive into Adobe's latest gem, *Project Turntable*! This fascinating tool offers engineers a chance to fully rotate 2D vectors like never before. Unveiled at their MAX conference, it sits at the intersection of imagination and engineering mastery. As Adobe's engineers sneak out innovative ideas, warm up those creative enginesβ€”who knows what else might come spinning your way?

Stay sharp and keep spinning those ideas!  
β€” The Engineering Brigade 🌟


waiting for input (type 'exit' or 'quit' to stop)
πŸ§‘: Email this to tuana.celik@deepset.ai You can decide on the subjectline
βŒ› iterating...

 TOOL CALL:
	call_e7thnZ8Bq1kBjU4dyrGLP0jK
	email
	receiver: tuana.celik@deepset.ai
	body: **Engineering Whirlwind**  
*Issue #42: AI Innovat...
	subject: Latest Innovations: Adobe's Project Turntable
βŒ› iterating...
πŸ€–: The newsletter has been successfully emailed to tuana.celik@deepset.ai with the subject "Latest Innovations: Adobe's Project Turntable." If you need any further assistance, feel free to ask!


waiting for input (type 'exit' or 'quit' to stop)
πŸ§‘: exit

Extras: Converting Tools

Convert functions into Tools

[ ]

Writing the JSON schema is not fun... πŸ€”

[ ]

We can do this instead πŸ‘‡

[ ]
Tool(name='newsletter_pipeline_func',
     description='Generate a newsletter based on some articles',
     parameters={'properties': {'articles': {'items': {'type': 'string'}, 'type': 'array', 'description': 'The articles to include in the newsletter'},
                                'target_people': {'default': 'programmers', 'type': 'string', 'description': 'The target audience for the newsletter'},
                                'n_words': {'default': 100, 'type': 'integer', 'description': 'The number of words to summarize the newsletter to'}},
                 'required': ['articles'],
                 'type': 'object'},
     function=<function newsletter_pipeline_func at 0x7f6fd96511b0>)

Convert Pre-Existing Tools into Haystack Tools

Haystack is quite flexible. This means if you have tools already defined elsewhere, you are able to convert them to Haystack tools. For example, LangChain has several interesting tools that we can seamlessly convert into Haystack tools.

[ ]
[ ]
[ ]
[CopyFileTool(root_dir='/'),
, DeleteFileTool(root_dir='/'),
, FileSearchTool(root_dir='/'),
, MoveFileTool(root_dir='/'),
, ReadFileTool(root_dir='/'),
, WriteFileTool(root_dir='/'),
, ListDirectoryTool(root_dir='/')]
[ ]
[ ]
[ ]
The files in the `/content/sample_data` directory are:

1. anscombe.json
2. README.md
3. mnist_train_small.csv
4. california_housing_train.csv
5. california_housing_test.csv
6. mnist_test.csv