小甲鱼 发表于 2025-2-17 19:38:55

已有 5 人购买  本主题需向作者支付 10 鱼币 才能浏览 购买主题

不二如是 发表于 2025-2-18 08:23:48

{:10_256:}{:10_256:}学习!!!流程图好赞

sunshine_8205 发表于 2025-2-18 09:38:57

{:5_106:}

Stubborn 发表于 2025-2-19 15:17:38

{:10_277:}

Stubborn 发表于 2025-2-19 15:55:06

非常好用,快去试试吧{:10_254:}
from openai import OpenAI

MASSAGES = []
CLIENT = OpenAI(api_key="", base_url="https://api.siliconflow.cn/v1")# 将 api_key 替换成你自己的


def stream_deepseek_answer():
    """一轮对话"""
    while True:
      try:

            contents = input("输入你的问题:\n")
            MASSAGES.append({"role": "user", "content": contents})

            stream = CLIENT.chat.completions.create(

                model="Pro/deepseek-ai/DeepSeek-R1",

                messages=MASSAGES,

                temperature=1,

                stream=True

            )

            reasoning_buffer = ""

            content_buffer = ""

            has_printed_thinking = False# 控制思考过程分隔线

            has_printed_answer = False# 控制正式回答分隔线

            print("\n=== DeepSeek的答案 ===")

            for chunk in stream:

                # 处理思考过程

                if hasattr(chunk.choices.delta, 'reasoning_content'):

                  reasoning = chunk.choices.delta.reasoning_content or ""

                  reasoning_buffer += reasoning

                  # 首次收到思考内容时打印分隔线

                  if not has_printed_thinking and reasoning.strip():
                        print("-------- Think -------->")

                        has_printed_thinking = True

                  # 实时打印思考内容(替换换行为空格保持流畅)

                  print(reasoning, end="", flush=True)

                # 处理正式回答

                if hasattr(chunk.choices.delta, 'content'):

                  contents = chunk.choices.delta.content or ""

                  content_buffer += contents

                  # 首次收到正式内容时打印分隔线

                  if not has_printed_answer and contents.strip():
                        print("<------- Think ---------")

                        has_printed_answer = True

                  # 实时打印正式内容

                  print(contents, end="", flush=True)

            print("\n========================")

      except Exception as e:
            print(f"发生错误: {e}\n服务器可能过载,请稍后再试。")


if __name__ == "__main__":
    stream_deepseek_answer()

快速收敛 发表于 2025-2-19 17:04:16

{:5_109:}

小甲鱼的二师兄 发表于 2025-2-19 18:01:41

越来越高级了?

科007 发表于 2025-2-20 17:49:47

学到了

神荼Q 发表于 2025-2-20 17:50:09

流程图大大提升了可阅读性

xiehongzhe 发表于 2025-2-20 17:50:31

爱了爱了,正想弄一个

clollipops 发表于 2025-2-20 17:50:35

学海无涯

格子penbeat 发表于 2025-2-20 17:51:05

爱了爱了

生生不息的鲸落 发表于 2025-2-20 17:51:12

挺厉害的

Collider-M31 发表于 2025-2-20 17:52:28

这么强?

Eric_1891574 发表于 2025-2-20 17:53:14

API 需要花钱才能搞到是吧

Referee_ZL 发表于 2025-2-20 17:54:02

好东西,赶紧收藏,抓紧去试试!

Ming-02 发表于 2025-2-20 17:54:25

强啊

Referee_ZL 发表于 2025-2-20 17:55:01

好东西,第一时间收藏!抓紧去试试!赞!

Pioneer. 发表于 2025-2-20 17:55:59

学习到了

每天提高一点点 发表于 2025-2-20 17:56:34

谢谢谢谢谢谢谢谢
页: [1] 2 3 4
查看完整版本: Python | Day 5:AI 时代是否还需要学习编程,使用流式输出(★★☆)