PIP 包
🌟 激动人心的消息! 现在,您可以将 gpt-researcher 无缝集成到您的应用程序中!
安装 GPT Researcher 的步骤
按照以下简单步骤开始
- 先决条件:确保您的机器上已安装 Python 3.10+ 💻
- 安装 gpt-researcher:从 PyPi 获取官方包。
pip install gpt-researcher
- 环境变量: 创建一个包含您的 OpenAI API 密钥的 .env 文件,或直接导出它
export OPENAI_API_KEY={Your OpenAI API Key here}
export TAVILY_API_KEY={Your Tavily API Key here}
- 开始在您自己的代码库中使用 GPT Researcher
使用示例
from gpt_researcher import GPTResearcher
import asyncio
async def get_report(query: str, report_type: str):
researcher = GPTResearcher(query, report_type)
research_result = await researcher.conduct_research()
report = await researcher.write_report()
# Get additional information
research_context = researcher.get_research_context()
research_costs = researcher.get_costs()
research_images = researcher.get_research_images()
research_sources = researcher.get_research_sources()
return report, research_context, research_costs, research_images, research_sources
if __name__ == "__main__":
query = "what team may win the NBA finals?"
report_type = "research_report"
report, context, costs, images, sources = asyncio.run(get_report(query, report_type))
print("Report:")
print(report)
print("\nResearch Costs:")
print(costs)
print("\nNumber of Research Images:")
print(len(images))
print("\nNumber of Research Sources:")
print(len(sources))
具体示例
示例 1:研究报告
query = "Latest developments in renewable energy technologies"
report_type = "research_report"
示例 2:资源报告
query = "List of top AI conferences in 2023"
report_type = "resource_report"
示例 3:大纲报告
query = "Outline for an article on the impact of AI in education"
report_type = "outline_report"
与 Web 框架集成
FastAPI 示例
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()
source_urls = researcher.get_source_urls()
research_costs = researcher.get_costs()
research_images = researcher.get_research_images()
research_sources = researcher.get_research_sources()
return {
"report": report,
"source_urls": source_urls,
"research_costs": research_costs,
"num_images": len(research_images),
"num_sources": len(research_sources)
}
# Run the server
# uvicorn main:app --reload
Flask 示例
先决条件:使用 async extra 安装 flask。
pip install 'flask[async]'
from flask import Flask, request, jsonify
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()
source_urls = researcher.get_source_urls()
research_costs = researcher.get_costs()
research_images = researcher.get_research_images()
research_sources = researcher.get_research_sources()
return jsonify({
"report": report,
"source_urls": source_urls,
"research_costs": research_costs,
"num_images": len(research_images),
"num_sources": len(research_sources)
})
# Run the server
# flask run
运行服务器
flask run
请求示例
curl -X GET "https://:5000/report/research_report?query=what team may win the nba finals?"
Getter 和 Setter
GPT Researcher 提供了几种方法来检索有关研究过程的附加信息
获取研究来源
来源是用于收集研究信息的 URL。
source_urls = researcher.get_source_urls()
获取研究上下文
上下文是研究中检索到的所有信息。它包括来源及其相应的内容。
research_context = researcher.get_research_context()
获取研究成本
成本是在研究过程中消耗的令牌数量。
research_costs = researcher.get_costs()
获取研究图片
检索研究过程中找到的图片列表。
research_images = researcher.get_research_images()
获取研究来源
检索研究来源列表,包括标题、内容和图片。
research_sources = researcher.get_research_sources()
设置详细模式 (Verbose)
您可以设置详细模式以获取更详细的日志。
researcher.set_verbose(True)
添加成本
如果您想跟踪外部使用的成本,也可以将成本添加到研究过程中。
researcher.add_costs(0.22)
高级用法
自定义研究过程
您可以通过在初始化 GPTResearcher 时传递附加参数来自定义研究过程的各个方面
researcher = GPTResearcher(
query="Your research query",
report_type="research_report",
report_format="APA",
tone="formal and objective",
max_subtopics=5,
verbose=True
)
处理研究结果
进行研究后,您可以通过多种方式处理结果
# Conduct research
research_result = await researcher.conduct_research()
# Generate a standard report
report = await researcher.write_report()
# Generate a customized report with specific formatting requirements
custom_report = await researcher.write_report(custom_prompt="Answer in short, 2 paragraphs max without citations.")
# Generate a focused report for a specific audience
executive_summary = await researcher.write_report(custom_prompt="Create an executive summary focused on business impact and ROI. Keep it under 500 words.")
# Generate a report with specific structure requirements
technical_report = await researcher.write_report(custom_prompt="Create a technical report with problem statement, methodology, findings, and recommendations sections.")
# Generate a conclusion
conclusion = await researcher.write_report_conclusion(report)
# Get subtopics
subtopics = await researcher.get_subtopics()
# Get draft section titles for a subtopic
draft_titles = await researcher.get_draft_section_titles("Subtopic name")
使用自定义提示自定义报告生成
write_report 方法接受一个 custom_prompt 参数,让您完全控制研究的呈现方式
# After conducting research
research_result = await researcher.conduct_research()
# Generate a report with a custom prompt
report = await researcher.write_report(
custom_prompt="Based on the research, provide a bullet-point summary of the key findings."
)
自定义提示可用于多种目的
-
格式控制:指定报告的结构、长度或风格
report = await researcher.write_report(
custom_prompt="Write a blog post in a conversational tone using the research. Include headings and a conclusion."
) -
受众定位:为特定读者量身定制内容
report = await researcher.write_report(
custom_prompt="Create a report for technical stakeholders, focusing on methodologies and implementation details."
) -
专门输出:生成特定类型的内容
report = await researcher.write_report(
custom_prompt="Create a FAQ section based on the research with at least 5 questions and detailed answers."
)
自定义提示将与研究上下文相结合,以生成您的定制报告。
使用研究上下文
您可以使用研究上下文进行进一步处理或分析
# Get the full research context
context = researcher.get_research_context()
# Get similar written contents based on draft section titles
similar_contents = await researcher.get_similar_written_contents_by_draft_section_titles(
current_subtopic="Subtopic name",
draft_section_titles=["Title 1", "Title 2"],
written_contents=some_written_contents,
max_results=10
)
这份全面的文档应能帮助用户理解和充分利用 GPT Researcher 包的全部功能。