bot.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { bluebookAiAgent } from "@/xiaolanbenlib/api/index";
  2. import Taro from "@tarojs/taro";
  3. import { getSimpleHeader } from "@/xiaolanbenlib/module/axios.js";
  4. import JsonChunkParser from "@/utils/jsonChunkParser";
  5. import request from "@/xiaolanbenlib/module/axios.js";
  6. import type { TMessageHistories, TRequestBody, TAppendMessages } from "@/types/bot";
  7. import { TextDecoder } from "text-encoding-shim";
  8. import { getTempFileContent } from "@/utils";
  9. // 获取与指定智能体的历史会话记录--按智能体维度倒序返回
  10. export type TGetMessageHistoriesParams = {
  11. agentId: string;
  12. startId?: string; // 起始ID, 如果未传入则获取最新的N条
  13. pageSize: number;
  14. };
  15. export const getMessageHistories = (data: TGetMessageHistoriesParams) => {
  16. return request.get<TMessageHistories>(
  17. `${bluebookAiAgent}api/v1/chat/messages`,
  18. { params: data }
  19. );
  20. };
  21. // 保存消息--追加覆盖保存模式
  22. export const appendMessages = (data: TAppendMessages) => {
  23. return request.post(`${bluebookAiAgent}api/v1/chat/messages/append`, data)
  24. }
  25. // 消息差评或取消差评
  26. type TDislikeMessage = {
  27. agentId: string,
  28. dislikeReason: string,
  29. isDislike: boolean,
  30. msgUk: string
  31. loginId: string
  32. }
  33. export const dislikeMessage = (data: TDislikeMessage) => {
  34. return request.put(`${bluebookAiAgent}api/v1/chat/messages/dislike`, data)
  35. }
  36. // 消息点赞或取消点赞
  37. type TLikeMessage = {
  38. agentId: string,
  39. isLike: boolean,
  40. msgUk: string
  41. loginId: string
  42. }
  43. export const likeMessage = (data: TLikeMessage) => {
  44. return request.put(`${bluebookAiAgent}api/v1/chat/messages/like`, data)
  45. }
  46. export const speechToText = async (agentId: string, tempFilePath: string) => {
  47. const content = await getTempFileContent<string>(tempFilePath, 'base64')
  48. return request.post<{
  49. emotions: string[],
  50. text: string
  51. }>(`${bluebookAiAgent}api/v1/chat/speech/text`, {
  52. agentId,
  53. speechBase64: content,
  54. })
  55. }
  56. // 文本聊天,流式返回消息
  57. export type TTextChatParams = {
  58. params: TRequestBody;
  59. onStart: () => void;
  60. onReceived: (m: { content: string; reasoningContent: string }) => void;
  61. onFinished: () => void;
  62. onError: () => void;
  63. };
  64. export const textChat = ({
  65. params,
  66. onStart,
  67. onReceived,
  68. onFinished,
  69. onError,
  70. }: TTextChatParams) => {
  71. onStart();
  72. let reqTask: Taro.RequestTask<any>;
  73. const jsonParser = new JsonChunkParser();
  74. jsonParser.onParseComplete((m) => {
  75. onFinished();
  76. });
  77. const onChunkReceived = (chunk: any) => {
  78. console.log('chunkReceived: ', chunk);
  79. const uint8Array = new Uint8Array(chunk.data);
  80. console.log('uint8Array: ', uint8Array);
  81. var string = new TextDecoder("utf-8").decode(uint8Array);
  82. console.log(string);
  83. jsonParser.parseChunk(string, (m) => {
  84. console.log('parseChunk', m);
  85. onReceived(m);
  86. });
  87. };
  88. const header = getSimpleHeader()
  89. try {
  90. const url = `${bluebookAiAgent}api/v1/chat/completions`;
  91. reqTask = Taro.request({
  92. url: url,
  93. data: params,
  94. enableChunked: true,
  95. method: "POST",
  96. header: {
  97. ...header
  98. },
  99. responseType: "arraybuffer",
  100. success: function (res) {
  101. console.log("服务端响应 >>", res);
  102. },
  103. complete: function(res) {
  104. console.log(res,4444)
  105. }
  106. });
  107. // reqTask.
  108. reqTask.onChunkReceived(onChunkReceived);
  109. } catch (e) {
  110. onError();
  111. }
  112. const stopChunk = () => {
  113. reqTask.offChunkReceived(onChunkReceived);
  114. };
  115. return stopChunk;
  116. };