Pārlūkot izejas kodu

Merge pull request #308 from wensname/fix-chapter7-bug

Fix:修复章节7.2.1流式输出重复打印问题,解决issue#220 issue#114
jjyaoao 5 mēneši atpakaļ
vecāks
revīzija
821bc389f4

+ 3 - 2
code/chapter7/my_main.py

@@ -17,5 +17,6 @@ response_stream = llm.think(messages)
 # 打印响应
 print("ModelScope Response:")
 for chunk in response_stream:
-    # chunk 已经是文本片段,可以直接使用
-    print(chunk, end="", flush=True)
+    # chunk在my_llm库中已经打印过一遍,这里只需要pass即可
+    # print(chunk, end="", flush=True)
+    pass

+ 3 - 2
docs/chapter7/Chapter7-Building-Your-Agent-Framework.md

@@ -259,8 +259,9 @@ response_stream = llm.think(messages)
 # Print response
 print("ModelScope Response:")
 for chunk in response_stream:
-    # chunk is already a text fragment, can be used directly
-    print(chunk, end="", flush=True)
+    # Chunk already printed in my_llm, just pass here
+    # print(chunk, end="", flush=True)
+    pass
 ```
 
 Through the above steps, we have successfully extended new functionality to the `hello-agents` library without modifying its source code. This method not only ensures code cleanliness and maintainability but also ensures that our customized functionality will not be lost when upgrading the `hello-agents` library in the future.

+ 3 - 2
docs/chapter7/第七章 构建你的Agent框架.md

@@ -259,8 +259,9 @@ response_stream = llm.think(messages)
 # 打印响应
 print("ModelScope Response:")
 for chunk in response_stream:
-    # chunk 已经是文本片段,可以直接使用
-    print(chunk, end="", flush=True)
+    # chunk在my_llm库中已经打印过一遍,这里只需要pass即可
+    # print(chunk, end="", flush=True)
+    pass
 ```
 
 通过以上步骤,我们就在不修改 `hello-agents` 库源码的前提下,成功为其扩展了新的功能。这种方法不仅保证了代码的整洁和可维护性,也使得未来升级 `hello-agents` 库时,我们的定制化功能不会丢失。