chatInputToChatCompletion.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { AudioContentPart, ChatCompletionInput, ImageContentPart, FileContentPart, TextInput } from './interface';
  2. export default function chatInputToChatCompletion(inputContent: any): ChatCompletionInput { // todo: 合入 aiChatInput 后补充类型定义
  3. const { references, attachments, inputContents, setup } = inputContent;
  4. let inputs: (ImageContentPart | AudioContentPart | FileContentPart | TextInput)[] = [];
  5. if (attachments?.length) { // todo: attachment 允许传递目录?
  6. attachments.forEach((item: any) => {
  7. const { name, url } = item;
  8. if (name.includes('.png') || name.includes('.jpg') || name.includes('.jpeg')) {
  9. inputs.push({
  10. type: 'image_url',
  11. image_url: {
  12. url: url,
  13. },
  14. });
  15. } else {
  16. // Inputs by file URL are not supported for chat completions. Use the ResponsesAPI for this option.
  17. inputs.push({
  18. type: 'file',
  19. file: {
  20. file_data: item.file_data,
  21. filename: name,
  22. file_id: item?.id,
  23. },
  24. });
  25. }
  26. });
  27. }
  28. if (inputContents?.length) {
  29. inputContents.forEach((item: any) => {
  30. inputs.push({
  31. type: 'text',
  32. text: item.text,
  33. });
  34. });
  35. }
  36. return {
  37. role: "user",
  38. messages: [{
  39. role: "user",
  40. content: inputs,
  41. }],
  42. model: setup?.model,
  43. references,
  44. setup: setup ?? {}
  45. };
  46. }