Skip to content

Commit

Permalink
added DeepSeek endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Jan 25, 2025
1 parent 537c24b commit 300d06f
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
154 changes: 154 additions & 0 deletions docs/15_endpoint_apis/08_deepseek_endpoint.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 300d06f

Please sign in to comment.