2023-07-12 19:17:32 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LLM Symbolic Math \n",
2023-07-14 15:49:09 +00:00
"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",
2023-10-10 17:11:46 +00:00
"execution_count": 3,
2023-07-14 15:49:09 +00:00
"metadata": {},
"outputs": [],
"source": [
2023-10-10 17:11:46 +00:00
"from langchain_experimental.llm_symbolic_math.base import LLMSymbolicMathChain\n",
2024-01-06 23:54:48 +00:00
"from langchain_openai import OpenAI\n",
2023-07-14 15:49:09 +00:00
"\n",
"llm = OpenAI(temperature=0)\n",
"llm_symbolic_math = LLMSymbolicMathChain.from_llm(llm)"
2023-07-12 19:17:32 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-07-14 15:49:09 +00:00
"## Integrals and derivates"
2023-07-12 19:17:32 +00:00
]
},
{
"cell_type": "code",
2023-10-10 17:11:46 +00:00
"execution_count": 4,
2023-07-12 19:17:32 +00:00
"metadata": {},
"outputs": [
{
2023-07-14 15:49:09 +00:00
"data": {
"text/plain": [
"'Answer: exp(x)*sin(x) + exp(x)*cos(x)'"
]
},
2023-10-10 17:11:46 +00:00
"execution_count": 4,
2023-07-14 15:49:09 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-03-28 20:08:22 +00:00
"llm_symbolic_math.invoke(\"What is the derivative of sin(x)*exp(x) with respect to x?\")"
2023-07-14 15:49:09 +00:00
]
},
{
"cell_type": "code",
2023-10-10 17:11:46 +00:00
"execution_count": 5,
2023-07-14 15:49:09 +00:00
"metadata": {},
"outputs": [
2023-07-12 19:17:32 +00:00
{
"data": {
"text/plain": [
2023-07-14 15:49:09 +00:00
"'Answer: exp(x)*sin(x)'"
2023-07-12 19:17:32 +00:00
]
},
2023-10-10 17:11:46 +00:00
"execution_count": 5,
2023-07-12 19:17:32 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-03-28 20:08:22 +00:00
"llm_symbolic_math.invoke(\n",
2023-07-14 15:49:09 +00:00
" \"What is the integral of exp(x)*sin(x) + exp(x)*cos(x) with respect to x?\"\n",
")"
2023-07-12 19:17:32 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-07-14 15:49:09 +00:00
"## Solve linear and differential equations"
2023-07-12 19:17:32 +00:00
]
},
{
"cell_type": "code",
2023-10-10 17:11:46 +00:00
"execution_count": 6,
2023-07-12 19:17:32 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2023-07-14 15:49:09 +00:00
"'Answer: Eq(y(t), C2*exp(-t) + (C1 + t/2)*exp(t))'"
2023-07-12 19:17:32 +00:00
]
},
2023-10-10 17:11:46 +00:00
"execution_count": 6,
2023-07-12 19:17:32 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-03-28 20:08:22 +00:00
"llm_symbolic_math.invoke('Solve the differential equation y\" - y = e^t')"
2023-07-12 19:17:32 +00:00
]
},
{
2023-07-14 15:49:09 +00:00
"cell_type": "code",
2023-10-10 17:11:46 +00:00
"execution_count": 7,
2023-07-12 19:17:32 +00:00
"metadata": {},
2023-07-14 15:49:09 +00:00
"outputs": [
{
"data": {
"text/plain": [
"'Answer: {0, -sqrt(3)*I/3, sqrt(3)*I/3}'"
]
},
2023-10-10 17:11:46 +00:00
"execution_count": 7,
2023-07-14 15:49:09 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
2023-07-12 19:17:32 +00:00
"source": [
2024-03-28 20:08:22 +00:00
"llm_symbolic_math.invoke(\"What are the solutions to this equation y^3 + 1/3y?\")"
2023-07-12 19:17:32 +00:00
]
},
{
"cell_type": "code",
2023-10-10 17:11:46 +00:00
"execution_count": 8,
2023-07-12 19:17:32 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2023-07-14 15:49:09 +00:00
"'Answer: (3 - sqrt(7), -sqrt(7) - 2, 1 - sqrt(7)), (sqrt(7) + 3, -2 + sqrt(7), 1 + sqrt(7))'"
2023-07-12 19:17:32 +00:00
]
},
2023-10-10 17:11:46 +00:00
"execution_count": 8,
2023-07-12 19:17:32 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-03-28 20:08:22 +00:00
"llm_symbolic_math.invoke(\"x = y + 5, y = z - 3, z = x * y. Solve for x, y, z\")"
2023-07-12 19:17:32 +00:00
]
}
],
"metadata": {
"kernelspec": {
2023-10-10 17:11:46 +00:00
"display_name": "Python 3 (ipykernel)",
2023-07-12 19:17:32 +00:00
"language": "python",
2023-10-10 17:11:46 +00:00
"name": "python3"
2023-07-12 19:17:32 +00:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-10-10 17:11:46 +00:00
"version": "3.11.4"
2023-07-12 19:17:32 +00:00
}
},
"nbformat": 4,
2023-10-10 17:11:46 +00:00
"nbformat_minor": 4
2023-07-12 19:17:32 +00:00
}