0-zsk.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // ==UserScript==
  2. // @name 知识库
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 利用知识库回答问题
  6. // @author lyyyyy
  7. // @match http://127.0.0.1:17860/
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=0.1
  9. // @run-at document-idle
  10. // @grant none
  11. // ==/UserScript==
  12. get_title_form_md = (s) => {
  13. console.log(s)
  14. try {
  15. return s.match('\\[(.+)\\]')[1]
  16. } catch {
  17. return s
  18. }
  19. }
  20. get_url_form_md = (s) => {
  21. console.log(s)
  22. try {
  23. return s.match('\\((.+)\\)')[1]
  24. } catch {
  25. return s
  26. }
  27. }
  28. window.answer_with_zsk = async (Q) => {
  29. // lsdh(false)
  30. app.top_p = 0.2
  31. app.chat.push({ "role": "user", "content": Q })
  32. kownladge = (await find(Q, 5)).filter(i => !i.score || i.score < 120).map(i => ({
  33. title: get_title_form_md(i.title),
  34. url: get_url_form_md(i.title),
  35. content: i.content
  36. }))
  37. if (kownladge.length > 0) {
  38. answer = {
  39. role: "AI",
  40. content: "",
  41. sources: kownladge
  42. }
  43. app.chat.push(answer)
  44. result = []
  45. for (let i in kownladge) {
  46. answer.content = '正在查找:' + kownladge[i].title
  47. if (i > 3) continue
  48. let prompt = app.zsk_summarize_prompt + '\n' +
  49. kownladge[i].content + "\n问题:" + Q
  50. result.push(await send(prompt, keyword = Q, show = false))
  51. }
  52. app.chat.pop()
  53. app.chat.pop()
  54. let prompt = app.zsk_answer_prompt + '\n' +
  55. result.join('\n') + "\n问题:" + Q
  56. return await send(prompt, keyword = Q, show = true, sources = kownladge)
  57. } else {
  58. app.chat.pop()
  59. sources = [{
  60. title: '未匹配到知识库',
  61. content: '本次对话内容完全由模型提供'
  62. }]
  63. return await send(Q, keyword = Q, show = true, sources = sources)
  64. }
  65. }
  66. func.push({
  67. name: "知识库",
  68. description: "通过知识库回答问题",
  69. question: async (Q) => {
  70. answer_with_zsk(Q)
  71. }
  72. })
  73. window.answer_with_fast_zsk = async (Q) => {
  74. // lsdh(false)
  75. app.top_p = 0.2
  76. kownladge = (await find(Q, app.zsk_step)).filter(i => !i.score || i.score < 120).map(i => ({
  77. title: get_title_form_md(i.title),
  78. url: get_url_form_md(i.title),
  79. content: i.content
  80. }))
  81. if (kownladge.length > 0) {
  82. if (app.llm_type == "rwkv") {
  83. let prompt = 'raw!Instruction: 深刻理解下面提供的信息,根据信息完成问答。\n\nInput: ' +
  84. kownladge.map((e, i) => i + 1 + "." + e.content).join('\n') + "\n\nResponse: Question: " + Q + "\nAnswer: "
  85. return await send(prompt, keyword = Q, show = true, sources = kownladge,
  86. addition_args = { cfg_factor: app.cfg_factor, cfg_ctx: Q })
  87. } else {
  88. let prompt = app.zsk_answer_prompt + '\n' +
  89. kownladge.map((e, i) => i + 1 + "." + e.content).join('\n') + "\n问题:" + Q
  90. return await send(prompt, keyword = Q, show = true, sources = kownladge)
  91. }
  92. } else {
  93. app.chat.pop()
  94. sources = [{
  95. title: '未匹配到知识库',
  96. content: '本次对话内容完全由模型提供'
  97. }]
  98. return await send(Q, keyword = Q, show = true, sources = sources)
  99. }
  100. }
  101. func.push({
  102. name: "快速知识库",
  103. question: window.answer_with_fast_zsk
  104. }
  105. )
  106. func.push({
  107. name: "sgwx知识库全文爬取",
  108. question: async () => {
  109. let Q = app.question
  110. lsdh(true)//打开历史对话
  111. lsdh(false)
  112. app.chat.push({ "role": "user", "content": Q })
  113. kownladge = await find(Q, 2)
  114. app.chat.push({
  115. "role": "AI", "content": "识别结果:\n|标题|内容|\n|--|--|\n" +
  116. kownladge.map(i => "|" + i.title + "|" + i.content.replace(/\n/g, ' ') + "|").join("\n")
  117. })
  118. result = []
  119. for (let i in kownladge) {
  120. wx_response = await fetch("/api/read_sgwx", {
  121. method: 'post',
  122. body: JSON.stringify({
  123. url: kownladge[i].title.match(/\((.+)\)/)[1],
  124. }),
  125. headers: {
  126. 'Content-Type': 'application/json'
  127. }
  128. })
  129. let prompt = "精炼地总结以下文段中与问题相关的信息为二十个字。\n" + await wx_response.text() + "\n问题:" + Q
  130. result.push(await send(prompt))
  131. }
  132. let prompt = "根据以下资料,用中文回答问题。\n" +
  133. result.join('\n') + "\n问题:" + Q
  134. await send(prompt)
  135. },
  136. })
  137. // func.push({
  138. // name: "知识库step",
  139. // question: async () => {
  140. // let Q = app.question
  141. // app.chat.push({ "role": "user", "content": "步数为0" })
  142. // kownladge = await find(Q, 0)
  143. // kownladge=kownladge.map(i => i.content).join('\n\n').replace(/'/g,"")
  144. // app.chat.push({ "role": "AI", "content": kownladge })
  145. // app.chat.push({ "role": "user", "content": "步数为1" })
  146. // kownladge = await find(Q, 1)
  147. // kownladge=kownladge.map(i => i.content).join('\n\n').replace(/'/g,"")
  148. // app.chat.push({ "role": "AI", "content": kownladge })
  149. // app.chat.push({ "role": "user", "content": "步数为2" })
  150. // kownladge = await find(Q, 2)
  151. // kownladge=kownladge.map(i => i.content).join('\n\n').replace(/'/g,"")
  152. // app.chat.push({ "role": "AI", "content": kownladge })
  153. // },
  154. // })
粤ICP备19079148号