ai 2.2.29 → 2.2.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +310 -130
- package/dist/index.js +705 -554
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +706 -556
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -4
- package/prompts/dist/index.d.ts +23 -2
- package/prompts/dist/index.js +14 -0
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs +14 -0
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.ts +92 -10
- package/react/dist/index.js +121 -11
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +121 -11
- package/react/dist/index.mjs.map +1 -1
- package/solid/dist/index.d.ts +61 -6
- package/solid/dist/index.js +136 -21
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +136 -21
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.ts +52 -3
- package/svelte/dist/index.js +163 -25
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +163 -25
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.ts +60 -5
- package/vue/dist/index.js +135 -20
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +135 -20
- package/vue/dist/index.mjs.map +1 -1
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../index.ts","../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["export * from './anthropic';\nexport * from './huggingface';\nexport * from './openai';\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart> | null;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Specifies the detail level of the image.\n */\n detail?: 'auto' | 'low' | 'high';\n\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url?: string;\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The contents of the assistant message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,SAAS,kCACd,UACA;AACA,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc;AAAA,IACvB,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB;AAAA,IAC3B;AAAA,EACF,CAAC,IAAI;AAET;;;ACZO,SAAS,qCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA,EAAa;AAAA;AAAA,IACtB,WAAW,SAAS,aAAa;AAC/B,aAAO;AAAA,EAAkB;AAAA;AAAA,IAC3B,WAAW,SAAS,UAAU;AAC5B,aAAO;AAAA,EAAe;AAAA;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE;AAAA,EACF,CAAC,EACA,KAAK,EAAE,IAAI;AAElB;AAOO,SAAS,sCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO,eAAe;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE,WAAW,SAAS,UAAU;AAC5B,YAAM,IAAI,MAAM,iDAAiD;AAAA,IACnE,OAA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;AAAA,QACrE;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,MAAM,QAAQ;AAAA,QAChB;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,cAAM;AAAA,MACR;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
1
|
+
{"version":3,"sources":["../index.ts","../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["export * from './anthropic';\nexport * from './huggingface';\nexport * from './openai';\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n\n case 'tool': {\n if (message.name === undefined) {\n throw new Error('Invalid tool message. Expected a name');\n }\n\n if (message.tool_call_id === undefined) {\n throw new Error('Invalid tool message. Expected a tool_call_id');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n tool_call_id: message.tool_call_id,\n } satisfies ChatCompletionMessageParam;\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart> | null;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Specifies the detail level of the image.\n */\n detail?: 'auto' | 'low' | 'high';\n\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url?: string;\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The contents of the assistant message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * The name of the function that this message is responding to.\n */\n\n name: string;\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,SAAS,kCACd,UACA;AACA,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S,QAAQ;AAAA,UACjB,MAAM,QAAQ;AAAA,UACd,cAAc,QAAQ;AAAA,QACxB;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/prompts/dist/index.mjs
CHANGED
@@ -108,6 +108,20 @@ function experimental_buildOpenAIMessages(messages) {
|
|
108
108
|
case "data": {
|
109
109
|
throw "unsupported message role 'data'";
|
110
110
|
}
|
111
|
+
case "tool": {
|
112
|
+
if (message.name === void 0) {
|
113
|
+
throw new Error("Invalid tool message. Expected a name");
|
114
|
+
}
|
115
|
+
if (message.tool_call_id === void 0) {
|
116
|
+
throw new Error("Invalid tool message. Expected a tool_call_id");
|
117
|
+
}
|
118
|
+
return {
|
119
|
+
role: message.role,
|
120
|
+
content: message.content,
|
121
|
+
name: message.name,
|
122
|
+
tool_call_id: message.tool_call_id
|
123
|
+
};
|
124
|
+
}
|
111
125
|
}
|
112
126
|
});
|
113
127
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart> | null;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Specifies the detail level of the image.\n */\n detail?: 'auto' | 'low' | 'high';\n\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url?: string;\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The contents of the assistant message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":";AAOO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5D,WAAW,SAAS,YAAY,UAAU,GAAG;AAC3C,aAAO;AAAA,EAAY;AAAA;AAAA;AAAA;AAAA,IACrB,OAAO;AACL,YAAM,IAAI,MAAM,yBAAyB,MAAM;AAAA,IACjD;AAAA,EACF,CAAC;AAED,SAAO,cAAc,aAAa,KAAK,EAAE,IAAI;AAC/C;;;AC3EO,SAAS,iCACd,UAC8B;AAC9B,SAAO,SAAS,IAAI,aAAW;AAC7B,YAAQ,QAAQ,MAAM;AAAA,MACpB,KAAK;AAAA,MACL,KAAK;AACH,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,QACnB;AAAA,MAEF,KAAK,aAAa;AAChB,cAAM,gBAAgB,QAAQ;AAE9B,YACE,kBAAkB,WACjB,OAAO,kBAAkB,YACxB,cAAc,cAAc,UAC5B,cAAc,SAAS,SACzB;AACA,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,eACE,kBAAkB,SACd,SACA;AAAA,YACE,MAAM,cAAc;AAAA,YACpB,WAAW,cAAc;AAAA,UAC3B;AAAA,QACR;AAAA,MACF;AAAA,MAEA,KAAK,YAAY;AACf,YAAI,QAAQ,SAAS,QAAW;AAC9B,gBAAM,IAAI,MAAM,mDAAmD;AAAA,QACrE;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,MAAM,QAAQ;AAAA,QAChB;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,cAAM;AAAA,MACR;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
1
|
+
{"version":3,"sources":["../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n\n case 'tool': {\n if (message.name === undefined) {\n throw new Error('Invalid tool message. Expected a name');\n }\n\n if (message.tool_call_id === undefined) {\n throw new Error('Invalid tool message. Expected a tool_call_id');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n tool_call_id: message.tool_call_id,\n } satisfies ChatCompletionMessageParam;\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart> | null;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Specifies the detail level of the image.\n */\n detail?: 'auto' | 'low' | 'high';\n\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url?: string;\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The contents of the assistant message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * Deprecated and replaced by `tool_calls`. The name and arguments of a function\n * that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string | null;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * The name of the function that this message is responding to.\n */\n\n name: string;\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":";AAOO,SAAS,kCACd,UACA;AACA,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc;AAAA,IACvB,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB;AAAA,IAC3B;AAAA,EACF,CAAC,IAAI;AAET;;;ACZO,SAAS,qCACd,UACA;AACA,SACE,SACG,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAC1B,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA,EAAa;AAAA;AAAA,IACtB,WAAW,SAAS,aAAa;AAC/B,aAAO;AAAA,EAAkB;AAAA;AAAA,IAC3B,WAAW,SAAS,UAAU;AAC5B,aAAO;AAAA,EAAe;AAAA;AAAA,IACxB,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,g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AAS,SACzB;AACA,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,eACE,kBAAkB,SACd,SACA;AAAA,YACE,MAAM,cAAc;AAAA,YACpB,WAAW,cAAc;AAAA,UAC3B;AAAA,QACR;AAAA,MACF;AAAA,MAEA,KAAK,YAAY;AACf,YAAI,QAAQ,SAAS,QAAW;AAC9B,gBAAM,IAAI,MAAM,mDAAmD;AAAA,QACrE;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,MAAM,QAAQ;AAAA,QAChB;AAAA,MACF;AAAA,MAEA,KAAK,QAAQ;AACX,cAAM;AAAA,MACR;AAAA,MAEA,KAAK,QAAQ;AACX,YAAI,QAAQ,SAAS,QAAW;AAC9B,gBAAM,IAAI,MAAM,uCAAuC;AAAA,QACzD;AAEA,YAAI,QAAQ,iBAAiB,QAAW;AACtC,gBAAM,IAAI,MAAM,+CAA+C;AAAA,QACjE;AAEA,eAAO;AAAA,UACL,MAAM,QAAQ;AAAA,UACd,SAAS,QAAQ;AAAA,UACjB,MAAM,QAAQ;AAAA,UACd,cAAc,QAAQ;AAAA,QACxB;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/react/dist/index.d.ts
CHANGED
@@ -11,6 +11,38 @@ interface FunctionCall {
|
|
11
11
|
*/
|
12
12
|
name?: string;
|
13
13
|
}
|
14
|
+
/**
|
15
|
+
* The tool calls generated by the model, such as function calls.
|
16
|
+
*/
|
17
|
+
interface ToolCall {
|
18
|
+
id: string;
|
19
|
+
type: string;
|
20
|
+
function: {
|
21
|
+
name: string;
|
22
|
+
arguments: string;
|
23
|
+
};
|
24
|
+
}
|
25
|
+
/**
|
26
|
+
* Controls which (if any) function is called by the model.
|
27
|
+
* - none means the model will not call a function and instead generates a message.
|
28
|
+
* - auto means the model can pick between generating a message or calling a function.
|
29
|
+
* - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
|
30
|
+
* none is the default when no functions are present. auto is the default if functions are present.
|
31
|
+
*/
|
32
|
+
type ToolChoice = 'none' | 'auto' | {
|
33
|
+
type: 'function';
|
34
|
+
function: {
|
35
|
+
name: string;
|
36
|
+
};
|
37
|
+
};
|
38
|
+
/**
|
39
|
+
* A list of tools the model may call. Currently, only functions are supported as a tool.
|
40
|
+
* Use this to provide a list of functions the model may generate JSON inputs for.
|
41
|
+
*/
|
42
|
+
interface Tool {
|
43
|
+
type: 'function';
|
44
|
+
function: Function;
|
45
|
+
}
|
14
46
|
interface Function {
|
15
47
|
/**
|
16
48
|
* The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
|
@@ -39,10 +71,11 @@ type IdGenerator = () => string;
|
|
39
71
|
*/
|
40
72
|
interface Message {
|
41
73
|
id: string;
|
74
|
+
tool_call_id?: string;
|
42
75
|
createdAt?: Date;
|
43
76
|
content: string;
|
44
77
|
ui?: string | JSX.Element | JSX.Element[] | null | undefined;
|
45
|
-
role: 'system' | 'user' | 'assistant' | 'function' | 'data';
|
78
|
+
role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
|
46
79
|
/**
|
47
80
|
* If the message has a role of `function`, the `name` field is the name of the function.
|
48
81
|
* Otherwise, the name field should not be set.
|
@@ -51,10 +84,15 @@ interface Message {
|
|
51
84
|
/**
|
52
85
|
* If the assistant role makes a function call, the `function_call` field
|
53
86
|
* contains the function call name and arguments. Otherwise, the field should
|
54
|
-
* not be set.
|
87
|
+
* not be set. (Deprecated and replaced by tool_calls.)
|
55
88
|
*/
|
56
89
|
function_call?: string | FunctionCall;
|
57
90
|
data?: JSONValue;
|
91
|
+
/**
|
92
|
+
* If the assistant role makes a tool call, the `tool_calls` field contains
|
93
|
+
* the tool call name and arguments. Otherwise, the field should not be set.
|
94
|
+
*/
|
95
|
+
tool_calls?: string | ToolCall[];
|
58
96
|
}
|
59
97
|
type CreateMessage = Omit<Message, 'id'> & {
|
60
98
|
id?: Message['id'];
|
@@ -65,8 +103,11 @@ type ChatRequest = {
|
|
65
103
|
functions?: Array<Function>;
|
66
104
|
function_call?: FunctionCall;
|
67
105
|
data?: Record<string, string>;
|
106
|
+
tools?: Array<Tool>;
|
107
|
+
tool_choice?: ToolChoice;
|
68
108
|
};
|
69
109
|
type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
|
110
|
+
type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
|
70
111
|
type RequestOptions = {
|
71
112
|
headers?: Record<string, string> | Headers;
|
72
113
|
body?: object;
|
@@ -75,6 +116,8 @@ type ChatRequestOptions = {
|
|
75
116
|
options?: RequestOptions;
|
76
117
|
functions?: Array<Function>;
|
77
118
|
function_call?: FunctionCall;
|
119
|
+
tools?: Array<Tool>;
|
120
|
+
tool_choice?: ToolChoice;
|
78
121
|
data?: Record<string, string>;
|
79
122
|
};
|
80
123
|
type UseChatOptions = {
|
@@ -103,6 +146,12 @@ type UseChatOptions = {
|
|
103
146
|
* automatically to the API and will be used to update the chat.
|
104
147
|
*/
|
105
148
|
experimental_onFunctionCall?: FunctionCallHandler;
|
149
|
+
/**
|
150
|
+
* Callback function to be called when a tool call is received.
|
151
|
+
* If the function returns a `ChatRequest` object, the request will be sent
|
152
|
+
* automatically to the API and will be used to update the chat.
|
153
|
+
*/
|
154
|
+
experimental_onToolCall?: ToolCallHandler;
|
106
155
|
/**
|
107
156
|
* Callback function to be called when the API response is received.
|
108
157
|
*/
|
@@ -285,7 +334,7 @@ type UseChatHelpers = {
|
|
285
334
|
setInput: React.Dispatch<React.SetStateAction<string>>;
|
286
335
|
/** An input/textarea-ready onChange handler to control the value of the input */
|
287
336
|
handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
|
288
|
-
/** Form submission handler to automatically reset input and append a user message
|
337
|
+
/** Form submission handler to automatically reset input and append a user message */
|
289
338
|
handleSubmit: (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void;
|
290
339
|
metadata?: Object;
|
291
340
|
/** Whether the API request is in progress */
|
@@ -350,28 +399,61 @@ declare function useCompletion({ api, id, initialCompletion, initialInput, crede
|
|
350
399
|
|
351
400
|
type AssistantStatus = 'in_progress' | 'awaiting_message';
|
352
401
|
type UseAssistantHelpers = {
|
353
|
-
/**
|
402
|
+
/**
|
403
|
+
* The current array of chat messages.
|
404
|
+
*/
|
354
405
|
messages: Message[];
|
355
|
-
/**
|
406
|
+
/**
|
407
|
+
* The current thread ID.
|
408
|
+
*/
|
356
409
|
threadId: string | undefined;
|
357
|
-
/**
|
410
|
+
/**
|
411
|
+
* The current value of the input field.
|
412
|
+
*/
|
358
413
|
input: string;
|
359
|
-
/**
|
414
|
+
/**
|
415
|
+
* Handler for the `onChange` event of the input field to control the input's value.
|
416
|
+
*/
|
360
417
|
handleInputChange: (event: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
|
361
|
-
/**
|
418
|
+
/**
|
419
|
+
* Form submission handler that automatically resets the input field and appends a user message.
|
420
|
+
*/
|
362
421
|
submitMessage: (event?: React.FormEvent<HTMLFormElement>, requestOptions?: {
|
363
422
|
data?: Record<string, string>;
|
364
423
|
}) => Promise<void>;
|
365
|
-
/**
|
424
|
+
/**
|
425
|
+
* The current status of the assistant. This can be used to show a loading indicator.
|
426
|
+
*/
|
366
427
|
status: AssistantStatus;
|
367
|
-
/**
|
428
|
+
/**
|
429
|
+
* The error thrown during the assistant message processing, if any.
|
430
|
+
*/
|
368
431
|
error: undefined | unknown;
|
369
432
|
};
|
370
433
|
type UseAssistantOptions = {
|
434
|
+
/**
|
435
|
+
* The API endpoint that accepts a `{ threadId: string | null; message: string; }` object and returns an `AssistantResponse` stream.
|
436
|
+
* The threadId refers to an existing thread with messages (or is `null` to create a new thread).
|
437
|
+
* The message is the next message that should be appended to the thread and sent to the assistant.
|
438
|
+
*/
|
371
439
|
api: string;
|
440
|
+
/**
|
441
|
+
* An optional string that represents the ID of an existing thread.
|
442
|
+
* If not provided, a new thread will be created.
|
443
|
+
*/
|
372
444
|
threadId?: string | undefined;
|
445
|
+
/**
|
446
|
+
* An optional literal that sets the mode of credentials to be used on the request.
|
447
|
+
* Defaults to "same-origin".
|
448
|
+
*/
|
373
449
|
credentials?: RequestCredentials;
|
450
|
+
/**
|
451
|
+
* An optional object of headers to be passed to the API endpoint.
|
452
|
+
*/
|
374
453
|
headers?: Record<string, string> | Headers;
|
454
|
+
/**
|
455
|
+
* An optional, additional body object to be passed to the API endpoint.
|
456
|
+
*/
|
375
457
|
body?: object;
|
376
458
|
};
|
377
459
|
declare function experimental_useAssistant({ api, threadId: threadIdParam, credentials, headers, body, }: UseAssistantOptions): UseAssistantHelpers;
|
package/react/dist/index.js
CHANGED
@@ -137,6 +137,23 @@ var dataMessageStreamPart = {
|
|
137
137
|
};
|
138
138
|
}
|
139
139
|
};
|
140
|
+
var toolCallStreamPart = {
|
141
|
+
code: "7",
|
142
|
+
name: "tool_calls",
|
143
|
+
parse: (value) => {
|
144
|
+
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
|
145
|
+
tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string";
|
146
|
+
})) {
|
147
|
+
throw new Error(
|
148
|
+
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
149
|
+
);
|
150
|
+
}
|
151
|
+
return {
|
152
|
+
type: "tool_calls",
|
153
|
+
value
|
154
|
+
};
|
155
|
+
}
|
156
|
+
};
|
140
157
|
var streamParts = [
|
141
158
|
textStreamPart,
|
142
159
|
functionCallStreamPart,
|
@@ -144,7 +161,8 @@ var streamParts = [
|
|
144
161
|
errorStreamPart,
|
145
162
|
assistantMessageStreamPart,
|
146
163
|
assistantControlDataStreamPart,
|
147
|
-
dataMessageStreamPart
|
164
|
+
dataMessageStreamPart,
|
165
|
+
toolCallStreamPart
|
148
166
|
];
|
149
167
|
var streamPartsByCode = {
|
150
168
|
[textStreamPart.code]: textStreamPart,
|
@@ -153,7 +171,8 @@ var streamPartsByCode = {
|
|
153
171
|
[errorStreamPart.code]: errorStreamPart,
|
154
172
|
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
155
173
|
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
156
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart
|
174
|
+
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
175
|
+
[toolCallStreamPart.code]: toolCallStreamPart
|
157
176
|
};
|
158
177
|
var StreamStringPrefixes = {
|
159
178
|
[textStreamPart.name]: textStreamPart.code,
|
@@ -162,7 +181,8 @@ var StreamStringPrefixes = {
|
|
162
181
|
[errorStreamPart.name]: errorStreamPart.code,
|
163
182
|
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
164
183
|
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
165
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code
|
184
|
+
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
185
|
+
[toolCallStreamPart.name]: toolCallStreamPart.code
|
166
186
|
};
|
167
187
|
var validCodes = streamParts.map((part) => part.code);
|
168
188
|
var parseStreamPart = (line) => {
|
@@ -288,20 +308,35 @@ async function parseComplexResponse({
|
|
288
308
|
};
|
289
309
|
functionCallMessage = prefixMap["function_call"];
|
290
310
|
}
|
311
|
+
let toolCallMessage = null;
|
312
|
+
if (type === "tool_calls") {
|
313
|
+
prefixMap["tool_calls"] = {
|
314
|
+
id: generateId(),
|
315
|
+
role: "assistant",
|
316
|
+
content: "",
|
317
|
+
tool_calls: value.tool_calls,
|
318
|
+
createdAt
|
319
|
+
};
|
320
|
+
toolCallMessage = prefixMap["tool_calls"];
|
321
|
+
}
|
291
322
|
if (type === "data") {
|
292
323
|
prefixMap["data"].push(...value);
|
293
324
|
}
|
294
325
|
const responseMessage = prefixMap["text"];
|
295
|
-
const merged = [
|
296
|
-
|
297
|
-
|
326
|
+
const merged = [
|
327
|
+
functionCallMessage,
|
328
|
+
toolCallMessage,
|
329
|
+
responseMessage
|
330
|
+
].filter(Boolean);
|
298
331
|
update(merged, [...prefixMap["data"]]);
|
299
332
|
}
|
300
333
|
onFinish == null ? void 0 : onFinish(prefixMap);
|
301
334
|
return {
|
302
|
-
messages: [
|
303
|
-
|
304
|
-
|
335
|
+
messages: [
|
336
|
+
prefixMap.text,
|
337
|
+
prefixMap.function_call,
|
338
|
+
prefixMap.tool_calls
|
339
|
+
].filter(Boolean),
|
305
340
|
data: prefixMap.data
|
306
341
|
};
|
307
342
|
}
|
@@ -387,6 +422,8 @@ async function callChatApi({
|
|
387
422
|
streamedResponse += decode(value);
|
388
423
|
if (streamedResponse.startsWith('{"function_call":')) {
|
389
424
|
responseMessage["function_call"] = streamedResponse;
|
425
|
+
} else if (streamedResponse.startsWith('{"tool_calls":')) {
|
426
|
+
responseMessage["tool_calls"] = streamedResponse;
|
390
427
|
} else {
|
391
428
|
responseMessage["content"] = streamedResponse;
|
392
429
|
}
|
@@ -401,6 +438,11 @@ async function callChatApi({
|
|
401
438
|
responseMessage["function_call"] = parsedFunctionCall;
|
402
439
|
appendMessage({ ...responseMessage });
|
403
440
|
}
|
441
|
+
if (streamedResponse.startsWith('{"tool_calls":')) {
|
442
|
+
const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
|
443
|
+
responseMessage["tool_calls"] = parsedToolCalls;
|
444
|
+
appendMessage({ ...responseMessage });
|
445
|
+
}
|
404
446
|
if (onFinish) {
|
405
447
|
onFinish(responseMessage);
|
406
448
|
}
|
@@ -412,6 +454,7 @@ async function callChatApi({
|
|
412
454
|
async function processChatStream({
|
413
455
|
getStreamedResponse: getStreamedResponse2,
|
414
456
|
experimental_onFunctionCall,
|
457
|
+
experimental_onToolCall,
|
415
458
|
updateChatRequest,
|
416
459
|
getCurrentMessages
|
417
460
|
}) {
|
@@ -420,12 +463,18 @@ async function processChatStream({
|
|
420
463
|
if ("messages" in messagesAndDataOrJustMessage) {
|
421
464
|
let hasFollowingResponse = false;
|
422
465
|
for (const message of messagesAndDataOrJustMessage.messages) {
|
423
|
-
if (message.function_call === void 0 || typeof message.function_call === "string") {
|
466
|
+
if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
|
424
467
|
continue;
|
425
468
|
}
|
426
469
|
hasFollowingResponse = true;
|
427
470
|
if (experimental_onFunctionCall) {
|
428
471
|
const functionCall = message.function_call;
|
472
|
+
if (typeof functionCall !== "object") {
|
473
|
+
console.warn(
|
474
|
+
"experimental_onFunctionCall should not be defined when using tools"
|
475
|
+
);
|
476
|
+
continue;
|
477
|
+
}
|
429
478
|
const functionCallResponse = await experimental_onFunctionCall(
|
430
479
|
getCurrentMessages(),
|
431
480
|
functionCall
|
@@ -436,22 +485,83 @@ async function processChatStream({
|
|
436
485
|
}
|
437
486
|
updateChatRequest(functionCallResponse);
|
438
487
|
}
|
488
|
+
if (experimental_onToolCall) {
|
489
|
+
const toolCalls = message.tool_calls;
|
490
|
+
if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
|
491
|
+
console.warn(
|
492
|
+
"experimental_onToolCall should not be defined when using tools"
|
493
|
+
);
|
494
|
+
continue;
|
495
|
+
}
|
496
|
+
const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
|
497
|
+
if (toolCallResponse === void 0) {
|
498
|
+
hasFollowingResponse = false;
|
499
|
+
break;
|
500
|
+
}
|
501
|
+
updateChatRequest(toolCallResponse);
|
502
|
+
}
|
439
503
|
}
|
440
504
|
if (!hasFollowingResponse) {
|
441
505
|
break;
|
442
506
|
}
|
443
507
|
} else {
|
508
|
+
let fixFunctionCallArguments2 = function(response) {
|
509
|
+
for (const message of response.messages) {
|
510
|
+
if (message.tool_calls !== void 0) {
|
511
|
+
for (const toolCall of message.tool_calls) {
|
512
|
+
if (typeof toolCall === "object") {
|
513
|
+
if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
|
514
|
+
toolCall.function.arguments = JSON.stringify(
|
515
|
+
toolCall.function.arguments
|
516
|
+
);
|
517
|
+
}
|
518
|
+
}
|
519
|
+
}
|
520
|
+
}
|
521
|
+
if (message.function_call !== void 0) {
|
522
|
+
if (typeof message.function_call === "object") {
|
523
|
+
if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
|
524
|
+
message.function_call.arguments = JSON.stringify(
|
525
|
+
message.function_call.arguments
|
526
|
+
);
|
527
|
+
}
|
528
|
+
}
|
529
|
+
}
|
530
|
+
}
|
531
|
+
};
|
532
|
+
var fixFunctionCallArguments = fixFunctionCallArguments2;
|
444
533
|
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
445
|
-
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
534
|
+
if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
|
446
535
|
break;
|
447
536
|
}
|
448
537
|
if (experimental_onFunctionCall) {
|
449
538
|
const functionCall = streamedResponseMessage.function_call;
|
539
|
+
if (!(typeof functionCall === "object")) {
|
540
|
+
console.warn(
|
541
|
+
"experimental_onFunctionCall should not be defined when using tools"
|
542
|
+
);
|
543
|
+
continue;
|
544
|
+
}
|
450
545
|
const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
|
451
546
|
if (functionCallResponse === void 0)
|
452
547
|
break;
|
548
|
+
fixFunctionCallArguments2(functionCallResponse);
|
453
549
|
updateChatRequest(functionCallResponse);
|
454
550
|
}
|
551
|
+
if (experimental_onToolCall) {
|
552
|
+
const toolCalls = streamedResponseMessage.tool_calls;
|
553
|
+
if (!(typeof toolCalls === "object")) {
|
554
|
+
console.warn(
|
555
|
+
"experimental_onToolCall should not be defined when using functions"
|
556
|
+
);
|
557
|
+
continue;
|
558
|
+
}
|
559
|
+
const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
|
560
|
+
if (toolCallResponse === void 0)
|
561
|
+
break;
|
562
|
+
fixFunctionCallArguments2(toolCallResponse);
|
563
|
+
updateChatRequest(toolCallResponse);
|
564
|
+
}
|
455
565
|
}
|
456
566
|
}
|
457
567
|
}
|