選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

use-llm-request.ts 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { LlmModelType } from '@/constants/knowledge';
  2. import userService from '@/services/user-service';
  3. import { useQuery } from '@tanstack/react-query';
  4. import {
  5. IThirdOAIModelCollection as IThirdAiModelCollection,
  6. IThirdOAIModel,
  7. } from '@/interfaces/database/llm';
  8. import { buildLlmUuid } from '@/utils/llm-util';
  9. export const useFetchLlmList = (modelType?: LlmModelType) => {
  10. const { data } = useQuery<IThirdAiModelCollection>({
  11. queryKey: ['llmList'],
  12. initialData: {},
  13. queryFn: async () => {
  14. const { data } = await userService.llm_list({ model_type: modelType });
  15. return data?.data ?? {};
  16. },
  17. });
  18. return data;
  19. };
  20. type IThirdOAIModelWithUuid = IThirdOAIModel & { uuid: string };
  21. export function useSelectFlatLlmList(modelType?: LlmModelType) {
  22. const llmList = useFetchLlmList(modelType);
  23. return Object.values(llmList).reduce<IThirdOAIModelWithUuid[]>((pre, cur) => {
  24. pre.push(...cur.map((x) => ({ ...x, uuid: buildLlmUuid(x) })));
  25. return pre;
  26. }, []);
  27. }
  28. export function useFindLlmByUuid(modelType?: LlmModelType) {
  29. const flatList = useSelectFlatLlmList(modelType);
  30. return (uuid: string) => {
  31. return flatList.find((x) => x.uuid === uuid);
  32. };
  33. }