mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-04 00:00:34 +00:00
Chain sequence created with expression language
This commit is contained in:
@@ -53,7 +53,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'My name is Kimberly Nguyen, call me at 313-666-7440 or email me at real.slim.shady@gmail.com'"
|
||||
"'My name is Marie Santos, call me at 313-666-7440 or email me at real.slim.shady@gmail.com'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
@@ -87,7 +87,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'My name is Jordan Green, call me at 844-271-5959 or email me at real.slim.shady@gmail.com'"
|
||||
"'My name is Victoria Mckinney, call me at 713-549-8623 or email me at real.slim.shady@gmail.com'"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
@@ -122,7 +122,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'My name is Kelly Carlson, call me at 328-247-8738x096 or email me at yrodriguez@example.org'"
|
||||
"'My name is Billy Russo, call me at 970-996-9453x038 or email me at jamie80@example.org'"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
@@ -153,7 +153,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'My polish phone number is PZHC84292597714701'"
|
||||
"'My polish phone number is EVIA70648911396944'"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
@@ -258,7 +258,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'+48 32 543 00 97'"
|
||||
"'+48 533 220 543'"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
@@ -319,7 +319,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'My polish phone number is 32 538 43 33'"
|
||||
"'My polish phone number is +48 692 715 636'"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
@@ -348,7 +348,7 @@
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'text': 'You can find our super secret data at https://supersecretdata.com',\n",
|
||||
" 'output_text': 'You can find our super secret data at http://johnson-johnson.biz/'}"
|
||||
" 'anonymized_text': 'You can find our super secret data at https://www.fox.org/'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
@@ -364,11 +364,13 @@
|
||||
"\n",
|
||||
"def anonymize_func(inputs: dict) -> dict:\n",
|
||||
" text = inputs[\"text\"]\n",
|
||||
" return {\"output_text\": anonymizer.anonymize(text)}\n",
|
||||
" return {\"anonymized_text\": anonymizer.anonymize(text)}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"anonymize_chain = TransformChain(\n",
|
||||
" input_variables=[\"text\"], output_variables=[\"output_text\"], transform=anonymize_func\n",
|
||||
" input_variables=[\"text\"],\n",
|
||||
" output_variables=[\"anonymized_text\"],\n",
|
||||
" transform=anonymize_func,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"anonymize_chain(\"You can find our super secret data at https://supersecretdata.com\")"
|
||||
@@ -379,7 +381,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\\\n",
|
||||
"Later, you can, for example, use such anonymization as part of `SimpleSequentialChain`, as shown below:"
|
||||
"Later, you can, for example, use such anonymization as part of chain sequence. We will use `LangChain Expression Language` ([learn more here](https://python.langchain.com/docs/guides/expression_language/)) for composing these chains together, as shown below:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -399,6 +401,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# ! pip install openai\n",
|
||||
"\n",
|
||||
"# Set env var OPENAI_API_KEY or load from a .env file:\n",
|
||||
"import dotenv\n",
|
||||
"\n",
|
||||
@@ -407,38 +411,43 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'input': 'You can find our super secret data at https://supersecretdata.com',\n",
|
||||
" 'output': ' http://www.hernandez.com/'}"
|
||||
"{'anonymized_text': StringPromptValue(text='According to this text, where can you find our super secret data?\\n\\nYou can find our super secret data at https://evans-summers.info/\\n\\nAnswer:'),\n",
|
||||
" 'text': ' https://evans-summers.info/'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain.chains import SimpleSequentialChain\n",
|
||||
"from operator import itemgetter\n",
|
||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain.chains.llm import LLMChain\n",
|
||||
"from langchain.llms.openai import OpenAI\n",
|
||||
"\n",
|
||||
"template = \"\"\"According to this text, where can you find our super secret data?\n",
|
||||
"\n",
|
||||
"{output_text}\n",
|
||||
"{anonymized_text}\n",
|
||||
"\n",
|
||||
"Answer:\"\"\"\n",
|
||||
"prompt = PromptTemplate(input_variables=[\"output_text\"], template=template)\n",
|
||||
"prompt = PromptTemplate(input_variables=[\"anonymized_text\"], template=template)\n",
|
||||
"llm_chain = LLMChain(llm=OpenAI(), prompt=prompt)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"sequential_chain = SimpleSequentialChain(chains=[anonymize_chain, llm_chain])\n",
|
||||
"sequential_chain(\"You can find our super secret data at https://supersecretdata.com\")"
|
||||
"chain = (\n",
|
||||
" anonymize_chain\n",
|
||||
" | {\"anonymized_text\": itemgetter(\"anonymized_text\")}\n",
|
||||
" | prompt\n",
|
||||
" | llm_chain\n",
|
||||
")\n",
|
||||
"chain.invoke(\"You can find our super secret data at https://supersecretdata.com\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user