11import json
2+ import re
23
34import uuid_utils .compat as uuid
45from django .db .models import QuerySet
@@ -69,7 +70,7 @@ def call_tool(self, params):
6970
7071 payload = {
7172 'message' : args .get ('message' ),
72- 'stream' : False ,
73+ 'stream' : True ,
7374 're_chat' : False
7475 }
7576 resp = ChatSerializers (data = {
@@ -81,6 +82,24 @@ def call_tool(self, params):
8182 'source' : {"type" : ChatSourceChoices .ONLINE .value },
8283 'debug' : False ,
8384 }).chat (payload )
84- data = json .loads (str (resp .text ))
85+ chunks = []
86+ for raw_line in resp :
87+ line = raw_line .decode ("utf-8" , errors = "replace" ).rstrip ("\r \n " )
88+ if not line .startswith ("data:" ):
89+ continue
90+ payload = line [5 :].strip ()
91+ if not payload or payload == "[DONE]" :
92+ continue
93+ try :
94+ event = json .loads (payload )
95+ except json .JSONDecodeError :
96+ continue
97+ if event .get ("operate" ) is True :
98+ chunks .append (event .get ("content" , "" ))
99+ if event .get ("is_end" ):
100+ break
85101
86- return {"content" : [{"type" : "text" , "text" : data .get ('data' , {}).get ('content' )}]}
102+ data = '' .join (chunks )
103+ # 排除<tool_calls_render></tool_calls_render>标签
104+ data = re .sub (r'<tool_calls_render>.*?</tool_calls_render>' , '' , data , flags = re .DOTALL )
105+ return {"content" : [{"type" : "text" , "text" : data }]}
0 commit comments