Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

canvas_service.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import logging
  18. import time
  19. import traceback
  20. from uuid import uuid4
  21. from agent.canvas import Canvas
  22. from api.db import TenantPermission
  23. from api.db.db_models import DB, CanvasTemplate, User, UserCanvas, API4Conversation
  24. from api.db.services.api_service import API4ConversationService
  25. from api.db.services.common_service import CommonService
  26. from api.utils import get_uuid
  27. from api.utils.api_utils import get_data_openai
  28. import tiktoken
  29. from peewee import fn
  30. class CanvasTemplateService(CommonService):
  31. model = CanvasTemplate
  32. class UserCanvasService(CommonService):
  33. model = UserCanvas
  34. @classmethod
  35. @DB.connection_context()
  36. def get_list(cls, tenant_id,
  37. page_number, items_per_page, orderby, desc, id, title):
  38. agents = cls.model.select()
  39. if id:
  40. agents = agents.where(cls.model.id == id)
  41. if title:
  42. agents = agents.where(cls.model.title == title)
  43. agents = agents.where(cls.model.user_id == tenant_id)
  44. if desc:
  45. agents = agents.order_by(cls.model.getter_by(orderby).desc())
  46. else:
  47. agents = agents.order_by(cls.model.getter_by(orderby).asc())
  48. agents = agents.paginate(page_number, items_per_page)
  49. return list(agents.dicts())
  50. @classmethod
  51. @DB.connection_context()
  52. def get_by_tenant_id(cls, pid):
  53. try:
  54. fields = [
  55. cls.model.id,
  56. cls.model.avatar,
  57. cls.model.title,
  58. cls.model.dsl,
  59. cls.model.description,
  60. cls.model.permission,
  61. cls.model.update_time,
  62. cls.model.user_id,
  63. cls.model.create_time,
  64. cls.model.create_date,
  65. cls.model.update_date,
  66. User.nickname,
  67. User.avatar.alias('tenant_avatar'),
  68. ]
  69. agents = cls.model.select(*fields) \
  70. .join(User, on=(cls.model.user_id == User.id)) \
  71. .where(cls.model.id == pid)
  72. # obj = cls.model.query(id=pid)[0]
  73. return True, agents.dicts()[0]
  74. except Exception as e:
  75. logging.exception(e)
  76. return False, None
  77. @classmethod
  78. @DB.connection_context()
  79. def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
  80. page_number, items_per_page,
  81. orderby, desc, keywords,
  82. ):
  83. fields = [
  84. cls.model.id,
  85. cls.model.avatar,
  86. cls.model.title,
  87. cls.model.dsl,
  88. cls.model.description,
  89. cls.model.permission,
  90. User.nickname,
  91. User.avatar.alias('tenant_avatar'),
  92. cls.model.update_time
  93. ]
  94. if keywords:
  95. agents = cls.model.select(*fields).join(User, on=(cls.model.user_id == User.id)).where(
  96. ((cls.model.user_id.in_(joined_tenant_ids) & (cls.model.permission ==
  97. TenantPermission.TEAM.value)) | (
  98. cls.model.user_id == user_id)),
  99. (fn.LOWER(cls.model.title).contains(keywords.lower()))
  100. )
  101. else:
  102. agents = cls.model.select(*fields).join(User, on=(cls.model.user_id == User.id)).where(
  103. ((cls.model.user_id.in_(joined_tenant_ids) & (cls.model.permission ==
  104. TenantPermission.TEAM.value)) | (
  105. cls.model.user_id == user_id))
  106. )
  107. if desc:
  108. agents = agents.order_by(cls.model.getter_by(orderby).desc())
  109. else:
  110. agents = agents.order_by(cls.model.getter_by(orderby).asc())
  111. count = agents.count()
  112. agents = agents.paginate(page_number, items_per_page)
  113. return list(agents.dicts()), count
  114. def completion(tenant_id, agent_id, session_id=None, **kwargs):
  115. query = kwargs.get("query", "")
  116. files = kwargs.get("files", [])
  117. inputs = kwargs.get("inputs", {})
  118. user_id = kwargs.get("user_id", "")
  119. if session_id:
  120. e, conv = API4ConversationService.get_by_id(session_id)
  121. assert e, "Session not found!"
  122. if not conv.message:
  123. conv.message = []
  124. if not isinstance(conv.dsl, str):
  125. conv.dsl = json.dumps(conv.dsl, ensure_ascii=False)
  126. canvas = Canvas(conv.dsl, tenant_id, agent_id)
  127. else:
  128. e, cvs = UserCanvasService.get_by_id(agent_id)
  129. assert e, "Agent not found."
  130. assert cvs.user_id == tenant_id, "You do not own the agent."
  131. if not isinstance(cvs.dsl, str):
  132. cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
  133. session_id=get_uuid()
  134. canvas = Canvas(cvs.dsl, tenant_id, agent_id)
  135. canvas.reset()
  136. conv = {
  137. "id": session_id,
  138. "dialog_id": cvs.id,
  139. "user_id": user_id,
  140. "message": [],
  141. "source": "agent",
  142. "dsl": cvs.dsl
  143. }
  144. API4ConversationService.save(**conv)
  145. conv = API4Conversation(**conv)
  146. message_id = str(uuid4())
  147. conv.message.append({
  148. "role": "user",
  149. "content": query,
  150. "id": message_id
  151. })
  152. txt = ""
  153. for ans in canvas.run(query=query, files=files, user_id=user_id, inputs=inputs):
  154. ans["session_id"] = session_id
  155. if ans["event"] == "message":
  156. txt += ans["data"]["content"]
  157. yield "data:" + json.dumps(ans, ensure_ascii=False) + "\n\n"
  158. conv.message.append({"role": "assistant", "content": txt, "created_at": time.time(), "id": message_id})
  159. conv.reference = canvas.get_reference()
  160. conv.errors = canvas.error
  161. API4ConversationService.append_message(conv.id, conv.to_dict())
  162. def completionOpenAI(tenant_id, agent_id, question, session_id=None, stream=True, **kwargs):
  163. """Main function for OpenAI-compatible completions, structured similarly to the completion function."""
  164. tiktokenenc = tiktoken.get_encoding("cl100k_base")
  165. e, cvs = UserCanvasService.get_by_id(agent_id)
  166. if not e:
  167. yield get_data_openai(
  168. id=session_id,
  169. model=agent_id,
  170. content="**ERROR**: Agent not found."
  171. )
  172. return
  173. if cvs.user_id != tenant_id:
  174. yield get_data_openai(
  175. id=session_id,
  176. model=agent_id,
  177. content="**ERROR**: You do not own the agent"
  178. )
  179. return
  180. if not isinstance(cvs.dsl, str):
  181. cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
  182. canvas = Canvas(cvs.dsl, tenant_id)
  183. canvas.reset()
  184. message_id = str(uuid4())
  185. # Handle new session creation
  186. if not session_id:
  187. query = canvas.get_preset_param()
  188. if query:
  189. for ele in query:
  190. if not ele["optional"]:
  191. if not kwargs.get(ele["key"]):
  192. yield get_data_openai(
  193. id=None,
  194. model=agent_id,
  195. content=f"`{ele['key']}` is required",
  196. completion_tokens=len(tiktokenenc.encode(f"`{ele['key']}` is required")),
  197. prompt_tokens=len(tiktokenenc.encode(question if question else ""))
  198. )
  199. return
  200. ele["value"] = kwargs[ele["key"]]
  201. if ele["optional"]:
  202. if kwargs.get(ele["key"]):
  203. ele["value"] = kwargs[ele['key']]
  204. else:
  205. if "value" in ele:
  206. ele.pop("value")
  207. cvs.dsl = json.loads(str(canvas))
  208. session_id = get_uuid()
  209. conv = {
  210. "id": session_id,
  211. "dialog_id": cvs.id,
  212. "user_id": kwargs.get("user_id", "") if isinstance(kwargs, dict) else "",
  213. "message": [{"role": "assistant", "content": canvas.get_prologue(), "created_at": time.time()}],
  214. "source": "agent",
  215. "dsl": cvs.dsl
  216. }
  217. canvas.messages.append({"role": "user", "content": question, "id": message_id})
  218. canvas.add_user_input(question)
  219. API4ConversationService.save(**conv)
  220. conv = API4Conversation(**conv)
  221. if not conv.message:
  222. conv.message = []
  223. conv.message.append({
  224. "role": "user",
  225. "content": question,
  226. "id": message_id
  227. })
  228. if not conv.reference:
  229. conv.reference = []
  230. conv.reference.append({"chunks": [], "doc_aggs": []})
  231. # Handle existing session
  232. else:
  233. e, conv = API4ConversationService.get_by_id(session_id)
  234. if not e:
  235. yield get_data_openai(
  236. id=session_id,
  237. model=agent_id,
  238. content="**ERROR**: Session not found!"
  239. )
  240. return
  241. canvas = Canvas(json.dumps(conv.dsl), tenant_id)
  242. canvas.messages.append({"role": "user", "content": question, "id": message_id})
  243. canvas.add_user_input(question)
  244. if not conv.message:
  245. conv.message = []
  246. conv.message.append({
  247. "role": "user",
  248. "content": question,
  249. "id": message_id
  250. })
  251. if not conv.reference:
  252. conv.reference = []
  253. conv.reference.append({"chunks": [], "doc_aggs": []})
  254. # Process request based on stream mode
  255. final_ans = {"reference": [], "content": ""}
  256. prompt_tokens = len(tiktokenenc.encode(str(question)))
  257. if stream:
  258. try:
  259. completion_tokens = 0
  260. for ans in canvas.run(stream=True, bypass_begin=True):
  261. if ans.get("running_status"):
  262. completion_tokens += len(tiktokenenc.encode(ans.get("content", "")))
  263. yield "data: " + json.dumps(
  264. get_data_openai(
  265. id=session_id,
  266. model=agent_id,
  267. content=ans["content"],
  268. object="chat.completion.chunk",
  269. completion_tokens=completion_tokens,
  270. prompt_tokens=prompt_tokens
  271. ),
  272. ensure_ascii=False
  273. ) + "\n\n"
  274. continue
  275. for k in ans.keys():
  276. final_ans[k] = ans[k]
  277. completion_tokens += len(tiktokenenc.encode(final_ans.get("content", "")))
  278. yield "data: " + json.dumps(
  279. get_data_openai(
  280. id=session_id,
  281. model=agent_id,
  282. content=final_ans["content"],
  283. object="chat.completion.chunk",
  284. finish_reason="stop",
  285. completion_tokens=completion_tokens,
  286. prompt_tokens=prompt_tokens
  287. ),
  288. ensure_ascii=False
  289. ) + "\n\n"
  290. # Update conversation
  291. canvas.messages.append({"role": "assistant", "content": final_ans["content"], "created_at": time.time(), "id": message_id})
  292. canvas.history.append(("assistant", final_ans["content"]))
  293. if final_ans.get("reference"):
  294. canvas.reference.append(final_ans["reference"])
  295. conv.dsl = json.loads(str(canvas))
  296. API4ConversationService.append_message(conv.id, conv.to_dict())
  297. yield "data: [DONE]\n\n"
  298. except Exception as e:
  299. traceback.print_exc()
  300. conv.dsl = json.loads(str(canvas))
  301. API4ConversationService.append_message(conv.id, conv.to_dict())
  302. yield "data: " + json.dumps(
  303. get_data_openai(
  304. id=session_id,
  305. model=agent_id,
  306. content="**ERROR**: " + str(e),
  307. finish_reason="stop",
  308. completion_tokens=len(tiktokenenc.encode("**ERROR**: " + str(e))),
  309. prompt_tokens=prompt_tokens
  310. ),
  311. ensure_ascii=False
  312. ) + "\n\n"
  313. yield "data: [DONE]\n\n"
  314. else: # Non-streaming mode
  315. try:
  316. all_answer_content = ""
  317. for answer in canvas.run(stream=False, bypass_begin=True):
  318. if answer.get("running_status"):
  319. continue
  320. final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else ""
  321. final_ans["reference"] = answer.get("reference", [])
  322. all_answer_content += final_ans["content"]
  323. final_ans["content"] = all_answer_content
  324. # Update conversation
  325. canvas.messages.append({"role": "assistant", "content": final_ans["content"], "created_at": time.time(), "id": message_id})
  326. canvas.history.append(("assistant", final_ans["content"]))
  327. if final_ans.get("reference"):
  328. canvas.reference.append(final_ans["reference"])
  329. conv.dsl = json.loads(str(canvas))
  330. API4ConversationService.append_message(conv.id, conv.to_dict())
  331. # Return the response in OpenAI format
  332. yield get_data_openai(
  333. id=session_id,
  334. model=agent_id,
  335. content=final_ans["content"],
  336. finish_reason="stop",
  337. completion_tokens=len(tiktokenenc.encode(final_ans["content"])),
  338. prompt_tokens=prompt_tokens,
  339. param=canvas.get_preset_param() # Added param info like in completion
  340. )
  341. except Exception as e:
  342. traceback.print_exc()
  343. conv.dsl = json.loads(str(canvas))
  344. API4ConversationService.append_message(conv.id, conv.to_dict())
  345. yield get_data_openai(
  346. id=session_id,
  347. model=agent_id,
  348. content="**ERROR**: " + str(e),
  349. finish_reason="stop",
  350. completion_tokens=len(tiktokenenc.encode("**ERROR**: " + str(e))),
  351. prompt_tokens=prompt_tokens
  352. )