diff --git a/docs/extras/modules/chains/additional/llm_symbolic_math.ipynb b/docs/extras/modules/chains/additional/llm_symbolic_math.ipynb index 4aca644c37b..6b2925a678e 100644 --- a/docs/extras/modules/chains/additional/llm_symbolic_math.ipynb +++ b/docs/extras/modules/chains/additional/llm_symbolic_math.ipynb @@ -5,14 +5,47 @@ "metadata": {}, "source": [ "# LLM Symbolic Math \n", - "This notebook showcases using LLMs and Python to Solve Algebraic Equations." + "This notebook showcases using LLMs and Python to Solve Algebraic Equations. Under the hood is makes use of [SymPy](https://www.sympy.org/en/index.html)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.llms import OpenAI\n", + "from langchain.chains.llm_symbolic_math.base import LLMSymbolicMathChain\n", + "\n", + "llm = OpenAI(temperature=0)\n", + "llm_symbolic_math = LLMSymbolicMathChain.from_llm(llm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Calculating the limit of an equation" + "## Integrals and derivates" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Answer: exp(x)*sin(x) + exp(x)*cos(x)'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "llm_symbolic_math.run(\"What is the derivative of sin(x)*exp(x) with respect to x?\")" ] }, { @@ -20,21 +53,10 @@ "execution_count": 18, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new LLMSymbolicMathChain chain...\u001b[0m\n", - "What is the limit of sin(x) / x as x goes to 0?\u001b[32;1m\u001b[1;3mAnswer: 1\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, { "data": { "text/plain": [ - "'Answer: 1'" + "'Answer: exp(x)*sin(x)'" ] }, "execution_count": 18, @@ -43,20 +65,16 @@ } ], "source": [ - "from langchain.llms import OpenAI\n", - "from langchain.chains.llm_symbolic_math.base import LLMSymbolicMathChain\n", - "\n", - "llm = OpenAI(temperature=0)\n", - "llm_symbolic_math = LLMSymbolicMathChain.from_llm(llm, verbose=True)\n", - "\n", - "llm_symbolic_math.run(\"What is the limit of sin(x) / x as x goes to 0?\")" + "llm_symbolic_math.run(\n", + " \"What is the integral of exp(x)*sin(x) + exp(x)*cos(x) with respect to x?\"\n", + ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Calculating an integral" + "## Solve linear and differential equations" ] }, { @@ -64,21 +82,10 @@ "execution_count": 19, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new LLMSymbolicMathChain chain...\u001b[0m\n", - "What is the integral of e^-x from 0 to infinity?\u001b[32;1m\u001b[1;3mAnswer: 1\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, { "data": { "text/plain": [ - "'Answer: 1'" + "'Answer: Eq(y(t), C2*exp(-t) + (C1 + t/2)*exp(t))'" ] }, "execution_count": 19, @@ -87,60 +94,55 @@ } ], "source": [ - "llm_symbolic_math.run(\"What is the integral of e^-x from 0 to infinity?\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Calculating an algebraic equation" + "llm_symbolic_math.run('Solve the differential equation y\" - y = e^t')" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new LLMSymbolicMathChain chain...\u001b[0m\n", - "What are the solutions to this equation x**2 - x?\u001b[32;1m\u001b[1;3mAnswer: 0 and 1.\u001b[0m\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, { "data": { "text/plain": [ - "'Answer: 0 and 1.'" + "'Answer: {0, -sqrt(3)*I/3, sqrt(3)*I/3}'" ] }, - "execution_count": 20, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "llm_symbolic_math.run(\"What are the solutions to this equation x**2 - x?\")" + "llm_symbolic_math.run(\"What are the solutions to this equation y^3 + 1/3y?\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "'Answer: (3 - sqrt(7), -sqrt(7) - 2, 1 - sqrt(7)), (sqrt(7) + 3, -2 + sqrt(7), 1 + sqrt(7))'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "llm_symbolic_math.run(\"x = y + 5, y = z - 3, z = x * y. Solve for x, y, z\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "venv", "language": "python", - "name": "python3" + "name": "venv" }, "language_info": { "codemirror_mode": { @@ -152,7 +154,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.3" } }, "nbformat": 4,