Skip to main content

PIP Package

PyPI version Open In Colab

๐ŸŒŸ Exciting News! Now, you can integrate gpt-researcher with your apps seamlessly!

Steps to Install GPT Researcher

Follow these easy steps to get started:

  1. Pre-requisite: Ensure Python 3.10+ is installed on your machine ๐Ÿ’ป
  2. Install gpt-researcher: Grab the official package from PyPi.
pip install gpt-researcher
  1. Environment Variables: Create a .env file with your OpenAI API key or simply export it
export OPENAI_API_KEY={Your OpenAI API Key here}
export TAVILY_API_KEY={Your Tavily API Key here}
  1. Start using GPT Researcher in your own codebase

Example Usage ๐Ÿ“

from gpt_researcher import GPTResearcher
import asyncio


from gpt_researcher import GPTResearcher
import asyncio


async def get_report(query: str, report_type: str) -> str:
researcher = GPTResearcher(query, report_type)
research_result = await researcher.conduct_research()
report = await researcher.write_report()
return report

if __name__ == "__main__":
query = "what team may win the NBA finals?"
report_type = "research_report"

report = asyncio.run(get_report(query, report_type))
print(report)

Specific Examples ๐ŸŒ

Example 1: Research Report ๐Ÿ“š

query = "Latest developments in renewable energy technologies"
report_type = "research_report"

Example 2: Resource Report ๐Ÿ“‹

query = "List of top AI conferences in 2023"
report_type = "resource_report"

Example 3: Outline Report ๐Ÿ“

query = "Outline for an article on the impact of AI in education"
report_type = "outline_report"

Integration with Web Frameworks ๐ŸŒ

FastAPI Example

from fastapi import FastAPI
from gpt_researcher import GPTResearcher
import asyncio

app = FastAPI()

@app.get("/report/{report_type}")
async def get_report(query: str, report_type: str) -> dict:
researcher = GPTResearcher(query, report_type)
research_result = await researcher.conduct_research()
report = await researcher.write_report()
return {"report": report}

# Run the server
# uvicorn main:app --reload

Flask Example

Pre-requisite: Install flask with the async extra.

pip install 'flask[async]'
from flask import Flask, request
from gpt_researcher import GPTResearcher

app = Flask(__name__)

@app.route('/report/<report_type>', methods=['GET'])
async def get_report(report_type):
query = request.args.get('query')
researcher = GPTResearcher(query, report_type)
research_result = await researcher.conduct_research()
report = await researcher.write_report()
return report

# Run the server
# flask run

Run the server

flask run

Example Request

curl -X GET "http://localhost:5000/report/research_report?query=what team may win the nba finals?"

Note: The above code snippets are just examples. You can customize them as per your requirements.

Getters and Setters

If you're interested in getting more details about the research, you can use the following getters:

Get Research Sources

Sources are the URLs that were used to gather information for the research.

source_urls = researcher.get_source_urls()

Get Research Context

Context is all the retrieved information from the research. It includes the sources and their corresponding content.

research_context = researcher.get_research_context()

Get Research Costs

Costs are the number of tokens consumed during the research process.

research_costs = researcher.get_costs()

Set Verbose

You can set the verbose mode to get more detailed logs.

researcher.set_verbose(True)

Add Costs

You can also add costs to the research process if you want to track the costs from external usage.

researcher.add_costs(0.22)