File size: 4,189 Bytes
79278ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { query } from "@/shared/api/query";

import axios from "axios";
import { parseDocumetnsInLlmAnswer } from "@/shared/utils/parseDocumnetsInLlmAnswer";
import {
  ChunkRequestType,
  ChunksResponseType,
  ChunksArrayItem,
  ChunkType,
  LlmRequestType,
  LlmResponseType,
  FeedbackRequestType,
  FeedbackResponseType,
  LogRequestType,
  LogResponseType,
  AbbreviationsDataType,
  AbbreviationsResponseDataType,
} from "./types";

export const getChunks = async (
  data: ChunkRequestType,
  signal: AbortSignal
): Promise<{ chunks: ChunksResponseType; chunksArray: ChunksArrayItem[] }> => {
  const response = await query<ChunksResponseType>({
    url: "/chunks",
    method: "post",
    data,
    signal,
  });

  if ("error" in response) {
    if (axios.isCancel(response.error)) {
      console.log("Запроса был отменен");
    }
    throw new Error(`Ошибка: ${response.error.status}`);
  }
  const chunksArray: ChunksArrayItem[] = [];

  // Проверки на null добавлены для корретной сквозной нумерации результатов в случае,
  // если чанков одного типа не пришло с бэка
  if (response.data.doc_chunks !== null) {
    response.data.doc_chunks?.forEach((element) => {
      chunksArray.push({
        type: ChunkType.doc_chunks,
        number: chunksArray.length,
        chunk: { doc_chunks: element },
      });
    });
  }

  if (response.data.people_search !== null) {
    response.data.people_search?.forEach((element) => {
      chunksArray.push({
        type: ChunkType.people_search,
        number: chunksArray.length,
        chunk: { people_search: element },
      });
    });
  }

  if (response.data.groups_search !== null) {
    chunksArray.push({
      type: ChunkType.groups_search,
      number: chunksArray.length,
      chunk: { groups_search: response.data.groups_search },
    });
  }

  if (response.data.rocks_nn_search !== null) {
    chunksArray.push({
      type: ChunkType.rocks_nn_search,
      number: chunksArray.length,
      chunk: { rocks_nn_search: response.data.rocks_nn_search },
    });
  }

  if (response.data.segmentation_search !== null) {
    chunksArray.push({
      type: ChunkType.segmentation_search,
      number: chunksArray.length,
      chunk: { segmentation_search: response.data.segmentation_search },
    });
  }

  return { chunks: response.data, chunksArray };
};

export const getLlmResponse = async (
  data: LlmRequestType,
  signal: AbortSignal
): Promise<LlmResponseType> => {
  const response = await query<LlmResponseType>({
    url: "/answer_llm",
    method: "post",
    data: { query: data.query, chunks: data.chunks },
    signal,
  });

  if ("error" in response) {
    if (axios.isCancel(response.error)) {
      console.log("Запроса был отменен");
    }
    throw new Error(`Ошибка: ${response.error.status}`);
  }
  const { matches, resultString } = parseDocumetnsInLlmAnswer(
    response.data.answer_llm,
    data.chunksArray
  );

  return {
    answer_llm: resultString,
    log_id: response.data.log_id,
    documentsNumbers: matches,
  };
};

export const postFeedback = async (
  data: FeedbackRequestType
): Promise<FeedbackResponseType> => {
  const response = await query<FeedbackResponseType>({
    url: "/feedback",
    method: "post",
    data,
  });

  if ("error" in response) {
    throw new Error(`Ошибка: ${response.error.status}`);
  }

  return response.data;
};

export const getLogs = async (
  data: LogRequestType | undefined
): Promise<LogResponseType[]> => {
  const response = await query<LogResponseType[]>({
    url: "/logs",
    method: "get",
    params: data,
  });
  if ("error" in response) {
    throw new Error(`Ошибка: ${response.error.status}`);
  }

  return response.data;
};

export const getAbbreviations = async (): Promise<AbbreviationsDataType> => {
  const response = await query<AbbreviationsResponseDataType>({
    url: "/collection/default",
    method: "get",
  });
  if ("error" in response) {
    throw new Error(`Ошибка: ${response.error.status}`);
  }

  return response.data.acronyms;
};