Skip to content

Instantly share code, notes, and snippets.

@alonsosilvaallende
Last active July 30, 2024 07:19
Show Gist options
  • Save alonsosilvaallende/0d5e3632992e5a6148a5b29b0cb1516b to your computer and use it in GitHub Desktop.
Save alonsosilvaallende/0d5e3632992e5a6148a5b29b0cb1516b to your computer and use it in GitHub Desktop.
Ollama-OpenAI-FC
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f383a4be-173b-4527-b0ce-ecd20218b03c",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:19:27.269999Z",
"iopub.status.busy": "2024-07-22T07:19:27.269221Z",
"iopub.status.idle": "2024-07-22T07:19:27.294606Z",
"shell.execute_reply": "2024-07-22T07:19:27.293402Z",
"shell.execute_reply.started": "2024-07-22T07:19:27.269946Z"
}
},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a09f9ddc-a301-4825-9ddf-aa02d1a04e38",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:19:27.988472Z",
"iopub.status.busy": "2024-07-22T07:19:27.987953Z",
"iopub.status.idle": "2024-07-22T07:19:28.406656Z",
"shell.execute_reply": "2024-07-22T07:19:28.406191Z",
"shell.execute_reply.started": "2024-07-22T07:19:27.988422Z"
}
},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"\n",
"client = OpenAI(\n",
" base_url = 'http://localhost:11434/v1',\n",
" api_key='ollama', # required, but unused\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0af97f6d-7afc-4a67-8c93-4d42267cbace",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:23:22.404158Z",
"iopub.status.busy": "2024-07-22T07:23:22.403597Z",
"iopub.status.idle": "2024-07-22T07:23:23.988777Z",
"shell.execute_reply": "2024-07-22T07:23:23.987854Z",
"shell.execute_reply.started": "2024-07-22T07:23:22.404105Z"
}
},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"interstellarninja/hermes-2-pro-llama-3-8b-tools\",\n",
" messages=[\n",
" {\"role\": \"user\", \"content\": \"Hi!\"}\n",
" ],\n",
" temperature=0,\n",
" seed=0,\n",
" stop=\"\\n\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1f936f63-56e6-44c2-9c61-8abc5f38c87f",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:23:24.642703Z",
"iopub.status.busy": "2024-07-22T07:23:24.642150Z",
"iopub.status.idle": "2024-07-22T07:23:24.668032Z",
"shell.execute_reply": "2024-07-22T07:23:24.667021Z",
"shell.execute_reply.started": "2024-07-22T07:23:24.642653Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Hello! How can I help you today? If you have any questions or need assistance, feel free to ask.'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "83a0130b-8a29-422c-9ad6-57cbae1dab9a",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:23:31.190063Z",
"iopub.status.busy": "2024-07-22T07:23:31.189543Z",
"iopub.status.idle": "2024-07-22T07:23:31.214438Z",
"shell.execute_reply": "2024-07-22T07:23:31.213193Z",
"shell.execute_reply.started": "2024-07-22T07:23:31.190013Z"
}
},
"outputs": [],
"source": [
"tools = [{\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_current_weather\",\n",
" \"description\": \"Get the current weather\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, CA\"\n",
" },\n",
" \"format\": {\n",
" \"type\": \"string\",\n",
" \"enum\": [\n",
" \"celsius\",\n",
" \"fahrenheit\"\n",
" ],\n",
" \"description\": \"The temperature unit to use. Infer this from the users location.\"\n",
" }\n",
" },\n",
" \"required\": [\n",
" \"location\",\n",
" \"format\"\n",
" ]\n",
" }\n",
" }\n",
" }\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "378420fc-dde7-4887-a497-3b7ffec2dfcc",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:30:53.307943Z",
"iopub.status.busy": "2024-07-22T07:30:53.306906Z",
"iopub.status.idle": "2024-07-22T07:30:53.331842Z",
"shell.execute_reply": "2024-07-22T07:30:53.330525Z",
"shell.execute_reply.started": "2024-07-22T07:30:53.307882Z"
}
},
"outputs": [],
"source": [
"import json\n",
"def get_current_weather(location, format=\"celsius\"):\n",
" \"\"\"Get the current weather in a given location\"\"\"\n",
" return json.dumps({\"location\": location, \"temperature\": \"22\", \"format\": format})"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "645364f6-1697-4f2e-af7c-ebf0157192e2",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:30:54.509505Z",
"iopub.status.busy": "2024-07-22T07:30:54.508384Z",
"iopub.status.idle": "2024-07-22T07:30:54.536153Z",
"shell.execute_reply": "2024-07-22T07:30:54.534437Z",
"shell.execute_reply.started": "2024-07-22T07:30:54.509444Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'{\"location\": \"Paris\", \"temperature\": \"22\", \"format\": \"celsius\"}'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_current_weather(\"Paris\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "ad168f0c-23e0-4a7a-af46-850feb1eb355",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:27:17.666143Z",
"iopub.status.busy": "2024-07-22T07:27:17.665618Z",
"iopub.status.idle": "2024-07-22T07:27:17.688411Z",
"shell.execute_reply": "2024-07-22T07:27:17.687415Z",
"shell.execute_reply.started": "2024-07-22T07:27:17.666089Z"
}
},
"outputs": [],
"source": [
"messages = [\n",
" {\"role\": \"user\", \"content\": \"What is the weather like today in Paris?\"}\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "9fed31d6-d64a-4785-8992-eae6f845e419",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:27:27.837453Z",
"iopub.status.busy": "2024-07-22T07:27:27.836345Z",
"iopub.status.idle": "2024-07-22T07:27:31.434724Z",
"shell.execute_reply": "2024-07-22T07:27:31.434067Z",
"shell.execute_reply.started": "2024-07-22T07:27:27.837391Z"
}
},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"interstellarninja/hermes-2-pro-llama-3-8b-tools\",\n",
" messages=messages,\n",
" tools=tools,\n",
" temperature=0,\n",
" seed=0\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "63789ee7-3ea2-4549-bfad-6f68f3c2638a",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:27:32.091614Z",
"iopub.status.busy": "2024-07-22T07:27:32.090981Z",
"iopub.status.idle": "2024-07-22T07:27:32.117305Z",
"shell.execute_reply": "2024-07-22T07:27:32.116236Z",
"shell.execute_reply.started": "2024-07-22T07:27:32.091491Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"ChatCompletionMessage(content='', role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_d4y9n4y7', function=Function(arguments='{\"format\":\"celsius\",\"location\":\"Paris, FR\"}', name='get_current_weather'), type='function')])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response_message = response.choices[0].message\n",
"response_message"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "4b198162-df5c-462d-a8fd-7b046b1db6b7",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:27:47.751360Z",
"iopub.status.busy": "2024-07-22T07:27:47.750848Z",
"iopub.status.idle": "2024-07-22T07:27:47.773446Z",
"shell.execute_reply": "2024-07-22T07:27:47.772677Z",
"shell.execute_reply.started": "2024-07-22T07:27:47.751307Z"
}
},
"outputs": [],
"source": [
"messages.append(response_message)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "38e81c3d-783e-4a13-a13a-4712cdd71677",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:31:07.250800Z",
"iopub.status.busy": "2024-07-22T07:31:07.250156Z",
"iopub.status.idle": "2024-07-22T07:31:07.277389Z",
"shell.execute_reply": "2024-07-22T07:31:07.276055Z",
"shell.execute_reply.started": "2024-07-22T07:31:07.250745Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[{'role': 'user', 'content': 'What is the weather like today in Paris?'},\n",
" ChatCompletionMessage(content='', role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_d4y9n4y7', function=Function(arguments='{\"format\":\"celsius\",\"location\":\"Paris, FR\"}', name='get_current_weather'), type='function')])]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "3464ecb2-d0bc-4abf-9a75-a09402eaaeb0",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:32:22.798552Z",
"iopub.status.busy": "2024-07-22T07:32:22.798034Z",
"iopub.status.idle": "2024-07-22T07:32:22.823255Z",
"shell.execute_reply": "2024-07-22T07:32:22.821826Z",
"shell.execute_reply.started": "2024-07-22T07:32:22.798498Z"
}
},
"outputs": [],
"source": [
"tool_calls = response_message.tool_calls"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "a65bce66-9588-48e5-8bc5-fa0a99350aa2",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:39:13.970360Z",
"iopub.status.busy": "2024-07-22T07:39:13.969365Z",
"iopub.status.idle": "2024-07-22T07:39:13.994314Z",
"shell.execute_reply": "2024-07-22T07:39:13.993456Z",
"shell.execute_reply.started": "2024-07-22T07:39:13.970300Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[ChatCompletionMessageToolCall(id='call_d4y9n4y7', function=Function(arguments='{\"format\":\"celsius\",\"location\":\"Paris, FR\"}', name='get_current_weather'), type='function')]"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool_calls"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "dbca5439-d7d9-4612-b367-ce107db80fb3",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:39:57.936735Z",
"iopub.status.busy": "2024-07-22T07:39:57.936235Z",
"iopub.status.idle": "2024-07-22T07:39:57.961089Z",
"shell.execute_reply": "2024-07-22T07:39:57.959933Z",
"shell.execute_reply.started": "2024-07-22T07:39:57.936681Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'call_d4y9n4y7'"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response_message.tool_calls[0].id"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "42733976-db06-47b6-a120-c26d88254ff2",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:33:20.418256Z",
"iopub.status.busy": "2024-07-22T07:33:20.417739Z",
"iopub.status.idle": "2024-07-22T07:33:20.439470Z",
"shell.execute_reply": "2024-07-22T07:33:20.438696Z",
"shell.execute_reply.started": "2024-07-22T07:33:20.418201Z"
}
},
"outputs": [],
"source": [
"function_name = tool_calls[0].function.name"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "a1f1c618-94a2-4bd3-9453-c75191ead075",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:36:02.870310Z",
"iopub.status.busy": "2024-07-22T07:36:02.869703Z",
"iopub.status.idle": "2024-07-22T07:36:02.895302Z",
"shell.execute_reply": "2024-07-22T07:36:02.894365Z",
"shell.execute_reply.started": "2024-07-22T07:36:02.870254Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'format': 'celsius', 'location': 'Paris, FR'}"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json.loads(tool_calls[0].function.arguments)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "81871686-b331-4c33-b85e-1436cffa1b5d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:35:37.961323Z",
"iopub.status.busy": "2024-07-22T07:35:37.960361Z",
"iopub.status.idle": "2024-07-22T07:35:37.986654Z",
"shell.execute_reply": "2024-07-22T07:35:37.985674Z",
"shell.execute_reply.started": "2024-07-22T07:35:37.961257Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Paris, FR'"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"location = json.loads(tool_calls[0].function.arguments).get(\"location\")\n",
"location"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "a2926511-63e4-4144-a383-dd5149d8eff2",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:36:34.229466Z",
"iopub.status.busy": "2024-07-22T07:36:34.228943Z",
"iopub.status.idle": "2024-07-22T07:36:34.254276Z",
"shell.execute_reply": "2024-07-22T07:36:34.253359Z",
"shell.execute_reply.started": "2024-07-22T07:36:34.229412Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'celsius'"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"format = json.loads(tool_calls[0].function.arguments).get(\"format\")\n",
"format"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "9d43945f-89a1-4852-aa5c-888d478b773e",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:37:31.640538Z",
"iopub.status.busy": "2024-07-22T07:37:31.639777Z",
"iopub.status.idle": "2024-07-22T07:37:31.663073Z",
"shell.execute_reply": "2024-07-22T07:37:31.662021Z",
"shell.execute_reply.started": "2024-07-22T07:37:31.640479Z"
}
},
"outputs": [],
"source": [
"available_functions = {\n",
" \"get_current_weather\": get_current_weather,\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "bf859583-af3e-4d13-9f5c-7d578f9b73e5",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:37:06.562770Z",
"iopub.status.busy": "2024-07-22T07:37:06.561886Z",
"iopub.status.idle": "2024-07-22T07:37:06.587160Z",
"shell.execute_reply": "2024-07-22T07:37:06.586313Z",
"shell.execute_reply.started": "2024-07-22T07:37:06.562714Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'get_current_weather'"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function_name"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "47acd9f1-e7e1-412f-9477-14988071e2d6",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:38:27.684590Z",
"iopub.status.busy": "2024-07-22T07:38:27.684076Z",
"iopub.status.idle": "2024-07-22T07:38:27.709162Z",
"shell.execute_reply": "2024-07-22T07:38:27.708285Z",
"shell.execute_reply.started": "2024-07-22T07:38:27.684536Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'{\"location\": \"Paris, FR\", \"temperature\": \"22\", \"format\": \"celsius\"}'"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function_response = available_functions[function_name](location=location, format=format)\n",
"function_response"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "26e72f1f-f8cd-4cdd-9636-ad1002ec4719",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:40:26.255978Z",
"iopub.status.busy": "2024-07-22T07:40:26.255443Z",
"iopub.status.idle": "2024-07-22T07:40:26.280294Z",
"shell.execute_reply": "2024-07-22T07:40:26.279090Z",
"shell.execute_reply.started": "2024-07-22T07:40:26.255923Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'call_d4y9n4y7'"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool_calls[0].id"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "3b299bf3-e54b-499a-af21-e0951e3feb20",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:40:45.301173Z",
"iopub.status.busy": "2024-07-22T07:40:45.300659Z",
"iopub.status.idle": "2024-07-22T07:40:45.323931Z",
"shell.execute_reply": "2024-07-22T07:40:45.322551Z",
"shell.execute_reply.started": "2024-07-22T07:40:45.301121Z"
}
},
"outputs": [],
"source": [
"messages.append(\n",
" {\n",
" \"tool_call_id\": tool_calls[0].id,\n",
" \"role\": \"tool\",\n",
" \"name\": function_name,\n",
" \"content\": function_response,\n",
" }\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "6168c998-8dd5-4e32-98f1-dc479e41da66",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:40:50.359148Z",
"iopub.status.busy": "2024-07-22T07:40:50.358640Z",
"iopub.status.idle": "2024-07-22T07:40:50.383299Z",
"shell.execute_reply": "2024-07-22T07:40:50.382429Z",
"shell.execute_reply.started": "2024-07-22T07:40:50.359095Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[{'role': 'user', 'content': 'What is the weather like today in Paris?'},\n",
" ChatCompletionMessage(content='', role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_d4y9n4y7', function=Function(arguments='{\"format\":\"celsius\",\"location\":\"Paris, FR\"}', name='get_current_weather'), type='function')]),\n",
" {'tool_call_id': 'call_d4y9n4y7',\n",
" 'role': 'tool',\n",
" 'name': 'get_current_weather',\n",
" 'content': '{\"location\": \"Paris, FR\", \"temperature\": \"22\", \"format\": \"celsius\"}'}]"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "46100b26-0541-4be1-9680-8b87257f30b8",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:41:08.740992Z",
"iopub.status.busy": "2024-07-22T07:41:08.740444Z",
"iopub.status.idle": "2024-07-22T07:41:13.721279Z",
"shell.execute_reply": "2024-07-22T07:41:13.720684Z",
"shell.execute_reply.started": "2024-07-22T07:41:08.740938Z"
}
},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"interstellarninja/hermes-2-pro-llama-3-8b-tools\",\n",
" messages=messages,\n",
" tools=tools,\n",
" temperature=0,\n",
" seed=0\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "4a9aa8b3-64d3-4c55-b84d-8e79c0f27d95",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-22T07:41:44.120459Z",
"iopub.status.busy": "2024-07-22T07:41:44.119954Z",
"iopub.status.idle": "2024-07-22T07:41:44.145705Z",
"shell.execute_reply": "2024-07-22T07:41:44.144207Z",
"shell.execute_reply.started": "2024-07-22T07:41:44.120406Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'The current temperature in Paris is 22 degrees Celsius.'"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.choices[0].message.content"
]
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment