Added DeepSeek to weeks 1, 2 and 8
This commit is contained in:
126
week2/day1.ipynb
126
week2/day1.ipynb
@@ -69,12 +69,19 @@
|
||||
"For Anthropic, visit https://console.anthropic.com/ \n",
|
||||
"For Google, visit https://ai.google.dev/gemini-api \n",
|
||||
"\n",
|
||||
"### Also - adding DeepSeek if you wish\n",
|
||||
"\n",
|
||||
"Optionally, if you'd like to also use DeepSeek, create an account [here](https://platform.deepseek.com/), create a key [here](https://platform.deepseek.com/api_keys) and top up with at least the minimum $2 [here](https://platform.deepseek.com/top_up).\n",
|
||||
"\n",
|
||||
"### Adding API keys to your .env file\n",
|
||||
"\n",
|
||||
"When you get your API keys, you need to set them as environment variables by adding them to your `.env` file.\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"OPENAI_API_KEY=xxxx\n",
|
||||
"ANTHROPIC_API_KEY=xxxx\n",
|
||||
"GOOGLE_API_KEY=xxxx\n",
|
||||
"DEEPSEEK_API_KEY=xxxx\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Afterwards, you may need to restart the Jupyter Lab Kernel (the Python process that sits behind this notebook) via the Kernel menu, and then rerun the cells from the top."
|
||||
@@ -120,7 +127,7 @@
|
||||
"# Load environment variables in a file called .env\n",
|
||||
"# Print the key prefixes to help with any debugging\n",
|
||||
"\n",
|
||||
"load_dotenv()\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
||||
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
|
||||
@@ -350,6 +357,123 @@
|
||||
"print(response.choices[0].message.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "33f70c88-7ca9-470b-ad55-d93a57dcc0ab",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## (Optional) Trying out the DeepSeek model\n",
|
||||
"\n",
|
||||
"### Let's ask DeepSeek a really hard question - both the Chat and the Reasoner model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3d0019fb-f6a8-45cb-962b-ef8bf7070d4d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Optionally if you wish to try DeekSeek, you can also use the OpenAI client library\n",
|
||||
"\n",
|
||||
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
|
||||
"\n",
|
||||
"if deepseek_api_key:\n",
|
||||
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
|
||||
"else:\n",
|
||||
" print(\"DeepSeek API Key not set - please skip to the next section if you don't wish to try the DeepSeek API\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c72c871e-68d6-4668-9c27-96d52b77b867",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Using DeepSeek Chat\n",
|
||||
"\n",
|
||||
"deepseek_via_openai_client = OpenAI(\n",
|
||||
" api_key=deepseek_api_key, \n",
|
||||
" base_url=\"https://api.deepseek.com\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"response = deepseek_via_openai_client.chat.completions.create(\n",
|
||||
" model=\"deepseek-chat\",\n",
|
||||
" messages=prompts,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(response.choices[0].message.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "50b6e70f-700a-46cf-942f-659101ffeceb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"challenge = [{\"role\": \"system\", \"content\": \"You are a helpful assistant\"},\n",
|
||||
" {\"role\": \"user\", \"content\": \"How many words are there in your answer to this prompt\"}]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66d1151c-2015-4e37-80c8-16bc16367cfe",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Using DeepSeek Chat with a harder question! And streaming results\n",
|
||||
"\n",
|
||||
"stream = deepseek_via_openai_client.chat.completions.create(\n",
|
||||
" model=\"deepseek-chat\",\n",
|
||||
" messages=challenge,\n",
|
||||
" stream=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"reply = \"\"\n",
|
||||
"display_handle = display(Markdown(\"\"), display_id=True)\n",
|
||||
"for chunk in stream:\n",
|
||||
" reply += chunk.choices[0].delta.content or ''\n",
|
||||
" reply = reply.replace(\"```\",\"\").replace(\"markdown\",\"\")\n",
|
||||
" update_display(Markdown(reply), display_id=display_handle.display_id)\n",
|
||||
"\n",
|
||||
"print(\"Number of words:\", len(reply.split(\" \")))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "43a93f7d-9300-48cc-8c1a-ee67380db495",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Using DeepSeek Reasoner - this may hit an error if DeepSeek is busy\n",
|
||||
"# It's over-subscribed (as of 28-Jan-2025) but should come back online soon!\n",
|
||||
"# If this fails, come back to this in a few days..\n",
|
||||
"\n",
|
||||
"response = deepseek_via_openai_client.chat.completions.create(\n",
|
||||
" model=\"deepseek-reasoner\",\n",
|
||||
" messages=challenge\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"reasoning_content = response.choices[0].message.reasoning_content\n",
|
||||
"content = response.choices[0].message.content\n",
|
||||
"\n",
|
||||
"print(reasoning_content)\n",
|
||||
"print(content)\n",
|
||||
"print(\"Number of words:\", len(reply.split(\" \")))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c09e6b5c-6816-4cd3-a5cd-a20e4171b1a0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Back to OpenAI with a serious question"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
|
||||
Reference in New Issue
Block a user