Back to Gpt Researcher

Tavily Samples

docs/docs/examples/examples.ipynb

3.5.01.5 KB
Original Source

Tavily Samples

Setup

python
# install tavily
!pip install tavily-python
python
# import and connect
from tavily import TavilyClient
client = TavilyClient(api_key="")
python
# simple query using tavily's advanced search
client.search("What happend in the latest burning man floods?", search_depth="advanced")

Sample 1: Reseach Report using Tavily and GPT-4 with Langchain

python
# install lanchain
!pip install langchain
python
# set up openai api key
openai_api_key = ""
python
# libraries
from langchain.adapters.openai import convert_openai_messages
from langchain_community.chat_models import ChatOpenAI

# setup query
query = "What happend in the latest burning man floods?"

# run tavily search
content = client.search(query, search_depth="advanced")["results"]

# setup prompt
prompt = [{
    "role": "system",
    "content":  f'You are an AI critical thinker research assistant. '\
                f'Your sole purpose is to write well written, critically acclaimed,'\
                f'objective and structured reports on given text.'
}, {
    "role": "user",
    "content": f'Information: """{content}"""\n\n' \
               f'Using the above information, answer the following'\
               f'query: "{query}" in a detailed report --'\
               f'Please use MLA format and markdown syntax.'
}]

# run gpt-4
lc_messages = convert_openai_messages(prompt)
report = ChatOpenAI(model='gpt-4',openai_api_key=openai_api_key).invoke(lc_messages).content

# print report
print(report)