{ "cells": [ { "metadata": { "ExecuteTime": { "end_time": "2026-09-03T09:14:58.535236Z", "start_time": "2026-09-03T09:14:57.335343800Z" } }, "cell_type": "code", "source": [ "from openai import OpenAI\n", "from openai.types.chat import (\n", " ChatCompletionSystemMessageParam,\n", " ChatCompletionUserMessageParam,\n", " ChatCompletionAssistantMessageParam\n", ")" ], "id": "255e9b078be17a5f", "outputs": [], "execution_count": 1 }, { "metadata": { "ExecuteTime": { "end_time": "2026-09-03T09:19:09.642201600Z", "start_time": "2026-09-03T09:19:09.628764300Z" } }, "cell_type": "code", "source": [ "client = OpenAI(\n", " api_key='sk-4bdcdad5d4cd4856bc0c308c3f74eb22',\n", " base_url='https://api.deepseek.com'\n", ")" ], "id": "6f8aeb64ad6aca1f", "outputs": [], "execution_count": 6 }, { "metadata": { "ExecuteTime": { "end_time": "2026-09-03T09:17:52.034706300Z", "start_time": "2026-09-03T09:17:52.023338600Z" } }, "cell_type": "code", "source": [ "messages: list[ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam] = [\n", " ChatCompletionSystemMessageParam(role=\"system\", content=\"你是一个ai助手,所有回答使用中文\")\n", "]" ], "id": "4530b047d03a8ab3", "outputs": [], "execution_count": 3 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": [ "# 第一轮对话\n", "print(\"===========\")\n", "messages.append(ChatCompletionUserMessageParam(role=\"user\", content=\"你好,我是大哥\"))\n", "\n", "response = client.chat.completions.create(\n", " model='deepseek-v4-flash',\n", " messages=messages,\n", " stream=False\n", ")" ], "id": "a42f7a8dbdfa3b8c" }, { "metadata": { "ExecuteTime": { "end_time": "2026-09-03T08:51:19.785432800Z", "start_time": "2026-09-03T08:51:19.759474700Z" } }, "cell_type": "code", "source": [ "# print(response.model_dump_json())\n", "print(response.choices[0].message.content)" ], "id": "fac34b2742f048e0", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "大哥你好!我是你的AI助手,随时听候差遣。今天有什么需要帮忙的吗?\n" ] } ], "execution_count": 12 }, { "metadata": { "ExecuteTime": { "end_time": "2026-09-03T08:51:41.715208300Z", "start_time": "2026-09-03T08:51:36.483133100Z" } }, "cell_type": "code", "source": [ "# 第二轮对话\n", "messages.append(ChatCompletionAssistantMessageParam(role=\"assistant\", content=response.choices[0].message.content))\n", "messages.append(ChatCompletionUserMessageParam(role=\"user\", content=\"你还记得我吗\"))\n", "response = client.chat.completions.create(\n", " model='deepseek-v4-flash',\n", " messages=messages,\n", ")\n", "print(response.choices[0].message.content)" ], "id": "3c9fa1f10b75c256", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "大哥,这个问题您刚刚才问过呢,我当然记得您!您就是“大哥”嘛,我怎么会忘?\n", "\n", "虽然我的记忆不能跨对话永久保存,但只要咱们在这个对话里,您说的每句话、问的每个问题,我都记得一清二楚——包括您刚才已经问过一次“还记得我吗”,我当时也解释过啦。\n", "\n", "您现在又问一遍,是在考验我,还是想看看我是不是“脸盲”呀?哈哈,放心吧!在这段对话期间,您永远是我的大哥,随叫随到,有事您吩咐!😄\n" ] } ], "execution_count": 14 }, { "metadata": { "ExecuteTime": { "end_time": "2026-09-03T09:19:38.275536400Z", "start_time": "2026-09-03T09:19:31.090446900Z" } }, "cell_type": "code", "source": [ "messages.append(ChatCompletionUserMessageParam(role=\"user\", content=\"你好,明天合肥天气怎么样\"))\n", "\n", "response = client.chat.completions.create(\n", " model='deepseek-v4-flash',\n", " messages=messages,\n", " stream=False\n", ")\n", "print(response.choices[0].message.content)" ], "id": "ea34cc88d92b2832", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "你好!很抱歉,由于我无法实时联网获取最新的气象数据,所以不能直接为你提供明天合肥的准确天气预报。\n", "\n", "不过,你可以通过以下几种方式快速查到最准确的信息:\n", "\n", "1. 打开手机自带的“天气”应用,添加并定位到“合肥”。\n", "2. 在搜索引擎(如百度)中直接搜索“**合肥明天天气**”,或其他(例如“墨迹天气”)\n", "3. 访问“中国天气网”(www.weather.com.cn),输入“合肥”即可查询。\n", "\n", "另外,由于我无法预知你目前的实际日期,建议你在查询时留意一下,如果是明天出发,记得顺带看一眼**后天**的预报,以便更好地安排行程。祝生活愉快!\n" ] } ], "execution_count": 7 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }