docs/examples/cookbooks/toolhouse_llamaindex.ipynb
<a href="https://colab.research.google.com/gist/iamdaniele/84cca60019384c4159df28e14e2dc61c/toolhouse-llamaindex-workflow.ipynb" target="_parent"></a>
In this notebook you'll learn how to create a sales prospecting workflow using Toolhouse and LlamaIndex. Sales prospecting allows companies to find the perfect potential customer based on the business's value proposition and target market.
The workflow will use a single agent to perform these activities:
Let's make sure all the required libraries are present. This example uses Llama 3.2 on Groq, but you can use any the LLMs supported by LlamaIndex.
%pip install llama-index
%pip install llama-index-llms-groq
%pip install toolhouse
Next, we'll pass the API keys.
To get a Toolhouse API key:
To get a Groq API Key, get access on Groq, then past your API key below.
Important: store your API keys safely when in production.
import os
os.environ[
"TOOLHOUSE_API_KEY"
] = "Get your Toolhouse API key at https://join.toolhouse.ai"
os.environ[
"GROQ_API_KEY"
] = "Get your Groq API key at https://console.groq.com"
We're going to import LlamaIndexas and Toolhouse. We then initialize Toolhouse and the Groq LLM.
from llama_index.llms.groq import Groq
from llama_index.core.agent import ReActAgent
from llama_index.core.memory import ChatMemoryBuffer
from toolhouse import Toolhouse, Provider
from llama_index.core.workflow import (
Context,
Event,
StartEvent,
StopEvent,
Workflow,
step,
)
llm = Groq(model="llama-3.2-11b-vision-preview")
th = Toolhouse(provider=Provider.LLAMAINDEX)
th.set_metadata("id", "llamaindex_agent")
th.set_metadata("timezone", 0)
The agent will require to search the web and get the contents of a page. To allow this, go to your Toolhouse dashboard and install the following tools:
The workflow will have four steps; we created an output event for each step to make the sequential aspect clearer.
Because Toolhouse integrates directly into LlamaIndex, you can pass the Toolhouse tools directly to the agent.
class WebsiteContentEvent(Event):
contents: str
class WebSearchEvent(Event):
results: str
class RankingEvent(Event):
results: str
class LogEvent(Event):
msg: str
class SalesRepWorkflow(Workflow):
agent = ReActAgent(
tools=th.get_tools(bundle="llamaindex test"),
llm=llm,
memory=ChatMemoryBuffer.from_defaults(),
)
@step
async def get_company_info(
self, ctx: Context, ev: StartEvent
) -> WebsiteContentEvent:
ctx.write_event_to_stream(
LogEvent(msg=f"Getting the contents of {ev.url}…")
)
prompt = f"Get the contents of {ev.url}, then summarize its key value propositions in a few bullet points."
contents = await self.agent.achat(prompt)
return WebsiteContentEvent(contents=str(contents.response))
@step
async def find_prospects(
self, ctx: Context, ev: WebsiteContentEvent
) -> WebSearchEvent:
ctx.write_event_to_stream(
LogEvent(
msg=f"Performing web searches to identify companies who can benefit from the business's offerings."
)
)
prompt = f"With that you know about the business, perform a web search to find 5 tech companies who may benefit from the business's product. Only answer with the names of the companies you chose."
results = await self.agent.achat(prompt)
return WebSearchEvent(results=str(results.response))
@step
async def select_best_company(
self, ctx: Context, ev: WebSearchEvent
) -> RankingEvent:
ctx.write_event_to_stream(
LogEvent(
msg=f"Selecting the best company who can benefit from the business's offering…"
)
)
prompt = "Select one company that can benefit from the business's product. Only use your knowledge to select the company. Respond with just the name of the company. Do not use tools."
results = await self.agent.achat(prompt)
ctx.write_event_to_stream(
LogEvent(
msg=f"The agent selected this company: {results.response}"
)
)
return RankingEvent(results=str(results.response))
@step
async def prepare_email(self, ctx: Context, ev: RankingEvent) -> StopEvent:
ctx.write_event_to_stream(
LogEvent(msg=f"Drafting a short email for sales outreach…")
)
prompt = f"Draft a short cold sales outreach email for the company you picked. Do not use tools."
email = await self.agent.achat(prompt)
ctx.write_event_to_stream(
LogEvent(msg=f"Here is the email: {email.response}")
)
return StopEvent(result=str(email.response))
Simply instantiate the workflow and pass the URL of a company to get started.
workflow = SalesRepWorkflow(timeout=None)
handler = workflow.run(url="https://toolhouse.ai")
async for event in handler.stream_events():
if isinstance(event, LogEvent):
print(event.msg)