Upload free_ask_internet.py
Browse files- free_ask_internet.py +27 -6
free_ask_internet.py
CHANGED
@@ -222,17 +222,39 @@ def chat(prompt, model:str, stream=True, debug=False):
|
|
222 |
|
223 |
|
224 |
def ask_internet(query:str, model:str, debug=False):
|
225 |
-
|
226 |
content_list = search_web_ref(query,debug=debug)
|
227 |
if debug:
|
228 |
print(content_list)
|
229 |
prompt = gen_prompt(query,content_list,context_length_limit=6000,debug=debug)
|
230 |
-
total_token =
|
231 |
-
|
|
|
|
|
232 |
for token in chat(prompt=prompt, model=model):
|
233 |
if token:
|
234 |
total_token += token
|
|
|
235 |
yield token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
yield "\n\n"
|
237 |
# 是否返回参考资料
|
238 |
if True:
|
@@ -242,7 +264,6 @@ def ask_internet(query:str, model:str, debug=False):
|
|
242 |
count = 1
|
243 |
for url_content in content_list:
|
244 |
url = url_content.get('url')
|
245 |
-
yield "*[{}. {}]({})*".format(str(count),url,url
|
246 |
yield "\n"
|
247 |
-
count += 1
|
248 |
-
|
|
|
222 |
|
223 |
|
224 |
def ask_internet(query:str, model:str, debug=False):
|
|
|
225 |
content_list = search_web_ref(query,debug=debug)
|
226 |
if debug:
|
227 |
print(content_list)
|
228 |
prompt = gen_prompt(query,content_list,context_length_limit=6000,debug=debug)
|
229 |
+
total_token = ""
|
230 |
+
|
231 |
+
# 收集所有回答内容
|
232 |
+
response_content = ""
|
233 |
for token in chat(prompt=prompt, model=model):
|
234 |
if token:
|
235 |
total_token += token
|
236 |
+
response_content += token
|
237 |
yield token
|
238 |
+
|
239 |
+
# 处理引用链接
|
240 |
+
if content_list:
|
241 |
+
# 创建引用到URL的映射
|
242 |
+
citation_map = {f"[citation:{i+1}]": content_list[i].get('url')
|
243 |
+
for i in range(len(content_list))}
|
244 |
+
|
245 |
+
# 替换所有引用为链接
|
246 |
+
modified_content = response_content
|
247 |
+
for citation, url in citation_map.items():
|
248 |
+
if url:
|
249 |
+
modified_content = modified_content.replace(
|
250 |
+
citation,
|
251 |
+
f"[{citation}]({url})"
|
252 |
+
)
|
253 |
+
|
254 |
+
# 输出修改后的内容
|
255 |
+
yield "\n\n修改后的引用链接:\n"
|
256 |
+
yield modified_content
|
257 |
+
|
258 |
yield "\n\n"
|
259 |
# 是否返回参考资料
|
260 |
if True:
|
|
|
264 |
count = 1
|
265 |
for url_content in content_list:
|
266 |
url = url_content.get('url')
|
267 |
+
yield "*[{}. {}]({})*".format(str(count),url,url)
|
268 |
yield "\n"
|
269 |
+
count += 1
|
|