Example: Company Information Extraction
This example demonstrates a powerful, real-world workflow: using Tavily to retrieve deep organizational context from the web, and then piping that context into an OpenAI Large Language Model (LLM) to extract and format specific structured data points.
We utilize the get_company_info helper method, which is specifically tuned to run parallel searches across news, finance, and general topics to build a robust profile of a company.
Prerequisites
You will need the official openai Python package installed, along with API keys for both services.
pip install openai tavily-python
The Workflow Script
import os
from openai import OpenAI
from tavily import TavilyClient
# 1. Initialize both clients using environment variables
openai_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
# The target company we want to research
query = "Information about Nvidia (nvidia.com)"
print(f"Calling Tavily company retrieval with query: {query}...")
print("[Note]: Using 'advanced' search depth may take up to 10 seconds.")
# 2. Retrieve the context
# We request the top 7 results. Tavily will automatically sort them by relevance.
context = tavily_client.get_company_info(
query=query,
search_depth="advanced",
max_results=7
)
# 3. Construct the LLM Prompt
# We use XML-like tags (<context>) to clearly demarcate the retrieved knowledge
# from the system instructions. This prevents prompt injection and hallucinations.
PROMPT = f"""
You are a business analyst tasked with collecting and summarizing company information and insights from a given context.
Anything between the following `context` html blocks is retrieved from a knowledge bank, and you MUST only use it to extract information.
<context>
{context}
<context/>
Given the context above only, please extract and answer based on the following parameters:
- company_name: What is the company name?
- company_domain: What is the company's root site domain name?
- description: What does this company do?
- business_model: How does this company make money?
- financials: If company is public, information from recent reports and stock performance.
- recent_news: Recent news about the company
- leadership_team: The leadership team (C-levels eg, CEO CFO CTO etc).
- general_information: Any valuable or insightful information found in the context not yet mentioned.
You must answer short and concise without opening remarks.
REMEMBER: If there is no relevant information within the context for a given param, you must answer with "None". For example: company_name: None.
"""
print(f"Calling OpenAI to extract structured information...")
# 4. Execute the LLM Extraction
chat_completion = openai_client.chat.completions.create(
messages=[
{
"role": "user",
"content": PROMPT,
}
],
# A low temperature (0.4) forces the model to be factual and deterministic,
# reducing the chance of hallucination outside the provided context.
temperature=0.4,
max_tokens=4000,
model="gpt-4-1106-preview",
)
# 5. Output the result
print("\n--- Analysis Result ---")
print(chat_completion.choices[0].message.content)
Why this architecture works
If you simply asked GPT-4 "What is recent news about Nvidia?", the model would hallucinate or fail if the news occurred after its training cutoff date.
By placing Tavily in front of the LLM (a classic RAG pattern), the LLM is transformed from a static knowledge base into a powerful reading comprehension and formatting engine, acting only upon the live, fact-checked data provided by the Tavily context string.