GPT Researcherをpipenvでインストールするメモ

kght6123,4 min read

GPT Researcherとは?

任意のトピックについてオンラインで包括的な調査を行う自律エージェントといわれていて、詳細で事実に基づいた偏りのない研究レポートが作成できます。

内部の主な仕組みとしてはPlan-and-SolveとRAGを使っているそうです。

機能とか見てみた感じ、任意のドキュメントを置いてそれを元に調査を行うことができるのでその部分にRAGを使っていて、包括的な調査の部分はPlan-and-Solve(私はあまり詳しくないですが)の部分で実現してそう。

https://github.com/assafelovic/gpt-researcher (opens in a new tab)

これをMacにpipenvでインストールして、Pythonのプログラムから使えるようにしました。

前提条件

pyenvでPythonをインストールしていることを前提とします。

$ which python
/Users/kght6123/.pyenv/versions/anaconda3-2022.10/bin/python
$ which pyenv
/opt/homebrew/bin/pyenv
$ which pip
/Users/kght6123/.pyenv/versions/anaconda3-2022.10/bin/pip

Pythonのバージョンをアップデートする(任意)

pyenv install -l
 
  3.10.9
  3.10.10
  3.11.0
  3.11-dev
  3.11.1
  3.11.2
  3.12.0a5
  3.12-dev
 
pyenv install 3.11.2
pyenv global 3.11.2
pyenv versions
  system
* 3.11.2 (set by /Users/kght6123/.pyenv/version)
  anaconda3-2022.10
  anaconda3-2022.10/envs/ldm

pipenvをインストールする

brew install pipenv

プロジェクトを作成して、GPT Researcherをインストールする

mkdir gpt-researcher-app && cd gpt-researcher-app
pipenv --python 3.11.2
pip install gpt-researcher

VSCodeで開いて、環境変数とPythonのプログラムを書く

code-insiders .

.envファイルを作成する

OpenAI APIはこちら (opens in a new tab)(基本、有料)

そのほかのAPIキーは以下のサイトから取得する。一定量を超えなければ無料で使える。

OPENAI_API_KEY=sk-proj-xxxxx
TAVILY_API_KEY=tvly-xxxxx
LANGCHAIN_API_KEY=lsv2_pt_xxxxx
DOC_PATH=./docs

RETRIEVER=tavily
# RETRIEVER=google
# RETRIEVER=bing
GOOGLE_API_KEY=
GOOGLE_CX_KEY=
BING_API_KEY=

# the name of the embedding model to use for Ollama
OLLAMA_EMBEDDING_MODEL=
# the Ollama endpoint to use
OLLAMA_BASE_URL=

Pythonのプログラムを書く

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"
 
  # 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"
 
  report = asyncio.run(get_report(query, report_type))
  print(report)
 
  # Get Research Sources​
  # source_urls = researcher.get_source_urls()
 
  # Get Research Context​
  # research_context = researcher.get_research_context()
 
  # Get Research Costs​
  # research_costs = researcher.get_costs()
 
  # Set Verbose
  # researcher.set_verbose(True)
 
  # Add Costs
  # researcher.add_costs(0.22)

実行する

$ pipenv run python main.py

まとめ

GPT Researcherをpipenvでインストールして、Pythonのプログラムから使えるようにしました。

実行ログをみてみると分かりますが、参照したURLやコンテンツが表示されているので、それを元にこちらで再調査を行うこともできそうです。また、実行結果もMarkdown形式で出力されるので、それをテンプレートに加筆修正してレポートを作成する、、、ということもできそうです。

また、あまり詳しくない技術について、その技術の概要や最新の動向を調査するのにも使えそうです。

実行例

参考サイト

Copyright kght6123.RSS