{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bce7335e-f3b2-44f3-90cc-8c0a23a89a21", "metadata": {}, "outputs": [], "source": [ "import os\n", "from langchain.agents import load_tools\n", "from langchain.agents import initialize_agent\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.utilities import GoogleSearchAPIWrapper\n", "from langchain.schema import (\n", " SystemMessage,\n", " HumanMessage,\n", " AIMessage\n", ")\n", "\n", "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", "# os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", "# os.environ[\"LANGCHAIN_API_KEY\"] = \"******\"\n", "# os.environ[\"LANGCHAIN_PROJECT\"] = \"Jarvis\"\n", "\n", "\n", "prefix_messages = [{\"role\": \"system\", \"content\": \"You are a helpful discord Chatbot.\"}]\n", "\n", "llm = ChatOpenAI(model_name='gpt-3.5-turbo', \n", " temperature=0.5, \n", " max_tokens = 2000)\n", "tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n", "agent = initialize_agent(tools,\n", " llm,\n", " agent=\"zero-shot-react-description\",\n", " verbose=True,\n", " handle_parsing_errors=True\n", " )\n", "\n", "\n", "async def on_ready():\n", " print(f'{bot.user} has connected to Discord!')\n", "\n", "async def on_message(message):\n", "\n", " print(\"Detected bot name in message:\", message.content)\n", "\n", " # Capture the output of agent.run() in the response variable\n", " response = agent.run(message.content)\n", "\n", " while response:\n", " print(response)\n", " chunk, response = response[:2000], response[2000:]\n", " print(f\"Chunk: {chunk}\")\n", " print(\"Response sent.\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "1551ce9f-b6de-4035-b6d6-825722823b48", "metadata": { "tags": [] }, "outputs": [], "source": [ "from dataclasses import dataclass\n", "@dataclass\n", "class Message:\n", " content: str" ] }, { "cell_type": "code", "execution_count": 23, "id": "6e6859ec-8544-4407-9663-6b53c0092903", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Detected bot name in message: Hi AI, how are you today?\n", "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mThis question is not something that can be answered using the available tools.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3mI need to follow the correct format for answering questions.\n", "Action: N/A\u001b[0m\n", "Observation: Invalid Format: Missing 'Action Input:' after 'Action:'\n", "Thought:\u001b[32;1m\u001b[1;3m\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "Agent stopped due to iteration limit or time limit.\n", "Chunk: Agent stopped due to iteration limit or time limit.\n", "Response sent.\n" ] } ], "source": [ "await on_message(Message(content=\"Hi AI, how are you today?\"))" ] }, { "cell_type": "code", "execution_count": 24, "id": "b850294c-7f8f-4e79-adcf-47e4e3a898df", "metadata": { "tags": [] }, "outputs": [], "source": [ "from langsmith import Client\n", "\n", "client = Client()" ] }, { "cell_type": "code", "execution_count": 25, "id": "6d089ddc-69bc-45a8-b8db-9962e4f1f5ee", "metadata": { "tags": [] }, "outputs": [], "source": [ "from itertools import islice\n", "\n", "runs = list(islice(client.list_runs(), 10))" ] }, { "cell_type": "code", "execution_count": 38, "id": "f0349fac-5a98-400f-ba03-61ed4e1332be", "metadata": { "tags": [] }, "outputs": [], "source": [ "runs = sorted(runs, key=lambda x: x.start_time, reverse=True)" ] }, { "cell_type": "code", "execution_count": 26, "id": "02f133f0-39ee-4b46-b443-12c1f9b76fff", "metadata": { "tags": [] }, "outputs": [], "source": [ "ids = [run.id for run in runs]" ] }, { "cell_type": "code", "execution_count": 39, "id": "3366dce4-0c38-4a7d-8111-046a58b24917", "metadata": { "tags": [] }, "outputs": [], "source": [ "runs2 = list(client.list_runs(id=ids))\n", "runs2 = sorted(runs2, key=lambda x: x.start_time, reverse=True)" ] }, { "cell_type": "code", "execution_count": 42, "id": "82915b90-39a0-47d6-9121-56a13f210f52", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['a36092d2-4ad5-4fb4-9b0d-0dba9a2ed836',\n", " '9398e6be-964f-4aa4-8de9-ad78cd4b7074']" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[str(x) for x in ids[:2]]" ] }, { "cell_type": "code", "execution_count": 48, "id": "f610ec91-dc48-4a17-91c5-5c4675c77abc", "metadata": { "tags": [] }, "outputs": [], "source": [ "from langsmith.run_helpers import traceable\n", "\n", "@traceable(run_type=\"llm\", name=\"\"\"\"\"\")\n", "def foo():\n", " return \"bar\"" ] }, { "cell_type": "code", "execution_count": 49, "id": "bd317bd7-8b2a-433a-8ec3-098a84ba8e64", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'bar'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo()" ] }, { "cell_type": "code", "execution_count": 52, "id": "b142519b-6885-415c-83b9-4a346fb90589", "metadata": { "tags": [] }, "outputs": [], "source": [ "from langchain.llms import AzureOpenAI" ] }, { "cell_type": "code", "execution_count": null, "id": "5c50bb2b-72b8-4322-9b16-d857ecd9f347", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.2" } }, "nbformat": 4, "nbformat_minor": 5 }