Files
langchain/docs/modules/chat/examples/qa.ipynb
Harrison Chase d6584fde16 cr
2023-03-01 21:28:30 -08:00

142 lines
3.5 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"id": "ca494454",
"metadata": {},
"source": [
"# Question Answering"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bed0dfef",
"metadata": {},
"outputs": [],
"source": [
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.vectorstores import Chroma\n",
"from langchain.docstore.document import Document\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain.indexes.vectorstore import VectorstoreIndexCreator"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "15a6d191",
"metadata": {},
"outputs": [],
"source": [
"index_creator = VectorstoreIndexCreator()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "483815ae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running Chroma using direct local API.\n",
"Using DuckDB in-memory for database. Data will be transient.\n"
]
}
],
"source": [
"from langchain.document_loaders import TextLoader\n",
"loader = TextLoader('../../state_of_the_union.txt')\n",
"docsearch = index_creator.from_loaders([loader]).vectorstore"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "35fd98c0",
"metadata": {},
"outputs": [],
"source": [
"query = \"What did the president say about Justice Breyer\"\n",
"docs = docsearch.similarity_search(query)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "86116c78",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat.question_answering import QAChain\n",
"from langchain.chat_models import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6b5d1a80",
"metadata": {},
"outputs": [],
"source": [
"chain = QAChain.from_model(model = ChatOpenAI(temperature=0))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5ff56c1d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The President honored Justice Stephen Breyer, an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, for his dedicated service to the country. The President also mentioned that one of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court, and he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to continue Justice Breyers legacy of excellence.'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Justice Breyer\"\n",
"chain.run(input_documents=docs, question=query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf32e6d6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}