{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "import os\n", "from dotenv import load_dotenv\n", "from groq import Groq\n", "from pydantic import create_model\n", "import inspect, json\n", "from inspect import Parameter\n", "\n", "print(load_dotenv())\n", "\n", "os.environ['GROQ_API_KEY'] = os.getenv(\"GROQ_API_KEY\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Custom agent**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Define the functions**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def abc(num1:int, num2:int)->int:\n", " \"Compute abc between two numbers\"\n", " return 2*(num1) - 2*(num2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc(2, 3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def jsonschema(f):\n", " \"\"\"\n", " Generate a JSON schema for the input parameters of the given function.\n", "\n", " Parameters:\n", " f (FunctionType): The function for which to generate the JSON schema.\n", "\n", " Returns:\n", " Dict: A dictionary containing the function name, description, and parameters schema.\n", " \"\"\"\n", " kw = {n: (o.annotation, ... if o.default == Parameter.empty else o.default)\n", " for n, o in inspect.signature(f).parameters.items()}\n", " s = create_model(f'Input for `{f.__name__}`', **kw).schema()\n", " return dict(name=f.__name__, description=f.__doc__, parameters=s)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'abc',\n", " 'description': 'Compute abc between two numbers',\n", " 'parameters': {'properties': {'num1': {'title': 'Num1', 'type': 'integer'},\n", " 'num2': {'title': 'Num2', 'type': 'integer'}},\n", " 'required': ['num1', 'num2'],\n", " 'title': 'Input for `abc`',\n", " 'type': 'object'}}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc_json = jsonschema(abc)\n", "abc_json" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "model_name = \"llama-3.3-70b-versatile\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ask Groq**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "client = Groq()\n", "\n", "response = client.chat.completions.create(\n", " model= model_name,\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": \"compute abc between 2 and 3\"},\n", " ],\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'To compute the absolute difference, also known as the absolute value of the difference, between 2 and 3:\\n\\n|2 - 3| = |-1| = 1\\n\\nSo the absolute difference between 2 and 3 is 1.'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "messages= [\n", " {\"role\": \"user\", \"content\": \"Compute abc between 2 and 3\"}\n", "]\n", "\n", "# Pass th function to groq model\n", "response = client.chat.completions.create(\n", " model=model_name,\n", " messages=messages,\n", " functions=[abc_json],\n", " function_call=\"auto\",\n", " temperature=0\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ChatCompletion(id='chatcmpl-e37ef9c8-5015-4811-91c8-6373640bb737', choices=[Choice(finish_reason='function_call', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', annotations=None, executed_tools=None, function_call=FunctionCall(arguments='{\"num1\":2,\"num2\":3}', name='abc'), reasoning=None, tool_calls=None))], created=1778583530, model='llama-3.3-70b-versatile', object='chat.completion', mcp_list_tools=None, service_tier='on_demand', system_fingerprint='fp_ce7bc1685b', usage=CompletionUsage(completion_tokens=21, prompt_tokens=237, total_tokens=258, completion_time=0.047009754, completion_tokens_details=None, prompt_time=0.013427763, prompt_tokens_details=None, queue_time=0.048241546, total_time=0.060437517), usage_breakdown=None, x_groq=XGroq(id='req_01krdxdtkmeghva26epfsabw0m', debug=None, seed=1357225914, usage=None))" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Executing the function by extracting the info from the output of the model**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "FunctionCall(arguments='{\"num1\":2,\"num2\":3}', name='abc')\n", "{\"num1\":2,\"num2\":3}\n", "\n" ] } ], "source": [ "print(response.choices[0].message.function_call)\n", "print(response.choices[0].message.function_call.arguments)\n", "print(type(response.choices[0].message.function_call.arguments))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Function name: abc\n", "Function arguments: {'num1': 2, 'num2': 3}\n", "\n" ] } ], "source": [ "func_name = response.choices[0].message.function_call.name\n", "func_args = json.loads(response.choices[0].message.function_call.arguments)\n", "print(\"Function name:\", func_name)\n", "print(\"Function arguments:\", func_args)\n", "print(type(func_args))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-2\n" ] } ], "source": [ "if func_name == 'abc':\n", " result = abc(**func_args)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **Using Langchain**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from langchain_core.tools import tool\n", "\n", "@tool\n", "def abc(num1:int, num2:int)->int:\n", " \"Compute abc between two numbers\"\n", " return 2*(num1) - 2*(num2)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Compute abc between two numbers'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abc.description" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from langchain_groq import ChatGroq \n", "llm = ChatGroq(model=\"llama-3.3-70b-versatile\", temperature=0)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "tools = [abc]\n", "\n", "llm_with_tools = llm.bind_tools(tools)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "response = llm_with_tools.invoke(\"Compute abc between 2 and 3\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'tool_calls': [{'id': 'rjae3mfkg',\n", " 'function': {'arguments': '{\"num1\":2,\"num2\":3}', 'name': 'abc'},\n", " 'type': 'function'}]}" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.additional_kwargs" ] } ], "metadata": { "kernelspec": { "display_name": "querymind (3.12.10)", "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.12.10" } }, "nbformat": 4, "nbformat_minor": 2 }