Created
September 15, 2023 18:01
-
-
Save jhsu/be2f9814ac10a159e3db00c6318ac896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The dotenv extension is already loaded. To reload it, use:\n", | |
" %reload_ext dotenv\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"from langchain.document_loaders import BrowserlessLoader\n", | |
"\n", | |
"%load_ext dotenv\n", | |
"%dotenv\n", | |
"\n", | |
"api_token = os.getenv('BROWSERLESS_API_KEY')\n", | |
"\n", | |
"if api_token is None:\n", | |
"\traise Exception('BROWSERLESS_API_KEY not found in environment variables')\n", | |
"\n", | |
"loader = BrowserlessLoader(\n", | |
"\tapi_token=api_token,\n", | |
"\turls=[\"https://read.cv/johnbrandon\",\n", | |
"\t],\n", | |
"\ttext_content=True,\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from langchain.llms import OpenAI\n", | |
"from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n", | |
"\n", | |
"documents = loader.load()\n", | |
"# if documents[0]:\n", | |
"# \tprint(documents[0].page_content)\n", | |
"\n", | |
"chain = load_qa_with_sources_chain(OpenAI(temperature=0))\n", | |
"\n", | |
"def print_answers(question: str):\n", | |
"\tprint(chain(\n", | |
"\t\t{ \"input_documents\": documents, \"question\": question },\n", | |
"\t\treturn_only_outputs=True\n", | |
"\t)[\"output_text\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" John Brandon has been a real estate broker in New York City for 11 years.\n", | |
"SOURCES: https://read.cv/johnbrandon\n" | |
] | |
} | |
], | |
"source": [ | |
"print_answers(\"How long as the person worked as a real estate agent in New York?\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.16" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment