Skip to content

Instantly share code, notes, and snippets.

@jskyzero
Created April 4, 2023 01:43
Show Gist options
  • Save jskyzero/085e6694c1eab3ffcb74584660ff43e4 to your computer and use it in GitHub Desktop.
Save jskyzero/085e6694c1eab3ffcb74584660ff43e4 to your computer and use it in GitHub Desktop.
ChatGPT_With_PDF.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPQBjlzaiPsozxYuuOwwvJb",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jskyzero/085e6694c1eab3ffcb74584660ff43e4/chatgpt_with_pdf.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "d6XDxCevCvUx",
"outputId": "cde4821d-309b-4f02-c2d3-4c37943bee78"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
],
"source": [
"# 建议将 PDF 文件保存在 Google Drive 上\n",
"\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"source": [
"# 在 Google Drive 上的工作目录\n",
"WORK_DIR = \"/content/drive/MyDrive/ChatGPT/PDF/\"\n",
"# env 文件名称,里面存储 OPENAI API KEY\n",
"ENV_FILE = \"key.env\"\n",
"# 处理处理的原文件\n",
"SRC_FILE = \"GameAIPro_Chapter11_Reactivity_and_Deliberation_in_Decision-Making_Systems.pdf\"\n",
"# 缓存的向量 index 文件\n",
"# INDEX_FILE = SRC_FILE + \".index\"\n",
"INDEX_FILE = \"index.temp\""
],
"metadata": {
"id": "lGGG4CF6DP69"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%capture\n",
"# update or install the necessary libraries\n",
"!pip install --upgrade llama_index\n",
"!pip install --upgrade langchain\n",
"!pip install --upgrade python-dotenv"
],
"metadata": {
"id": "Xq6fr7wlDt13"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import logging\n",
"import sys\n",
"\n",
"logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
"logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
],
"metadata": {
"id": "R1kf9g1WDw0S"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from llama_index import GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext\n",
"from llama_index.response.notebook_utils import display_response\n",
"from llama_index.prompts.prompts import QuestionAnswerPrompt\n",
"from langchain.chat_models import ChatOpenAI\n",
"from IPython.display import Markdown, display\n",
"from langchain.callbacks.base import CallbackManager\n",
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler"
],
"metadata": {
"id": "gO74UNt6ED2U"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Load environment variables (OPENAI_API_KEY)\n",
"\n",
"import os\n",
"import shutil\n",
"from dotenv import load_dotenv\n",
"\n",
"shutil.copyfile(os.path.join(WORK_DIR, ENV_FILE), \".env\")\n",
"\n",
"load_dotenv()\n",
"\n",
"# API configuration\n",
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
"if OPENAI_API_KEY == None:\n",
" raise Exception(\"Need set OPENAI_API_KEY\")"
],
"metadata": {
"id": "sUGiCPgzEGtd"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Load pdf to documents\n",
"\n",
"from pathlib import Path\n",
"from llama_index import download_loader\n",
"\n",
"# 中文 PDF 建议使用 CJKPDFReader,英文建议用 PDFReader\n",
"# 其他类型文件,请去 https://llamahub.ai/ 寻找合适的 Loader\n",
"# CJKPDFReader = download_loader(\"CJKPDFReader\")\n",
"PDFReader = download_loader(\"PDFReader\")\n",
"\n",
"loader = PDFReader()\n",
"index_file = os.path.join(Path(WORK_DIR), Path(INDEX_FILE))\n",
"\n",
"service_context = ServiceContext.from_defaults(chunk_size_limit=512)\n",
"\n",
"NeedNew = False\n",
"\n",
"if os.path.exists(index_file) == False or NeedNew:\n",
" documents = loader.load_data(file=os.path.join(Path(WORK_DIR), Path(SRC_FILE)))\n",
" # 默认 chunk_size_limit=4096,缩减 chunk_size 可以有效降低 Token 使用,但是会导致最终提供给 GPT 的上下文变少,从而影响效果\n",
" index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)\n",
" index.save_to_disk(index_file)\n",
"else:\n",
" index = GPTSimpleVectorIndex.load_from_disk(index_file)"
],
"metadata": {
"id": "95rof9MpFDp3"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"llm_predictor = LLMPredictor(llm=ChatOpenAI(\n",
" # 将 temperature 范围为 0-1,越接近0越具备创造性\n",
" # 典型值:0(arc53/DocsGPT)、0.2(madawei2699/myGPTReader)\n",
" temperature=0.2,\n",
" model_name=\"gpt-3.5-turbo\",\n",
"))\n",
"\n",
"\n",
"QUESTION_ANSWER_PROMPT_TMPL = (\n",
" \"Context information is below. \\n\"\n",
" \"---------------------\\n\"\n",
" \"{context_str}\"\n",
" \"\\n---------------------\\n\"\n",
" \"{query_str}\\n\"\n",
")\n",
"\n",
"QUESTION_ANSWER_PROMPT_TMPL_2 = \"\"\"\n",
"You are an AI assistant providing helpful advice. \n",
"You are given the following extracted parts of a long document and a question. Provide a conversational answer based on the context provided.\n",
"If you can't find the answer in the context below, just say \"Hmm, I'm not sure.\" Don't try to make up an answer.\n",
"If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.\n",
"\n",
"\n",
"\n",
"Context information is below.\n",
"=========\n",
"{context_str}\n",
"=========\n",
"{query_str}\n",
"\"\"\"\n",
"\n",
"QUESTION_ANSWER_PROMPT = QuestionAnswerPrompt(QUESTION_ANSWER_PROMPT_TMPL_2)\n",
"\n",
"def chat(query):\n",
" # 当 chunk_size 较小以及问题较为简洁时,回答的语言就不是很好控制,需要在问题增加内容。\n",
" # 目前在 prompt 上进行多次尝试无效,所以直接加到query 上。\n",
" query = query + \" 请使用中文回答。\"\n",
" result = index.query(\n",
" query,\n",
" #llm_predictor=llm_predictor,\n",
" text_qa_template=QUESTION_ANSWER_PROMPT,\n",
" # default: For the given index, “create and refine” an answer by sequentially \n",
" # going through each Node; make a separate LLM call per Node. Good for more \n",
" # detailed answers.\n",
" # compact: For the given index, “compact” the prompt during each LLM call \n",
" # by stuffing as many Node text chunks that can fit within the maximum prompt size. \n",
" # If there are too many chunks to stuff in one prompt, “create and refine” an answer \n",
" # by going through multiple prompts.\n",
" # tree_summarize: Given a set of Nodes and the query, recursively construct a \n",
" # tree and return the root node as the response. Good for summarization purposes.\n",
" response_mode=\"tree_summarize\",\n",
" similarity_top_k=3,\n",
" # mode=\"default\" will a create and refine an answer sequentially through \n",
" # the nodes of the list. \n",
" # mode=\"embedding\" will synthesize an answer by \n",
" # fetching the top-k nodes by embedding similarity.\n",
" mode=\"embedding\",\n",
" )\n",
" print(f\"Token used: {llm_predictor.last_token_usage}, total used: {llm_predictor.total_tokens_used}\")\n",
" return result\n",
"\n",
"# It's not work now, please don't use it.\n",
"# Bug: https://github.com/jerryjliu/llama_index/issues/831\n",
"def chat_stream(query):\n",
" return index.query(\n",
" query,\n",
" #llm_predictor=llm_predictor,\n",
" text_qa_template=QUESTION_ANSWER_PROMPT,\n",
" response_mode=\"tree_summarize\",\n",
" similarity_top_k=3,\n",
" streaming=True,\n",
" mode=\"embedding\",\n",
" )\n",
"\n",
"# response_stream = chat_stream(\"这本书讲了什么?\")\n",
"# response_stream.print_response_stream()"
],
"metadata": {
"id": "21mbGeiZH7Df"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"display_response(chat(\"详细说明这篇文章讲了什么\"))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 378
},
"id": "9eEvPuhkIHXh",
"outputId": "15707cda-6edc-40b4-fd49-924815189ba9"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Token used: 0, total used: 0\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Final Response:`** 这篇文章讲述了决策系统的设计,特别是在视频游戏中的应用。文章提出了反应性和审议机制作为两个主要元素,以便在设计决策系统时考虑。文章还提出了一种三层架构,用于管理行动,以减少决策系统的内在复杂性。"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "---"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Source Node 1/3`**"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**Document ID:** ac772eeb-3a34-4672-9f0e-9ed3330d0fca<br>**Similarity:** 0.7046596264502671<br>**Text:** The first \none is that the Deliberating Layer explicitly uses decision models to manage actions, ...<br>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "---"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Source Node 2/3`**"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**Document ID:** 04376ab0-06f2-48c5-b712-23b66ebba54d<br>**Similarity:** 0.7015029056814353<br>**Text:** in the design of every video game agent. \nBy showing how to integrate these key concepts as core ...<br>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "---"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Source Node 3/3`**"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**Document ID:** 694b1afd-bf7f-4e89-ae38-83290a970588<br>**Similarity:** 0.6981718779929988<br>**Text:** 137Reactivity \tand\tDeliberation \tin\t\nDecision-Making \tSystems\nCarle Côté11\n11.1\t \tIntroduction\nDe...<br>"
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [
"display_response(chat(\"详细说明文章中深思熟虑的含义\"))"
],
"metadata": {
"id": "VbezXH9AOgJY",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 361
},
"outputId": "dc90dd2b-c1cb-4e09-b712-3cc2a01c1bdf"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Token used: 0, total used: 0\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Final Response:`** 深思熟虑是指在决策系统设计中,使用决策模型来管理行动,而不是行动本身。它可以帮助我们减少决策系统的复杂性,从而更容易选择何时使用FSM、BT、规划器或其他决策模型,而不是纯粹的经验方法。"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "---"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Source Node 1/3`**"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**Document ID:** 2a185e11-b1ad-4f36-b8c1-2cfd13436c29<br>**Similarity:** 0.7300689746682939<br>**Text:** 11. Reactivity and Deliberation in Decision-Making Systems\n conceptual model of an existing actio...<br>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "---"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Source Node 2/3`**"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**Document ID:** ac772eeb-3a34-4672-9f0e-9ed3330d0fca<br>**Similarity:** 0.7296706779467793<br>**Text:** The first \none is that the Deliberating Layer explicitly uses decision models to manage actions, ...<br>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "---"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**`Source Node 3/3`**"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "**Document ID:** 04376ab0-06f2-48c5-b712-23b66ebba54d<br>**Similarity:** 0.7267166638755808<br>**Text:** in the design of every video game agent. \nBy showing how to integrate these key concepts as core ...<br>"
},
"metadata": {}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment