mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
163 lines
4.7 KiB
Plaintext
163 lines
4.7 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# AnalyticDB\n",
|
|||
|
"\n",
|
|||
|
"This notebook shows how to use functionality related to the AnalyticDB vector database.\n",
|
|||
|
"To run, you should have an [AnalyticDB](https://www.alibabacloud.com/help/en/analyticdb-for-postgresql/latest/product-introduction-overview) instance up and running:\n",
|
|||
|
"- Using [AnalyticDB Cloud Vector Database](https://www.alibabacloud.com/product/hybriddb-postgresql). Click here to fast deploy it."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 1,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
|
|||
|
"from langchain.text_splitter import CharacterTextSplitter\n",
|
|||
|
"from langchain.vectorstores import AnalyticDB"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"source": [
|
|||
|
"Split documents and get embeddings by call OpenAI API"
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"from langchain.document_loaders import TextLoader\n",
|
|||
|
"loader = TextLoader('../../../state_of_the_union.txt')\n",
|
|||
|
"documents = loader.load()\n",
|
|||
|
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
|
|||
|
"docs = text_splitter.split_documents(documents)\n",
|
|||
|
"\n",
|
|||
|
"embeddings = OpenAIEmbeddings()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"source": [
|
|||
|
"Connect to AnalyticDB by setting related ENVIRONMENTS.\n",
|
|||
|
"```\n",
|
|||
|
"export PG_HOST={your_analyticdb_hostname}\n",
|
|||
|
"export PG_PORT={your_analyticdb_port} # Optional, default is 5432\n",
|
|||
|
"export PG_DATABASE={your_database} # Optional, default is postgres\n",
|
|||
|
"export PG_USER={database_username}\n",
|
|||
|
"export PG_PASSWORD={database_password}\n",
|
|||
|
"```\n",
|
|||
|
"\n",
|
|||
|
"Then store your embeddings and documents into AnalyticDB"
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"import os\n",
|
|||
|
"\n",
|
|||
|
"connection_string = AnalyticDB.connection_string_from_db_params(\n",
|
|||
|
" driver=os.environ.get(\"PG_DRIVER\", \"psycopg2cffi\"),\n",
|
|||
|
" host=os.environ.get(\"PG_HOST\", \"localhost\"),\n",
|
|||
|
" port=int(os.environ.get(\"PG_PORT\", \"5432\")),\n",
|
|||
|
" database=os.environ.get(\"PG_DATABASE\", \"postgres\"),\n",
|
|||
|
" user=os.environ.get(\"PG_USER\", \"postgres\"),\n",
|
|||
|
" password=os.environ.get(\"PG_PASSWORD\", \"postgres\"),\n",
|
|||
|
")\n",
|
|||
|
"\n",
|
|||
|
"vector_db = AnalyticDB.from_documents(\n",
|
|||
|
" docs,\n",
|
|||
|
" embeddings,\n",
|
|||
|
" connection_string= connection_string,\n",
|
|||
|
")"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"source": [
|
|||
|
"Query and retrieve data"
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
}
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
|||
|
"docs = vector_db.similarity_search(query)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n",
|
|||
|
"\n",
|
|||
|
"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n",
|
|||
|
"\n",
|
|||
|
"One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n",
|
|||
|
"\n",
|
|||
|
"And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"print(docs[0].page_content)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": null,
|
|||
|
"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.10.9"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 1
|
|||
|
}
|