{ "cells": [ { "cell_type": "markdown", "id": "b0023ae4-d8b4-40a7-b1f4-49e2e2447fbe", "metadata": {}, "source": [ "

GroQ

\n", "\n", "Groq is a lightning fast inference api for large language models. We here intend to explore some of its capabilities and gauge the performance of its various hosted models on different tasks." ] }, { "cell_type": "markdown", "id": "212a627f-57b7-4dd0-8fa7-375e414050f9", "metadata": {}, "source": [ "[

GroQ + Langchain

](https://python.langchain.com/docs/integrations/chat/groq/)\n", "\n", "It is often useful to combine such an API with an LLM framework that allows us to perform advanced operations. Langchain is one of the more ubiquitous ones so we will turn to that." ] }, { "cell_type": "code", "execution_count": 6, "id": "385f99dd-d702-497d-8f86-04e25ec8da64", "metadata": {}, "outputs": [], "source": [ "import os\n", "groq_api_key = os.getenv(\"GROQ_API_KEY\")\n", "if groq_api_key is None:\n", " raise ValueError(\"Groq API key environment variable not set\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "30721e40-e728-44ab-b94c-1f328881199e", "metadata": {}, "outputs": [], "source": [ "from langchain_groq import ChatGroq" ] }, { "cell_type": "code", "execution_count": 3, "id": "20b24c21-019b-498c-aef7-fbc39cd959e2", "metadata": {}, "outputs": [], "source": [ "llm = ChatGroq(\n", " model=\"llama3-8b-8192\",\n", " temperature=0,\n", " max_tokens=None,\n", " timeout=None,\n", " max_retries=2,\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "id": "ae47d02f-d7f4-49e0-9d1a-a301116dfb70", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content='Je adore le programmation.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 35, 'total_tokens': 42, 'completion_time': 0.005833333, 'prompt_time': 0.004470233, 'queue_time': 0.009797347000000001, 'total_time': 0.010303566}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_6a6771ae9c', 'finish_reason': 'stop', 'logprobs': None}, id='run-d6fc7095-9f72-405a-ad18-3148da303a81-0', usage_metadata={'input_tokens': 35, 'output_tokens': 7, 'total_tokens': 42})" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "messages = [\n", " (\n", " \"system\",\n", " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", " ),\n", " (\"human\", \"I love programming.\"),\n", "]\n", "ai_msg = llm.invoke(messages)\n", "ai_msg" ] }, { "cell_type": "code", "execution_count": 5, "id": "724f99f7-abff-4a4c-9464-8d8df62840f8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content='Ich liebe Programmierung!\\n\\nTranslation: I love programming.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 12, 'prompt_tokens': 30, 'total_tokens': 42, 'completion_time': 0.01, 'prompt_time': 0.001169453, 'queue_time': 0.012058078, 'total_time': 0.011169453}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_179b0f92c9', 'finish_reason': 'stop', 'logprobs': None}, id='run-8f6b3c1a-e9d1-41de-b5c4-2659214b9bd5-0', usage_metadata={'input_tokens': 30, 'output_tokens': 12, 'total_tokens': 42})" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain_core.prompts import ChatPromptTemplate\n", "\n", "prompt = ChatPromptTemplate.from_messages(\n", " [\n", " (\n", " \"system\",\n", " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", " ),\n", " (\"human\", \"{input}\"),\n", " ]\n", ")\n", "\n", "chain = prompt | llm\n", "chain.invoke(\n", " {\n", " \"input_language\": \"English\",\n", " \"output_language\": \"German\",\n", " \"input\": \"I love programming.\",\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "d4faefc1-4736-46ab-b786-965f662434c7", "metadata": {}, "source": [ "

GroQ + Langchain + Tool Use

\n", "\n", "Here we test the capabilities of tool-enabled models hosted by groq to perform *function calling* (such as **llama3-groq-8b-8192-tool-use-preview**). This involves the LLM returning a json schema formatted in a particular way that it can be used to call functions with arguments." ] }, { "cell_type": "code", "execution_count": 9, "id": "5dc16219-bbf5-42e6-a014-bc67e0e3cf93", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import Tool, AgentExecutor, create_react_agent\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_groq import ChatGroq" ] }, { "cell_type": "code", "execution_count": 15, "id": "9287b236-19ee-45cf-8503-fbdd05c038ee", "metadata": {}, "outputs": [], "source": [ "# Create the LLM\n", "llm = ChatGroq(\n", " model=\"llama3-groq-70b-8192-tool-use-preview\",\n", " temperature=0,\n", " max_tokens=None,\n", " timeout=None,\n", " max_retries=2,\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "id": "e9aa733c-057b-436e-ab9b-4e94d2e91df5", "metadata": {}, "outputs": [], "source": [ "def calculate_square(number: str) -> str:\n", " \"\"\"Calculates the square of a given number\"\"\"\n", " try:\n", " num = float(number)\n", " return str(num * num)\n", " except ValueError:\n", " return \"Please provide a valid number\"\n", "\n", "# Define the tool\n", "tools = [\n", " Tool(\n", " name=\"Calculator\",\n", " func=calculate_square,\n", " description=\"Useful for calculating the square of a number. Input should be a single number.\"\n", " )\n", "]" ] }, { "cell_type": "code", "execution_count": 17, "id": "e8da1fd2-8f1f-40c5-8d35-07bbcbf41e96", "metadata": {}, "outputs": [], "source": [ "# Create the prompt template\n", "prompt = ChatPromptTemplate.from_messages([\n", " (\"system\", \"\"\"You are a helpful assistant that can use tools to solve problems.\n", " \n", " Available tools:\n", " {tools}\n", " \n", " {agent_scratchpad}\n", " \n", " Use the following format:\n", " Question: The user question you must answer\n", " Thought: Your thought process about what to do\n", " Action: The action to take, should be one of [{tool_names}]\n", " Action Input: The input to the action\n", " Observation: The result of the action\n", " ... (this Thought/Action/Action Input/Observation can repeat N times)\n", " Thought: I now know the final answer\n", " Final Answer: The final answer to the original question\n", " \"\"\"),\n", " (\"human\", \"{input}\")\n", "])\n", "\n", "# Create the agent\n", "agent = create_react_agent(llm, tools, prompt)\n", "\n", "# Create the agent executor\n", "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)" ] }, { "cell_type": "code", "execution_count": 18, "id": "c0ba975b-f671-4473-8684-ac621c3bfeeb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3mQuestion: What is the square of 7?\n", "Thought: I can use the Calculator tool to find the square of a number.\n", "Action: Calculator\n", "Action Input: 7\u001b[0m\u001b[36;1m\u001b[1;3m49.0\u001b[0m\u001b[32;1m\u001b[1;3m\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "Final Result: Agent stopped due to iteration limit or time limit.\n" ] } ], "source": [ "result = agent_executor.invoke({\"input\": \"What is the square of 7?\"}, handle_parsing_errors=True)\n", "print(\"\\nFinal Result:\", result[\"output\"])" ] }, { "cell_type": "markdown", "id": "4eeddfdd-da26-421b-a8e0-425f6c6854e2", "metadata": {}, "source": [ "

Results

\n", "\n", "We managed to get tool use working with the model `llama3-groq-70b-8192-tool-use-preview`. This is a much bigger model than ` llama3-groq-8b-8192-tool-use-preview` which simply outputted that it did not have the capabilities to perform such a task (causing the program to error out).\n", "The choice of the model clearly continues to be a primary determinant in whether function calling works or not. In a sense, this creates a single point of failure for such systems." ] }, { "cell_type": "code", "execution_count": null, "id": "fabf8023-0ec4-4861-ad92-627867232108", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "groq", "language": "python", "name": "groq" }, "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }