Skip to content

Instantly share code, notes, and snippets.

@giswqs
Last active February 20, 2025 00:55
Show Gist options
  • Save giswqs/8146c5f3ea70cd43750c646376a02394 to your computer and use it in GitHub Desktop.
Save giswqs/8146c5f3ea70cd43750c646376a02394 to your computer and use it in GitHub Desktop.
deepseek
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Open WebUI API Endpoints with Deepseek R1 distrilled models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get available models:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!curl -H \"Authorization: Bearer $WEBUI_API_KEY\" http://localhost:8080/api/models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ask a question:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"curl -X POST http://localhost:8080/api/chat/completions \\\n",
" -H \"Authorization: Bearer $WEBUI_API_KEY\" \\\n",
" -H \"Content-Type: application/json\" \\\n",
" -d '{\n",
" \"model\": \"deepseek-r1:1.5b\",\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Why is the sky blue?\"\n",
" }\n",
" ]\n",
" }'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Stream the answer:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"curl -X POST http://localhost:8080/api/chat/completions \\\n",
"-H \"Authorization: Bearer $WEBUI_API_KEY\" \\\n",
"-H \"Content-Type: application/json\" \\\n",
"-d '{\n",
" \"model\": \"deepseek-r1:1.5b\",\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Why is the sky blue?\"\n",
" }\n",
" ],\n",
" \"stream\": true\n",
" }'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get available models using Python:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"\n",
"def get_available_models(api_key=None, url=\"http://localhost:8080/api/models\"):\n",
" \"\"\"\n",
" Retrieve available models from the API endpoint.\n",
"\n",
" Args:\n",
" api_key (str, optional): API key for authentication. If not provided,\n",
" the function will attempt to read the environment variable 'WEBUI_API_KEY'.\n",
" url (str, optional): The API URL to fetch the models from. Defaults to \"http://localhost:8080/api/models\".\n",
"\n",
" Returns:\n",
" list: A list of model names if successful.\n",
" None: If an error occurs.\n",
" \n",
" Raises:\n",
" ValueError: If no API key is provided and the environment variable 'WEBUI_API_KEY' is not set.\n",
" \"\"\"\n",
" # Use the provided API key or retrieve it from the environment variable\n",
" if api_key is None:\n",
" api_key = os.getenv(\"WEBUI_API_KEY\")\n",
" if not api_key:\n",
" raise ValueError(\"Please set the environment variable 'WEBUI_API_KEY' with your API key.\")\n",
"\n",
" headers = {\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
"\n",
" try:\n",
" response = requests.get(url, headers=headers)\n",
" response.raise_for_status() # Raises an error for non-2xx responses\n",
"\n",
" # Extract the list of models from the JSON response\n",
" data = response.json().get('data', [])\n",
" models = [model.get('name') for model in data]\n",
" return models\n",
" except requests.exceptions.RequestException as e:\n",
" print(\"An error occurred:\", e)\n",
" return None\n",
"\n",
"# Example usage:\n",
"if __name__ == \"__main__\":\n",
" models = get_available_models()\n",
" if models is not None:\n",
" print(\"Available Models:\")\n",
" print(models)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the API endpoint to ask a question using Python:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import requests\n",
"\n",
"def get_chat_completion(content=\"Why is the sky blue?\", api_key=None, model=\"deepseek-r1:1.5b\", \n",
" url=\"http://localhost:8080/api/chat/completions\"):\n",
" \"\"\"\n",
" Sends a chat completion request to the API and returns the response content.\n",
"\n",
" Args:\n",
" content (str, optional): The user message content to send. Defaults to \"Why is the sky blue?\".\n",
" api_key (str, optional): Your API key. If not provided, it is read from the\n",
" 'WEBUI_API_KEY' environment variable.\n",
" model (str, optional): The model to use. Defaults to \"deepseek-r1:1.5b\".\n",
" url (str, optional): The API endpoint URL.\n",
"\n",
" Returns:\n",
" str: The chat completion content from the API response.\n",
" Returns None if an error occurs.\n",
" \n",
" Raises:\n",
" ValueError: If no API key is provided and the environment variable is not set.\n",
" \"\"\"\n",
" # Use provided API key or retrieve it from the environment variable\n",
" if api_key is None:\n",
" api_key = os.getenv(\"WEBUI_API_KEY\")\n",
" if not api_key:\n",
" raise ValueError(\"Please set the environment variable 'WEBUI_API_KEY' with your API key.\")\n",
"\n",
" headers = {\n",
" \"Authorization\": f\"Bearer {api_key}\",\n",
" \"Content-Type\": \"application/json\"\n",
" }\n",
"\n",
" # Define the payload with the given content as the user's message\n",
" payload = {\n",
" \"model\": model,\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": content\n",
" }\n",
" ]\n",
" }\n",
"\n",
" try:\n",
" response = requests.post(url, headers=headers, json=payload)\n",
" response.raise_for_status() # Raises an exception for HTTP error responses\n",
"\n",
" # Parse the JSON response and extract the chat completion content\n",
" response_data = response.json()\n",
" chat_content = response_data['choices'][0]['message']['content']\n",
" return chat_content\n",
"\n",
" except requests.exceptions.RequestException as err:\n",
" print(\"An error occurred:\", err)\n",
" except json.JSONDecodeError as json_err:\n",
" print(\"Failed to parse JSON response:\", json_err)\n",
"\n",
" return None\n",
"\n",
"# Example usage:\n",
"if __name__ == \"__main__\":\n",
" # You can modify the content argument as needed.\n",
" result = get_chat_completion(content=\"Why is the sky blue?\")\n",
" if result is not None:\n",
" print(\"Response:\")\n",
" print(result)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geo",
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@JunchuanYu
Copy link

Professor Wu, is there any way to realize the interaction between deepseek and remote sensing and GIS software, or to perform tasks in real time through code-driven software

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