Back to Llama Index

Prompt for the API key securely

llama-index-integrations/tools/llama-index-tools-dappier/examples/dappier_ai_recommendations.ipynb

0.14.214.7 KB
Original Source

Building a Dappier AI Recommendations Agent

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/llama-index-integrations/tools/llama-index-tools-dappier/examples/dappier_ai_recommendations.ipynb" target="_parent"></a>

This tutorial walks through using the LLM tools provided by Dappier to allow LLMs to use Dappier's pre-trained, LLM ready RAG models and natural language APIs to ensure factual, up-to-date, responses from premium content providers across key verticals like News, Finance, Sports, Weather, and more.

To get started, you will need an OpenAI API key and a Dappier API key

We will import the relevant agents and tools and pass them our keys here:

Installation

First, install the llama-index and llama-index-tools-dappier packages

python
%pip install llama-index llama-index-tools-dappier

Setup API keys

You'll need to set up your API keys for OpenAI and Dappier.

You can go to here to get API Key from Open AI.

python
import os
from getpass import getpass

# Prompt for the API key securely
openai_api_key = getpass("Enter your API key: ")
os.environ["OPENAI_API_KEY"] = openai_api_key

You can go to here to get API Key from Dappier with free credits.

python
# Prompt for the Dappier API key securely
dappier_api_key = getpass("Enter your API key: ")
os.environ["DAPPIER_API_KEY"] = dappier_api_key

Setup Dappier Tool

Initialize the Dappier Real-Time Search Tool, convert it to a tool list.

python
from llama_index.tools.dappier import (
    DappierAIRecommendationsToolSpec,
)

dappier_tool = DappierAIRecommendationsToolSpec()

dappier_tool_list = dappier_tool.to_tool_list()
for tool in dappier_tool_list:
    print(tool.metadata.name)

Usage

We've imported our OpenAI agent, set up the api key, and initialized our tool. Let's test out the tool before setting up our Agent.

Sports News

Real-time news, updates, and personalized content from top sports sources like Sportsnaut, Forever Blueshirts, Minnesota Sports Fan, LAFB Network, Bounding Into Sports, and Ringside Intel.

python
print(
    dappier_tool.get_sports_news_recommendations(
        query="latest sports news", similarity_top_k=3
    )
)

Lifestyle News

Real-time updates, analysis, and personalized content from top sources like The Mix, Snipdaily, Nerdable, and Familyproof.

python
print(
    dappier_tool.get_lifestyle_news_recommendations(
        query="latest lifestyle updates", similarity_top_k=3
    )
)

iHeartDogs Articles

A dog care expert with access to articles on health, behavior, lifestyle, grooming, ownership, and more.

python
print(
    dappier_tool.get_iheartdogs_recommendations(
        query="dog care tips", similarity_top_k=3
    )
)

iHeartCats Articles

A cat care expert with access to articles on health, behavior, lifestyle, grooming, ownership, and more.

python
print(
    dappier_tool.get_iheartcats_recommendations(
        query="cat care advice", similarity_top_k=3
    )
)

GreenMonster Articles

A helpful guide to making conscious and compassionate choices that help people, animals, and the planet.

python
print(
    dappier_tool.get_greenmonster_recommendations(
        query="sustainable living", similarity_top_k=3
    )
)

WISH-TV News

Politics, breaking news, multicultural news, Hispanic language content, Entertainment, Health, Education, and many more.

python
print(
    dappier_tool.get_wishtv_recommendations(
        query="latest breaking news", similarity_top_k=3
    )
)

9 and 10 News

Up-to-date local news, weather forecasts, sports coverage, and community stories for Northern Michigan, including the Cadillac and Traverse City areas.

python
print(
    dappier_tool.get_nine_and_ten_news_recommendations(
        query="northern michigan local news", similarity_top_k=3
    )
)

Using the Dappier AI Recommendations tool in an Agent

We can create an agent with access to the Dappier AI Recommendations tool start testing it out:

python
from llama_index.core.agent import FunctionAgent
from llama_index.llms.openai import OpenAI

agent = FunctionAgent(
    tools=dappier_tool_list,
    llm=OpenAI(model="gpt-4o"),
)
python
print(
    await agent.run(
        "Get latest sports news, lifestyle news, breaking news, dog care advice and summarize it into different sections, with source links."
    )
)