You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/libs/partners/azure-dynamic-sessions/docs/provider.ipynb

170 lines
5.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Azure Container Apps dynamic sessions\n",
"\n",
"Azure Container Apps dynamic sessions provides a secure and scalable way to run a Python code interpreter in Hyper-V isolated sandboxes. This allows your agents to run potentially untrusted code in a secure environment. The code interpreter environment includes many popular Python packages, such as NumPy, pandas, and scikit-learn.\n",
"\n",
"## Pre-requisites\n",
"\n",
"By default, the `SessionsPythonREPLTool` tool uses `DefaultAzureCredential` to authenticate with Azure. Locally, it'll use your credentials from the Azure CLI or VS Code. Install the Azure CLI and log in with `az login` to authenticate.\n",
"\n",
"## Using the tool\n",
"\n",
"Set variables:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import dotenv\n",
"dotenv.load_dotenv()\n",
"\n",
"POOL_MANAGEMENT_ENDPOINT = os.getenv(\"POOL_MANAGEMENT_ENDPOINT\")\n",
"AZURE_OPENAI_ENDPOINT = os.getenv(\"AZURE_OPENAI_ENDPOINT\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\\n \"result\": 42,\\n \"stdout\": \"\",\\n \"stderr\": \"\"\\n}'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_azure_dynamic_sessions import SessionsPythonREPLTool\n",
"\n",
"\n",
"tool = SessionsPythonREPLTool(pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT)\n",
"tool.run(\"6 * 7\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Full agent example"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mI need to calculate the compound interest on the initial amount over 6 years.\n",
"Action: Python_REPL\n",
"Action Input: \n",
"```python\n",
"initial_amount = 500\n",
"interest_rate = 0.05\n",
"time_period = 6\n",
"final_amount = initial_amount * (1 + interest_rate)**time_period\n",
"final_amount\n",
"```\u001b[0m\u001b[36;1m\u001b[1;3m{\n",
" \"result\": 670.0478203125002,\n",
" \"stdout\": \"\",\n",
" \"stderr\": \"\"\n",
"}\u001b[0m\u001b[32;1m\u001b[1;3mThe final amount after 6 years will be $670.05\n",
"Final Answer: $670.05\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'input': 'If I put $500 in a bank account with a 5% interest rate, how much money will I have in the account after 6 years?',\n",
" 'output': '$670.05'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from azure.identity import DefaultAzureCredential\n",
"from langchain_azure_dynamic_sessions import SessionsPythonREPLTool\n",
"from langchain_openai import AzureChatOpenAI\n",
"from langchain import agents, hub\n",
"\n",
"\n",
"credential = DefaultAzureCredential()\n",
"os.environ[\"OPENAI_API_TYPE\"] = \"azure_ad\"\n",
"os.environ[\"OPENAI_API_KEY\"] = credential.get_token(\"https://cognitiveservices.azure.com/.default\").token\n",
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = AZURE_OPENAI_ENDPOINT\n",
"\n",
"llm = AzureChatOpenAI(\n",
" azure_deployment=\"gpt-35-turbo\",\n",
" openai_api_version=\"2023-09-15-preview\",\n",
" streaming=True,\n",
" temperature=0,\n",
")\n",
"\n",
"repl = SessionsPythonREPLTool(\n",
" pool_management_endpoint=POOL_MANAGEMENT_ENDPOINT,\n",
")\n",
"\n",
"tools = [repl]\n",
"react_agent = agents.create_react_agent(\n",
" llm=llm,\n",
" tools=tools,\n",
" prompt=hub.pull(\"hwchase17/react\"),\n",
")\n",
"\n",
"react_agent_executor = agents.AgentExecutor(agent=react_agent, tools=tools, verbose=True, handle_parsing_errors=True)\n",
"\n",
"react_agent_executor.invoke({\"input\": \"If I put $500 in a bank account with a 5% interest rate, how much money will I have in the account after 6 years?\"})"
]
}
],
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}