From 300d06f67dda0aff951d80730af85c70721a4af7 Mon Sep 17 00:00:00 2001 From: Robert Haase Date: Sat, 25 Jan 2025 11:11:57 +0100 Subject: [PATCH] added DeepSeek endpoint --- .../08_deepseek_endpoint.ipynb | 154 ++++++++++++++++++ docs/_toc.yml | 1 + 2 files changed, 155 insertions(+) create mode 100644 docs/15_endpoint_apis/08_deepseek_endpoint.ipynb diff --git a/docs/15_endpoint_apis/08_deepseek_endpoint.ipynb b/docs/15_endpoint_apis/08_deepseek_endpoint.ipynb new file mode 100644 index 0000000..86d8ff7 --- /dev/null +++ b/docs/15_endpoint_apis/08_deepseek_endpoint.ipynb @@ -0,0 +1,154 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "87404224-b84b-409f-8683-c4a243d29722", + "metadata": {}, + "source": [ + "# DeepSeek endpoint\n", + "In this notebook we will use the [DeepSeek](https://www.deepseek.com/) API. Before you can access it, you need to create an account and [API key](https://platform.deepseek.com/api_keys). You will see that also this method uses the OpenAI API and we change the `base_url`." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "752e974d-9aaf-44aa-80fb-01a042cf5774", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'1.58.1'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "import openai\n", + "openai.__version__" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ab55e229-93b9-4e9b-974d-037002690bf0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def prompt_deepseek(message:str, model=\"deepseek-chat\"):\n", + " \"\"\"A prompt helper function that sends a message to DeepSeek\n", + " and returns only the text response.\n", + " \"\"\"\n", + " import os\n", + " \n", + " # convert message in the right format if necessary\n", + " if isinstance(message, str):\n", + " message = [{\"role\": \"user\", \"content\": message}]\n", + " \n", + " # setup connection to the LLM\n", + " client = openai.OpenAI()\n", + " client.base_url = \"https://api.deepseek.com\"\n", + " client.api_key = os.environ.get('DEEPSEEK_API_KEY')\n", + " response = client.chat.completions.create(\n", + " model=model,\n", + " messages=message\n", + " )\n", + " \n", + " # extract answer\n", + " return response.choices[0].message.content" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "a7654a20-a307-4b26-8d25-bef20b70224e", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello! How can I assist you today? 😊'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prompt_deepseek(\"Hi!\")" + ] + }, + { + "cell_type": "markdown", + "id": "578e9edd-b58f-4fd0-a56d-1966105221dc", + "metadata": {}, + "source": [ + "## Exercise\n", + "List the models available in the DeepSeek endpoint and try them out by specifying them when calling `prompt_deepseek()`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "05171ba7-a775-41c5-954d-7d4fc2b5b625", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "deepseek-chat\n", + "deepseek-reasoner\n" + ] + } + ], + "source": [ + "client = openai.OpenAI()\n", + "client.base_url = \"https://api.deepseek.com\"\n", + "client.api_key = os.environ.get('DEEPSEEK_API_KEY')\n", + "\n", + "print(\"\\n\".join([model.id for model in client.models.list().data]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e810ee2-4d22-42f6-add5-532cf95b4b9c", + "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.11.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/_toc.yml b/docs/_toc.yml index f92a281..023e5ae 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -28,6 +28,7 @@ parts: - file: 15_endpoint_apis/03_blablador_endpoint.ipynb - file: 15_endpoint_apis/05_azure_endpoints.ipynb - file: 15_endpoint_apis/07_mistral_endpoint.ipynb + - file: 15_endpoint_apis/08_deepseek_endpoint.ipynb - file: 15_endpoint_apis/10_anthropic_api.ipynb - file: 15_endpoint_apis/20_google_gemini.ipynb - file: 15_endpoint_apis/30_huggingface.ipynb