-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
537c24b
commit 300d06f
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters