mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-01 21:35:34 +00:00
241 lines
6.4 KiB
Plaintext
241 lines
6.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d464a12a",
|
|
"metadata": {
|
|
"id": "eg0Hwptz9g5q"
|
|
},
|
|
"source": [
|
|
"# SQLite\n",
|
|
"\n",
|
|
">[SQLite](https://en.wikipedia.org/wiki/SQLite) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the most widely deployed database engine, as it is used by several of the top web browsers, operating systems, mobile phones, and other embedded systems.\n",
|
|
"\n",
|
|
"In this walkthrough we'll create a simple conversation chain which uses `ConversationEntityMemory` backed by a `SqliteEntityStore`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5c923f56-24a9-4f8f-9b91-138cc025c47e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n",
|
|
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "61fda020-23a2-4605-afad-58260535ec8c",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Usage\n",
|
|
"\n",
|
|
"To use the storage you need to provide only 2 things:\n",
|
|
"\n",
|
|
"1. Session Id - a unique identifier of the session, like user name, email, chat id etc.\n",
|
|
"2. Connection string - a string that specifies the database connection. For SQLite, that string is `slqlite:///` followed by the name of the database file. If that file doesn't exist, it will be created."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "4576e914a866fb40",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2023-08-28T10:04:38.077748Z",
|
|
"start_time": "2023-08-28T10:04:36.105894Z"
|
|
},
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_community.chat_message_histories import SQLChatMessageHistory\n",
|
|
"\n",
|
|
"chat_message_history = SQLChatMessageHistory(\n",
|
|
" session_id=\"test_session_id\", connection_string=\"sqlite:///sqlite.db\"\n",
|
|
")\n",
|
|
"\n",
|
|
"chat_message_history.add_user_message(\"Hello\")\n",
|
|
"chat_message_history.add_ai_message(\"Hi\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "b476688cbb32ba90",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2023-08-28T10:04:38.929396Z",
|
|
"start_time": "2023-08-28T10:04:38.915727Z"
|
|
},
|
|
"collapsed": false,
|
|
"jupyter": {
|
|
"outputs_hidden": false
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[HumanMessage(content='Hello'), AIMessage(content='Hi')]"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chat_message_history.messages"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e400509a-1957-4d1d-bbd6-01e8dc3dccb3",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Chaining\n",
|
|
"\n",
|
|
"We can easily combine this message history class with [LCEL Runnables](/docs/how_to/message_history)\n",
|
|
"\n",
|
|
"To do this we will want to use OpenAI, so we need to install that. We will also need to set the OPENAI_API_KEY environment variable to your OpenAI key.\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"pip install -U langchain-openai\n",
|
|
"\n",
|
|
"export OPENAI_API_KEY='sk-xxxxxxx'\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "6558418b-0ece-4d01-9661-56d562d78f7a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
|
"from langchain_core.runnables.history import RunnableWithMessageHistory\n",
|
|
"from langchain_openai import ChatOpenAI"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "82149122-61d3-490d-9bdb-bb98606e8ba1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prompt = ChatPromptTemplate.from_messages(\n",
|
|
" [\n",
|
|
" (\"system\", \"You are a helpful assistant.\"),\n",
|
|
" MessagesPlaceholder(variable_name=\"history\"),\n",
|
|
" (\"human\", \"{question}\"),\n",
|
|
" ]\n",
|
|
")\n",
|
|
"\n",
|
|
"chain = prompt | ChatOpenAI()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "2df90853-b67c-490f-b7f8-b69d69270b9c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"chain_with_history = RunnableWithMessageHistory(\n",
|
|
" chain,\n",
|
|
" lambda session_id: SQLChatMessageHistory(\n",
|
|
" session_id=session_id, connection_string=\"sqlite:///sqlite.db\"\n",
|
|
" ),\n",
|
|
" input_messages_key=\"question\",\n",
|
|
" history_messages_key=\"history\",\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "0ce596b8-3b78-48fd-9f92-46dccbbfd58b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# This is where we configure the session id\n",
|
|
"config = {\"configurable\": {\"session_id\": \"<SQL_SESSION_ID>\"}}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "38e1423b-ba86-4496-9151-25932fab1a8b",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"AIMessage(content='Hello Bob! How can I assist you today?')"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain_with_history.invoke({\"question\": \"Hi! I'm bob\"}, config=config)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "2ee4ee62-a216-4fb1-bf33-57476a84cf16",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"AIMessage(content='Your name is Bob! Is there anything specific you would like assistance with, Bob?')"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain_with_history.invoke({\"question\": \"Whats my name\"}, config=config)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"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.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|