diff --git a/docs/extras/expression_language/cookbook.ipynb b/docs/extras/expression_language/cookbook.ipynb index 04b74164dd..c10d0a7672 100644 --- a/docs/extras/expression_language/cookbook.ipynb +++ b/docs/extras/expression_language/cookbook.ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 2, "id": "3c634ef0", "metadata": {}, "outputs": [], @@ -70,7 +70,7 @@ { "data": { "text/plain": [ - "AIMessage(content='Why don\\'t bears use cell phones? \\n\\nBecause they always get terrible \"grizzly\" reception!', additional_kwargs={}, example=False)" + "AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they have bear feet!\", additional_kwargs={}, example=False)" ] }, "execution_count": 5, @@ -117,7 +117,7 @@ { "data": { "text/plain": [ - "AIMessage(content=\"Why don't bears use cell phones?\", additional_kwargs={}, example=False)" + "AIMessage(content=\"Why don't bears wear shoes?\", additional_kwargs={}, example=False)" ] }, "execution_count": 7, @@ -210,7 +210,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "id": "cc194c78", "metadata": {}, "outputs": [], @@ -228,17 +228,17 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "id": "e3d69a18", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\"Why don't bears wear shoes?\\n\\nBecause they have bear feet!\"" + "\"Sure, here's a bear joke for you:\\n\\nWhy don't bears like fast food?\\n\\nBecause they can't catch it!\"" ] }, - "execution_count": 13, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -259,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "id": "ad0dd88e", "metadata": {}, "outputs": [], @@ -274,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "id": "1e7aa8eb", "metadata": {}, "outputs": [ @@ -285,7 +285,7 @@ " 'punchline': 'Because they have bear feet!'}" ] }, - "execution_count": 15, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -296,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "id": "d4aa1a01", "metadata": {}, "outputs": [], @@ -311,17 +311,17 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, "id": "8b6df9ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\"Why don't bears like fast food?\"" + "\"Why don't bears wear shoes?\"" ] }, - "execution_count": 18, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -345,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "id": "5d3d8ffe", "metadata": {}, "outputs": [], @@ -366,7 +366,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 18, "id": "33be32af", "metadata": {}, "outputs": [], @@ -378,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 19, "id": "df3f3fa2", "metadata": {}, "outputs": [], @@ -390,7 +390,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 20, "id": "bfc47ec1", "metadata": {}, "outputs": [], @@ -405,7 +405,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 21, "id": "eae31755", "metadata": {}, "outputs": [], @@ -420,7 +420,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 22, "id": "f3040b0c", "metadata": {}, "outputs": [ @@ -437,7 +437,7 @@ "'Harrison worked at Kensho.'" ] }, - "execution_count": 18, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -448,7 +448,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 23, "id": "e1d20c7c", "metadata": {}, "outputs": [], @@ -471,7 +471,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 24, "id": "7ee8b2d4", "metadata": {}, "outputs": [ @@ -488,7 +488,7 @@ "'Harrison ha lavorato a Kensho.'" ] }, - "execution_count": 20, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -509,7 +509,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 25, "id": "3f30c348", "metadata": {}, "outputs": [], @@ -520,7 +520,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 26, "id": "64ab1dbf", "metadata": {}, "outputs": [], @@ -538,7 +538,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 27, "id": "7d628c97", "metadata": {}, "outputs": [], @@ -553,7 +553,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 28, "id": "f60a5d0f", "metadata": {}, "outputs": [], @@ -566,7 +566,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 29, "id": "7d007db6", "metadata": {}, "outputs": [], @@ -583,7 +583,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 30, "id": "5c32cc89", "metadata": {}, "outputs": [], @@ -605,7 +605,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 31, "id": "135c8205", "metadata": {}, "outputs": [ @@ -622,7 +622,7 @@ "AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False)" ] }, - "execution_count": 17, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -636,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 32, "id": "424e7e7a", "metadata": {}, "outputs": [ @@ -653,7 +653,7 @@ "AIMessage(content='Harrison worked at Kensho.', additional_kwargs={}, example=False)" ] }, - "execution_count": 15, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -677,7 +677,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 33, "id": "e31dd17c", "metadata": {}, "outputs": [], @@ -687,7 +687,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 34, "id": "d4bffe94", "metadata": {}, "outputs": [], @@ -697,7 +697,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 35, "id": "733be985", "metadata": {}, "outputs": [], @@ -744,7 +744,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 36, "id": "806e390c", "metadata": {}, "outputs": [ @@ -762,7 +762,7 @@ " 'docs': [Document(page_content='harrison worked at kensho', metadata={})]}" ] }, - "execution_count": 46, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -775,7 +775,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 37, "id": "977399fd", "metadata": {}, "outputs": [], @@ -788,7 +788,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 38, "id": "f94f7de4", "metadata": {}, "outputs": [ @@ -799,7 +799,7 @@ " AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False)]}" ] }, - "execution_count": 48, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -820,17 +820,17 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 39, "id": "d65d4e9e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'El país en el que nació la ciudad de Honolulu, Hawái, donde nació Barack Obama, el 44º presidente de los Estados Unidos, es Estados Unidos.'" + "'El país en el que se encuentra la ciudad de Honolulu, Hawái, donde nació Barack Obama, el 44º presidente de los Estados Unidos, es Estados Unidos.'" ] }, - "execution_count": 31, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -850,7 +850,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 40, "id": "878f8176", "metadata": {}, "outputs": [], @@ -869,17 +869,17 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 41, "id": "d621a870", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "ChatPromptValue(messages=[HumanMessage(content=\"What is the color of A fruit that has a color similar to #7E7DE6 is the Peruvian Apple Cactus (Cereus repandus). It is a tropical fruit with a vibrant purple or violet exterior. and The country's flag that has the color #7E7DE6 is North Macedonia.\", additional_kwargs={}, example=False)])" + "ChatPromptValue(messages=[HumanMessage(content=\"What is the color of A fruit that is of color #FF4500 is typically an orange fruit. and The country's flag that has the color #FF4500 is the flag of India.\", additional_kwargs={}, example=False)])" ] }, - "execution_count": 33, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -888,6 +888,101 @@ "chain2.invoke({})" ] }, + { + "cell_type": "markdown", + "id": "6d75a313-f1c8-4e94-9a17-24e0bf4a2bdc", + "metadata": {}, + "source": [ + "### Branching and Merging\n", + "\n", + "You may want the output of one component to be processed by 2 or more other components. [RunnableMaps](https://api.python.langchain.com/en/latest/schema/langchain.schema.runnable.base.RunnableMap.html) let you split or fork the chain so multiple components can process the input in parallel. Later, other components can join or merge the results to synthesize a final response. This type of chain creates a computation graph that looks like the following:\n", + "\n", + "```text\n", + " Input\n", + " / \\\n", + " / \\\n", + " Branch1 Branch2\n", + " \\ /\n", + " \\ /\n", + " Combine\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "247fa0bd-4596-4063-8cb3-1d7fc119d982", + "metadata": {}, + "outputs": [], + "source": [ + "planner = (\n", + " ChatPromptTemplate.from_template(\n", + " \"Generate an argument about: {input}\"\n", + " )\n", + " | ChatOpenAI()\n", + " | StrOutputParser()\n", + " | {\"base_response\": RunnablePassthrough()}\n", + ")\n", + "\n", + "arguments_for = (\n", + " ChatPromptTemplate.from_template(\n", + " \"List the pros or positive aspects of {base_response}\"\n", + " )\n", + " | ChatOpenAI()\n", + " | StrOutputParser()\n", + ")\n", + "arguments_against = (\n", + " ChatPromptTemplate.from_template(\n", + " \"List the cons or negative aspects of {base_response}\"\n", + " )\n", + " | ChatOpenAI()\n", + " | StrOutputParser()\n", + ")\n", + "\n", + "final_responder = (\n", + " ChatPromptTemplate.from_messages(\n", + " [\n", + " (\"ai\", \"{original_response}\"),\n", + " (\"human\", \"Pros:\\n{results_1}\\n\\nCons:\\n{results_2}\"),\n", + " (\"system\", \"Generate a final response given the critique\"),\n", + " ]\n", + " )\n", + " | ChatOpenAI()\n", + " | StrOutputParser()\n", + ")\n", + "\n", + "chain = (\n", + " planner \n", + " | {\n", + " \"results_1\": arguments_for,\n", + " \"results_2\": arguments_against,\n", + " \"original_response\": itemgetter(\"base_response\"),\n", + " }\n", + " | final_responder\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "2564f310-0674-4bb1-9c4e-d7848ca73511", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"While Scrum has its limitations and potential drawbacks, it is important to note that these can be mitigated with proper understanding, implementation, and adaptation. Here are some ways to address the critique:\\n\\n1. Lack of structure: While Scrum promotes self-organization, it is essential to provide clear guidelines, define roles and responsibilities, and establish a shared understanding of the project's goals and expectations. This can be achieved through effective communication and regular alignment meetings.\\n\\n2. Time and resource constraints: Proper planning, prioritization, and resource allocation are crucial in managing the sprint cycles effectively. Teams can leverage tools and techniques such as backlog refinement, sprint planning, and capacity planning to ensure that workloads are manageable and realistic.\\n\\n3. Managing large teams: Scaling frameworks like Scrum of Scrums or LeSS (Large-Scale Scrum) can be implemented to coordinate the efforts of multiple Scrum teams. These frameworks provide mechanisms for communication, synchronization, and alignment across teams.\\n\\n4. Limited documentation: Although Scrum emphasizes working software over comprehensive documentation, it is important to strike a balance. Teams can adopt lightweight documentation practices such as user stories, acceptance criteria, and sprint reviews to capture relevant information and promote knowledge transfer.\\n\\n5. Resolving conflicts and fostering collaboration: Conflict resolution techniques and team-building activities can help address conflicts and foster a collaborative environment. Encouraging open and honest communication, promoting a culture of trust and respect, and providing opportunities for team members to share ideas and perspectives can contribute to better team dynamics.\\n\\n6. Long-term planning: While Scrum focuses on short-term goals, it is still important to have a long-term vision and roadmap. Teams can incorporate longer-term planning activities, such as release planning or product roadmapping, to align the project with broader strategic objectives and ensure a balance between adaptability and long-term goals.\\n\\n7. Skilled Scrum Master: Investing in the training and development of a skilled Scrum Master is crucial. Organizations can provide training and support for Scrum Masters to enhance their understanding of Scrum principles, facilitation skills, and ability to address challenges effectively.\\n\\n8. Scope management: To prevent scope creep, teams should establish a well-defined product backlog and prioritize requirements based on value and feasibility. Regular backlog refinement and stakeholder engagement can help ensure that changes are evaluated and incorporated in a controlled manner.\\n\\n9. Applicability to different domains: While Scrum originated in software development, it has been successfully applied in various industries and domains. Organizations can tailor Scrum practices to suit their specific needs, making necessary adaptations and incorporating domain-specific practices as required.\\n\\nBy addressing these concerns and adapting Scrum to the specific context, organizations can maximize the benefits of Scrum while mitigating potential drawbacks. It is important to continuously evaluate and improve the implementation to ensure the best outcomes for the project and the team.\"" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.invoke({\"input\": \"scrum\"})" + ] + }, { "cell_type": "markdown", "id": "d094d637", @@ -900,7 +995,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 66, "id": "252625fd", "metadata": {}, "outputs": [], @@ -916,7 +1011,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 67, "id": "57886e84", "metadata": {}, "outputs": [], @@ -926,7 +1021,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 68, "id": "a303b089", "metadata": {}, "outputs": [], @@ -937,7 +1032,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 69, "id": "7aa9ea06", "metadata": {}, "outputs": [], @@ -948,7 +1043,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 70, "id": "6a3d3f5d", "metadata": {}, "outputs": [], @@ -961,17 +1056,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 71, "id": "8aeda930", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='Thank you for the compliment! The sum of 2 + 2 is equal to 4.', additional_kwargs={}, example=False)" + "AIMessage(content='Thank you for the compliment! The sum of 2 and 2 is 4.', additional_kwargs={}, example=False)" ] }, - "execution_count": 9, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -987,31 +1082,22 @@ "source": [ "## Tools\n", "\n", - "You can use any LangChain tool easily" + "You can use any LangChain tool easily." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 72, "id": "9232d2a9", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.14) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n", - " warnings.warn(\n" - ] - } - ], + "outputs": [], "source": [ "from langchain.tools import DuckDuckGoSearchRun" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 73, "id": "a0c64d2c", "metadata": {}, "outputs": [], @@ -1021,7 +1107,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 74, "id": "391969b6", "metadata": {}, "outputs": [], @@ -1034,7 +1120,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 75, "id": "e3d9d20d", "metadata": {}, "outputs": [], @@ -1044,17 +1130,17 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 76, "id": "55f2967d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\"What sports games are on TV today & tonight? Watch and stream live sports on TV today, tonight, tomorrow. Today's 2023 sports TV schedule includes football, basketball, baseball, hockey, motorsports, soccer and more. Watch on TV or stream online on ESPN, FOX, FS1, CBS, NBC, ABC, Peacock, Paramount+, fuboTV, local channels and many other networks. Weather Alerts Alerts Bar. Not all offers available in all states, please visit BetMGM for the latest promotions for your area. Must be 21+ to gamble, please wager responsibly. If you or someone ... Speak of the Devils. Good Morning Arizona. Happy Hour Spots. Jaime's Local Love. Surprise Squad. Silver Apple. Field Trip Friday. Seen on TV. Arizona Highways TV. MLB Games Tonight: How to Watch on TV, Streaming & Odds - Friday, July 28. San Diego Padres' Juan Soto plays during the first baseball game in a doubleheader, Saturday, July 15, 2023, in Philadelphia. (AP Photo/Matt Slocum) (APMedia) Today's MLB schedule features top teams in action. Among those games is the Texas Rangers playing the San Diego ... TV. Cleveland at Chi. White Sox. 1:10pm. Bally Sports. NBCS-CHI. Cleveland Guardians (50-51) are second place in AL Central and Chicago White Sox (41-61) are fourth place in AL Central. The Guardians are 23-27 on the road this season and White Sox are 21-26 at home. Chi. Cubs at St. Louis.\"" + "\"What sports games are on TV today & tonight? Watch and stream live sports on TV today, tonight, tomorrow. Today's 2023 sports TV schedule includes football, basketball, baseball, hockey, motorsports, soccer and more. Watch on TV or stream online on ESPN, FOX, FS1, CBS, NBC, ABC, Peacock, Paramount+, fuboTV, local channels and many other networks. MLB Games Tonight: How to Watch on TV, Streaming & Odds - Wednesday, September 6. Texas Rangers second baseman Marcus Semien, left, tags out Houston Astros' Jose Altuve (27) who was attempting to stretch out a single in the seventh inning of a baseball game, Monday, Sept. 4, 2023, in Arlington, Texas. (AP Photo/Tony Gutierrez) (APMedia) There ... MLB Games Tonight: How to Watch on TV, Streaming & Odds - Sunday, September 3. Los Angeles Dodgers right fielder Mookie Betts, left, gives a thumbs up to Vanessa Bryant, right, widow of Kobe ... WEEK 16 NFL TV SCHEDULE. NFL Games Thursday, 12/21/23. TIME ET. TV. New Orleans at LA Rams. 8:15pm. AMZN. NFL Games Saturday, 12/23/23. TIME ET. The second half of tonight's college football schedule still has some good games remaining to watch on your television.. We've already seen an exciting one when Colorado upset TCU. And we saw some ...\"" ] }, - "execution_count": 10, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -1077,7 +1163,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 77, "id": "6bb221b3", "metadata": {}, "outputs": [], @@ -1105,17 +1191,17 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 78, "id": "5488ec85", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='3 + 9 is equal to 12.', additional_kwargs={}, example=False)" + "AIMessage(content='3 + 9 equals 12.', additional_kwargs={}, example=False)" ] }, - "execution_count": 36, + "execution_count": 78, "metadata": {}, "output_type": "execute_result" } @@ -1124,6 +1210,78 @@ "chain.invoke({\"foo\": \"bar\", \"bar\": \"gah\"})" ] }, + { + "cell_type": "markdown", + "id": "4728ddd9-914d-42ce-ae9b-72c9ce8ec940", + "metadata": {}, + "source": [ + "## Accepting a Runnable Config\n", + "\n", + "Runnable lambdas can optionally accept a [RunnableConfig](https://api.python.langchain.com/en/latest/schema/langchain.schema.runnable.config.RunnableConfig.html?highlight=runnableconfig#langchain.schema.runnable.config.RunnableConfig), which they can use to pass callbacks, tags, and other configuration information to nested runs." + ] + }, + { + "cell_type": "code", + "execution_count": 139, + "id": "80b3b5f6-5d58-44b9-807e-cce9a46bf49f", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.schema.runnable import RunnableConfig" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "id": "ff0daf0c-49dd-4d21-9772-e5fa133c5f36", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "def parse_or_fix(text: str, config: RunnableConfig):\n", + " fixing_chain = (\n", + " ChatPromptTemplate.from_template(\n", + " \"Fix the following text:\\n\\n```text\\n{input}\\n```\\nError: {error}\"\n", + " \" Don't narrate, just respond with the fixed data.\"\n", + " )\n", + " | ChatOpenAI()\n", + " | StrOutputParser()\n", + " )\n", + " for _ in range(3):\n", + " try:\n", + " return json.loads(text)\n", + " except Exception as e:\n", + " text = fixing_chain.invoke({\"input\": text, \"error\": e}, config)\n", + " return \"Failed to parse\"" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "id": "1a5e709e-9d75-48c7-bb9c-503251990505", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tokens Used: 65\n", + "\tPrompt Tokens: 56\n", + "\tCompletion Tokens: 9\n", + "Successful Requests: 1\n", + "Total Cost (USD): $0.00010200000000000001\n" + ] + } + ], + "source": [ + "from langchain.callbacks import get_openai_callback\n", + "\n", + "with get_openai_callback() as cb:\n", + " RunnableLambda(parse_or_fix).invoke(\"{foo: bar}\", {\"tags\": [\"my-tag\"], \"callbacks\": [cb]})\n", + " print(cb)" + ] + }, { "cell_type": "markdown", "id": "506e9636", @@ -1136,7 +1294,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 106, "id": "7a927516", "metadata": {}, "outputs": [], @@ -1144,13 +1302,14 @@ "template = \"\"\"Based on the table schema below, write a SQL query that would answer the user's question:\n", "{schema}\n", "\n", - "Question: {question}\"\"\"\n", + "Question: {question}\n", + "SQL Query:\"\"\"\n", "prompt = ChatPromptTemplate.from_template(template)" ] }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 107, "id": "3f51f386", "metadata": {}, "outputs": [], @@ -1160,7 +1319,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 111, "id": "2ccca6fc", "metadata": {}, "outputs": [], @@ -1170,7 +1329,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 109, "id": "05ba88ee", "metadata": {}, "outputs": [], @@ -1181,7 +1340,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 112, "id": "a4eda902", "metadata": {}, "outputs": [], @@ -1192,7 +1351,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 113, "id": "5046cb17", "metadata": {}, "outputs": [], @@ -1211,17 +1370,17 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 114, "id": "a5552039", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'SELECT COUNT(*) \\nFROM Employee;'" + "'SELECT COUNT(EmployeeId) FROM Employee'" ] }, - "execution_count": 48, + "execution_count": 114, "metadata": {}, "output_type": "execute_result" } @@ -1232,7 +1391,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 115, "id": "d6fee130", "metadata": {}, "outputs": [], @@ -1248,7 +1407,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 116, "id": "923aa634", "metadata": {}, "outputs": [], @@ -1271,7 +1430,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 117, "id": "e94963d8", "metadata": {}, "outputs": [ @@ -1281,7 +1440,7 @@ "AIMessage(content='There are 8 employees.', additional_kwargs={}, example=False)" ] }, - "execution_count": 53, + "execution_count": 117, "metadata": {}, "output_type": "execute_result" } @@ -1300,7 +1459,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 118, "id": "bd7c259a", "metadata": {}, "outputs": [], @@ -1311,7 +1470,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 119, "id": "73795d2d", "metadata": {}, "outputs": [], @@ -1331,7 +1490,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 120, "id": "42859e8a", "metadata": {}, "outputs": [], @@ -1343,7 +1502,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 121, "id": "5ded1a86", "metadata": {}, "outputs": [], @@ -1353,7 +1512,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 122, "id": "208c2b75", "metadata": {}, "outputs": [ @@ -1370,7 +1529,7 @@ "'4\\n'" ] }, - "execution_count": 68, + "execution_count": 122, "metadata": {}, "output_type": "execute_result" } @@ -1391,7 +1550,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 123, "id": "7998efd8", "metadata": {}, "outputs": [], @@ -1409,7 +1568,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 124, "id": "fa0087f3", "metadata": {}, "outputs": [], @@ -1419,7 +1578,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 125, "id": "06b531ae", "metadata": {}, "outputs": [ @@ -1429,7 +1588,7 @@ "{'history': []}" ] }, - "execution_count": 101, + "execution_count": 125, "metadata": {}, "output_type": "execute_result" } @@ -1440,7 +1599,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 126, "id": "d9437af6", "metadata": {}, "outputs": [], @@ -1456,7 +1615,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 127, "id": "bed1e260", "metadata": {}, "outputs": [ @@ -1466,7 +1625,7 @@ "AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)" ] }, - "execution_count": 103, + "execution_count": 127, "metadata": {}, "output_type": "execute_result" } @@ -1479,7 +1638,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 128, "id": "890475b4", "metadata": {}, "outputs": [], @@ -1489,7 +1648,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 129, "id": "e8fcb77f", "metadata": {}, "outputs": [ @@ -1500,7 +1659,7 @@ " AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)]}" ] }, - "execution_count": 105, + "execution_count": 129, "metadata": {}, "output_type": "execute_result" } @@ -1511,17 +1670,17 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 130, "id": "d837d5c3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='Your name is Bob. You mentioned it in your previous message. Is there anything else I can help you with, Bob?', additional_kwargs={}, example=False)" + "AIMessage(content='Your name is Bob.', additional_kwargs={}, example=False)" ] }, - "execution_count": 106, + "execution_count": 130, "metadata": {}, "output_type": "execute_result" } @@ -1544,7 +1703,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 131, "id": "4f5f6449-940a-4f5c-97c0-39b71c3e2a68", "metadata": {}, "outputs": [], @@ -1555,7 +1714,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 132, "id": "fcb8312b-7e7a-424f-a3ec-76738c9a9d21", "metadata": {}, "outputs": [], @@ -1565,7 +1724,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 133, "id": "b24b9148-f6b0-4091-8ea8-d3fb281bd950", "metadata": {}, "outputs": [], @@ -1578,7 +1737,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 134, "id": "1c8ed87c-9ca6-4559-bf60-d40e94a0af08", "metadata": {}, "outputs": [], @@ -1588,7 +1747,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 135, "id": "5256b9bd-381a-42b0-bfa8-7e6d18f853cb", "metadata": {}, "outputs": [ @@ -1598,7 +1757,7 @@ "'\\n\\nYou are stupid.'" ] }, - "execution_count": 34, + "execution_count": 135, "metadata": {}, "output_type": "execute_result" } @@ -1609,7 +1768,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 136, "id": "fe6e3b33-dc9a-49d5-b194-ba750c58a628", "metadata": {}, "outputs": [], @@ -1619,7 +1778,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 137, "id": "d8ba0cbd-c739-4d23-be9f-6ae092bd5ffb", "metadata": {}, "outputs": [ @@ -1630,7 +1789,7 @@ " 'output': \"Text was found that violates OpenAI's content policy.\"}" ] }, - "execution_count": 37, + "execution_count": 137, "metadata": {}, "output_type": "execute_result" } @@ -1638,6 +1797,14 @@ "source": [ "moderated_chain.invoke({\"input\": \"you are stupid\"})" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f07b5300-8676-48ee-ab77-3f2dc2ecd415", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -1656,7 +1823,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.1" + "version": "3.11.2" } }, "nbformat": 4,