让Deepseek自己和自己对话(通过API)

注册Deepseek 开放平台之后,我发现居然有¥10的赠送余额。同时,截止本文写作时,Deepseek V3的价格便宜的离谱,大家自己看吧。

模型上下文长度最大思维链长度最大输出长度百万tokens
输入价格
(缓存命中)
百万tokens
输入价格
(缓存未命中)
百万tokens
输出价格
输出价格
deepseek-chat64K8K0.5元
0.1元
2元
1元
8元
2元
deepseek-reasoner64K32K8K1元4元16元

这样的话,送的10元余额可有的玩了,那么怎么才能把这些余额快速消耗掉呢(bushi)?那必然是自动对话了。

from openai import OpenAI
import os

# AI configuration
log_path = "./conversation_log.txt"
with open(log_path, 'w', encoding='utf-8') as chat_history:
    chat_history.truncate(0)

client = OpenAI(api_key="<api_key>")

# Character set
first_character = [
        {"role": "system", "content": "角色1设定"},
    ]

second_character = [
        {"role": "system", "content": "角色2设定"},
    ]

# Input the first message as the second character
new_message = input()
with open(log_path, 'a', encoding='utf-8') as chat_history:
    chat_history.write(f"<user_input>\n{new_message}\n<user_input_end>\n\n")
first_character.append({"role": "user", "content": new_message},)

var = 1
while var==1:
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=first_character,
            stream=False
        )

        first_character.append({"role": "assistant", "content": response.choices[0].message.content},)
        print(response.choices[0].message.content)
        with open(log_path, 'a', encoding='utf-8') as chat_history:
            chat_history.write(f"<character_1>\n{response.choices[0].message.content}\n<character_1_end>\n\n")

        second_character.append({"role": "user", "content": response.choices[0].message.content}, )
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=second_character,
            stream=False
        )

        second_character.append({"role": "assistant", "content": response.choices[0].message.content}, )
        print(response.choices[0].message.content)
        first_character.append({"role": "user", "content": response.choices[0].message.content}, )
        with open(log_path, 'a', encoding='utf-8') as chat_history:
            chat_history.write(f"<character_2>\n{response.choices[0].message.content}\n<character_2_end>\n\n")

只需要提前在目录下准备一个空的 conversation_log.txt 就可以记录对话内容了,不过每次程序运行时这个文件都会被清空,因此要记得保存。

使用例: 让Deepseek自己问出Win10激活码(以及cosplay,哈哈)
对话历史记录文件

嗯,那很好玩了……