LocalGPTResearcherおよびWebGPTResearcherツールは、特定のトピックまたはクエリに関する徹底的な研究の実施を支援するように設計されています。これらのツールは、GPTモデルの力を活用して詳細なレポートを生成し、さまざまな研究関連のタスクに最適です。 LocalGPTResearcherツールはローカルデータファイルにアクセスし、 WebGPTResearcherはWebから情報を取得します。
LocalGPTResearcher 、PDF、Word Documents、CSVSなど、さまざまなローカルファイル形式を使用できます。WebGPTResearcherはインターネットから直接データを取得し、最新の情報収集に適しています。Python 3がシステムにインストールされていることを確認してください。
PIPを使用して必要なパッケージをインストールします。
pip install gpt-researcherLocalGPTResearcherの場合、次の環境変数を設定する必要があります。
export DOC_PATH=/path/to/your/documents
export OPENAI_API_KEY=your-openai-api-key
export TAVILY_API_KEY=your-tavily-api-key WebGPTResearcherの場合、 OPENAI_API_KEYとTAVILY_API_KEYのみが必要です。
export OPENAI_API_KEY=your-openai-api-key
export TAVILY_API_KEY=your-tavily-api-keyこの例は、 LocalGPTResearcherを使用してローカルドキュメントに基づいてレポートを生成する方法を示しています。
from libs . community . langchain_community . tools . gpt_researcher . tool import LocalGPTResearcher # This will be changed after successful PR
# Initialize the tool
researcher_local = LocalGPTResearcher ( report_type = "research_report" )
# You can also define it as `researcher_local = LocalGPTResearcher()` - default report_type is research_report.
# Run a query
query = "What is the demographics of Apple inc look like?"
report = researcher_local . invoke ({ "query" : query })
print ( "Generated Report:" , report )この例は、 WebGPTResearcherを使用してWebデータに基づいてレポートを生成する方法を示しています。
from libs . community . langchain_community . tools . gpt_researcher . tool import WebGPTResearcher # This will be changed after successful PR
# Initialize the tool
researcher_web = WebGPTResearcher ( report_type = "research_report" ) # report_type="research_report" is optional as the default value is `research_report`
# Run a query
query = "What are the latest advancements in AI?"
report = researcher_web . invoke ({ "query" : query })
print ( "Generated Report:" , report )WebGPTResearcherを使用してAgentExecutor使用しますLLMと当社のツールを使用してエッセイを作成し、レポートの最後に引用/署名を提供するAgentExecutorラッパーを構築する方法を見てみましょう。
from libs . community . langchain_community . tools . gpt_researcher . tool import WebGPTResearcher # This will be changed after successful PR
from langchain import hub
from langchain_core . tools import Tool
from langchain . agents import AgentExecutor , create_react_agent
from langchain . tools import tool
from langchain_openai import ChatOpenAI
# Let us see how to use the WebGPTResearcher tool along with AgentExecutor to perform a grand task with decision making.
# 1. Let us build a Reactive Agent who takes decisions based on reasoning.
# 2. Let us give our agent 2 tools - WebGPTResearcher and a dummy tool that provides a signature at the end of the text
# 3. We can then wrap our agent and tools inside an AgentExecutor object and ask our question!
# The expectation is the response must be signed at the end after a long report on a research topic.
# Create a new tool called citation_provider.
@ tool
def citation_provider ( text : str ) -> str :
"""Provides a citation or signature"""
return " n - Written by GPT-Makesh n Thanks for reading! n "
# Create the WebGPTResearcher tool
researcher_web = WebGPTResearcher ( "research_report" )
# Initialize tools and components
tools = [
researcher_web ,
Tool (
name = "citation_tool" ,
func = citation_provider ,
description = "Useful for when you need to add citation or signature at the end of text" ,
),
]
# Create an LLM
llm = ChatOpenAI ( model = "gpt-4o" )
prompt = hub . pull ( "hwchase17/react" )
# Create the ReAct agent using the create_react_agent function
agent = create_react_agent (
llm = llm ,
tools = tools ,
prompt = prompt ,
stop_sequence = True ,
)
# Wrap the components inside an AgentExecutor
agent_executor = AgentExecutor . from_agent_and_tools ( agent = agent , tools = tools , verbose = True )
# Run the agent
question = "What are the recent advancements in AI? Provide a citation for your report too."
response = agent_executor . invoke ({ "input" : question })
print ( "Agent Response:" , response )WebGPTResearcherの単純なシーケンシャルチェーンレポートを作成する研究者と、レポートを評価してスコアリングするグレーダーを持つランナブルのチェーンを構築しましょう。
from libs . community . langchain_community . tools . gpt_researcher . tool import WebGPTResearcher
from langchain . prompts import ChatPromptTemplate
from langchain . schema . output_parser import StrOutputParser
from langchain . schema . runnable import RunnableLambda
from langchain_openai import ChatOpenAI
# Let us use WebGPTResearcher to grade the essay using LECL langchain Chaining tricks
# 1. Use the researcher to write an essay
# 2. Pass it as a chat_prompt_template (a runnable) to a grader to score the essay
# 3. Parse the output as a string
# Create a ChatOpenAI model
grader = ChatOpenAI ( model = "gpt-4o" )
researcher_tool = WebGPTResearcher ()
prompt_template = ChatPromptTemplate . from_messages (
[
( "system" , "You are a essay grader. Give score out of 10 in brief" ),
( "human" , "The essay: {essay}" ),
]
)
# Define our WebGPTResearcher tool as a RunnableLambda
researcher = RunnableLambda ( lambda x : researcher_tool . invoke ( x ))
# Create the combined chain using LangChain Expression Language (LCEL)
chain = researcher | prompt_template | grader | StrOutputParser ()
# Run the chain
result = chain . invoke ({ "query" : "What are the recent advancements in AI?" })
# Output
print ( result )BaseGPTResearcherを拡張しますBaseGPTResearcherクラスを拡張して、カスタムツールを作成できます。これが例です:
from libs . community . langchain_community . tools . gpt_researcher . tool import WebGPTResearcher
class CustomGPTResearcher ( BaseGPTResearcher ):
name = ""
description = ""
def __init__ ( self , report_type : ReportType = ReportType . RESEARCH ):
super (). __init__ ( report_type = report_type , report_source = "web" )
# Override or extend methods as needed (You need to implement `_run()` method, `_arun()` is optional)APIリファレンス:(GPT研究者ツール)[リンク]
以下に示すように、カスタムGPTRツールを定義できます。
import asyncio
from typing import Optional , Type
from langchain_core . callbacks import CallbackManagerForToolRun
from langchain_core . pydantic_v1 import BaseModel , Field
from langchain_core . tools import BaseTool
from gpt_researcher import GPTResearcher
class GPTRInput ( BaseModel ):
"""Input schema for the GPT-Researcher tool."""
query : str = Field ( description = "The search query for the research" )
class MyGPTResearcher ( BaseTool ):
name : str = "custom_gpt_researcher"
description : str = "Base tool for researching and producing detailed information about a topic or query using the internet."
args_schema : Type [ BaseModel ] = GPTRInput
async def get_report ( self , query : str ) -> str :
try :
researcher = GPTResearcher (
query = query ,
report_type = "research_report" ,
report_source = "web" ,
verbose = False
)
await researcher . conduct_research ()
report = await researcher . write_report ()
return report
except Exception as e :
raise ValueError ( f"Error generating report: { str ( e ) } " )
def _run (
self ,
query : str ,
run_manager : Optional [ CallbackManagerForToolRun ] = None
) -> str :
answer = asyncio . run ( self . get_report ( query = query ))
answer += " n n - By GPT-Makesh. n Thanks for reading!"
return answer
my_researcher = MyGPTResearcher ()
report = my_researcher . invoke ({ "query" : "What are the recent advancements in AI?" })
print ( report )または、既製の変更なしで提供されたツールを直接使用できます。
from libs . community . langchain_community . tools . gpt_researcher . tool import WebGPTResearcher , LocalGPTResearcher
# Use LocalGPTResearcher
researcher_local = LocalGPTResearcher ( report_type = "research_report" )
report = researcher_local . invoke ({ 'query' : "What can you tell about the company?" })
# Use WebGPTResearcher
researcher_web = WebGPTResearcher ( report_type = "research_report" )
report = researcher_web . invoke ({ 'query' : "What are the latest advancements in AI?" })gpt-4o-miniやgpt-4o (128Kコンテキスト)などのモデルを必要な場合にのみ使用して、パフォーマンスとコストに最適化されています。平均的な研究タスクには約3分かかり、費用は約0.005ドルです。GPT-Researcherツールを改善および拡張するための貢献を歓迎します。 Githubリポジトリにアクセスして、貢献を始めてください。