{ "cells": [ { "cell_type": "code", "execution_count": 163, "id": "aa531c59-0557-4348-bef6-0a614ee39d0b", "metadata": {}, "outputs": [], "source": [ "import html\n", "import json\n", "import requests" ] }, { "cell_type": "code", "execution_count": 164, "id": "90b627b6-fc63-4a26-b3c6-0594f847fe78", "metadata": {}, "outputs": [], "source": [ "#URL = 'http://localhost:5001/api/v1/generate'\n", "URL = 'http://localhost:5001/api/v1/chat'" ] }, { "cell_type": "code", "execution_count": 165, "id": "ee38c219-22d8-4d84-b125-4a1b3ac8561f", "metadata": {}, "outputs": [], "source": [ "available_functions = \"\"\"\n", "{\n", " \"name\": \"go_to_location\",\n", " \"description\": \"Go to a given location.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"latitude\": {\n", " \"type\": \"number\",\n", " \"description\": \"The latitude to go to.\",\n", " },\n", " \"longitude\": {\n", " \"type\": \"number\",\n", " \"description\": \"The longitude to go to.\",\n", " },\n", " },\n", " \"required\": [\"latitude\", \"longitude\"],\n", " },\n", "},\n", "{\n", " \"name\": \"pan_in_direction\",\n", " \"description\": \"Pan in a given direction and distance in kilometers.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"direction\": {\n", " \"type\": \"string\",\n", " \"description\": \"The direction to pan in. One of 'north', 'south', 'east', 'west', 'northwest', 'northeast', 'southwest', 'southeast'.\",\n", " },\n", " \"distance_in_kilometers\": {\n", " \"type\": \"number\",\n", " \"description\": \"The distance to pan in kilometers. If not provided, defaults to 1.\",\n", " },\n", " },\n", " \"required\": [\"direction\"],\n", " },\n", "},\n", "{\n", " \"name\": \"zoom_in\",\n", " \"description\": \"Zoom in by a given number of zoom levels.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"zoom_levels\": {\n", " \"type\": \"number\",\n", " \"description\": \"The number of zoom levels to zoom in. If not provided, defaults to 1.\",\n", " },\n", " },\n", " },\n", " \"required\": [\"zoom_levels\"],\n", "},\n", "{\n", " \"name\": \"zoom_out\",\n", " \"description\": \"Zoom out by a given number of zoom levels.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"zoom_levels\": {\n", " \"type\": \"number\",\n", " \"description\": \"The number of zoom levels to zoom out. If not provided, defaults to 1.\",\n", " },\n", " },\n", " },\n", " \"required\": [\"zoom_levels\"],\n", "}\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 166, "id": "d007f9f6-c76e-4237-bd85-5cd6a0e03521", "metadata": {}, "outputs": [], "source": [ "system_message = f\"\"\"As an AI assistant, please select the most suitable function and parameters \n", "from the list of available functions below, based on the user's input. Provide your response in JSON format.\"\"\"" ] }, { "cell_type": "code", "execution_count": 167, "id": "8f7e36b3-ce1f-4c6b-ba81-9c5a884b4e93", "metadata": {}, "outputs": [], "source": [ "def send_message(message, system_message, function_descriptions, temperature=0.0):\n", " payload = f\"\"\"{system_message}\\nInput: {message}\\nAvailable functions:\\n{function_descriptions}\"\"\"\n", " history = {'internal': [], 'visible': []}\n", " request = {\n", " 'user_input': payload,\n", " 'history': history,\n", " 'temperature': temperature,\n", " }\n", " response = requests.post(URL, json=request)\n", " if response.status_code == 200:\n", " result = response.json()['results'][0]['history']\n", " response_data = html.unescape(result['visible'][-1][1])\n", " response_message = json.loads(response_data.strip('\\n'))\n", " response = {\"response\": response_message}\n", " return response" ] }, { "cell_type": "code", "execution_count": 168, "id": "82831633-bd48-4e22-9b09-80a686eadd3d", "metadata": {}, "outputs": [], "source": [ "message = \"go to paris\"" ] }, { "cell_type": "code", "execution_count": 169, "id": "5ee7f345-2e3a-4ac6-8601-c0fdc6d49b62", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'response': {'function': 'go_to_location',\n", " 'parameters': {'latitude': 48.8567, 'longitude': 2.3522}}}" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "send_message(message, system_message, available_functions)" ] }, { "cell_type": "code", "execution_count": 170, "id": "f2b4aee0-f2e2-4783-bb33-76f9906dbb66", "metadata": {}, "outputs": [], "source": [ "message = \"zoom in\"" ] }, { "cell_type": "code", "execution_count": 171, "id": "bad94859-7014-4abe-bf1e-22303634eecf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'response': {'function': 'zoom_in', 'parameters': {'zoom_levels': 2}}}" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "send_message(message, system_message, available_functions)" ] }, { "cell_type": "code", "execution_count": 172, "id": "8f755da8-0ce8-4ed5-bae5-21712bc604bf", "metadata": {}, "outputs": [], "source": [ "message = \"pan north by 30km\"" ] }, { "cell_type": "code", "execution_count": 173, "id": "5efddf84-6bea-457d-91b7-ab70f5f4909f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'response': {'function': 'pan_in_direction',\n", " 'parameters': {'direction': 'north', 'distance_in_kilometers': 30}}}" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "send_message(message, system_message, available_functions)" ] }, { "cell_type": "code", "execution_count": null, "id": "2ba2bbc0-7d27-48ed-bd0a-b11dfa099098", "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.9.17" } }, "nbformat": 4, "nbformat_minor": 5 }