Skip to content

Instantly share code, notes, and snippets.

@alonsosilvaallende
Last active July 22, 2024 07:04
Show Gist options
  • Save alonsosilvaallende/c4731e0db6bc8292ad2ae7e66ceb1ffd to your computer and use it in GitHub Desktop.
Save alonsosilvaallende/c4731e0db6bc8292ad2ae7e66ceb1ffd to your computer and use it in GitHub Desktop.
Ollama-FC
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1a337d4a-7345-45d8-a540-7bd11d974d6d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-20T12:58:23.766633Z",
"iopub.status.busy": "2024-07-20T12:58:23.766145Z",
"iopub.status.idle": "2024-07-20T12:58:24.129443Z",
"shell.execute_reply": "2024-07-20T12:58:24.128545Z",
"shell.execute_reply.started": "2024-07-20T12:58:23.766580Z"
}
},
"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": 2,
"id": "db0a666c-7b21-402a-845b-fb19a9b0c090",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-20T12:58:25.009448Z",
"iopub.status.busy": "2024-07-20T12:58:25.008906Z",
"iopub.status.idle": "2024-07-20T12:58:25.018361Z",
"shell.execute_reply": "2024-07-20T12:58:25.016954Z",
"shell.execute_reply.started": "2024-07-20T12:58:25.009394Z"
}
},
"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": 3,
"id": "4e7fc54a-738b-427f-8987-6c68e4a6f5c6",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-20T12:58:26.231707Z",
"iopub.status.busy": "2024-07-20T12:58:26.231066Z",
"iopub.status.idle": "2024-07-20T12:58:26.827830Z",
"shell.execute_reply": "2024-07-20T12:58:26.826866Z",
"shell.execute_reply.started": "2024-07-20T12:58:26.231618Z"
}
},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"mike/mistral\",\n",
" messages=[\n",
" {\"role\": \"user\", \"content\": \"What is the weather like today in Paris?\"}\n",
" ],\n",
" tools=tools\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c32419c1-7f7c-435a-8bc1-ccf09b6bcd54",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-20T12:58:30.677504Z",
"iopub.status.busy": "2024-07-20T12:58:30.676951Z",
"iopub.status.idle": "2024-07-20T12:58:30.691485Z",
"shell.execute_reply": "2024-07-20T12:58:30.690406Z",
"shell.execute_reply.started": "2024-07-20T12:58:30.677453Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'get_current_weather'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.choices[0].message.tool_calls[0].function.name"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "931cc31b-f5e8-4111-9ac5-79593227ff60",
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-20T12:58:31.782745Z",
"iopub.status.busy": "2024-07-20T12:58:31.781367Z",
"iopub.status.idle": "2024-07-20T12:58:31.791082Z",
"shell.execute_reply": "2024-07-20T12:58:31.789541Z",
"shell.execute_reply.started": "2024-07-20T12:58:31.782688Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'{\"format\":\"celsius\",\"location\":\"Paris, FR\"}'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.choices[0].message.tool_calls[0].function.arguments"
]
}
],
"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
}
@812781385
Copy link

Can't hit function call

---- response ---- ChatCompletion(id='chatcmpl-800', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content="I'm sorry, but as an AI language model, I don't have access to real-time information about the current weather. However, you can easily check the current weather in Paris by searching online for a reliable weather website or app, or by checking local news sources. They should be able to provide you with up-to-date information on temperature, precipitation, and other relevant weather conditions.", role='assistant', function_call=None, tool_calls=None))], created=1721613846, model='qwen:32b', object='chat.completion', service_tier=None, system_fingerprint='fp_ollama', usage=CompletionUsage(completion_tokens=78, prompt_tokens=14, total_tokens=92))

@alonsosilvaallende
Copy link
Author

alonsosilvaallende commented Jul 22, 2024

@812781385 Thank you for trying this gist. It probably has to do with the template of the model you are using (qwen:32b?) that has not been updated yet. I have seen three models with updated templates:

You can also try to modify the template yourself by following the templates of the models I have mentioned: https://www.gpu-mart.com/blog/custom-llm-models-with-ollama-modelfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment