ai 3.0.4 → 3.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "3.0.4",
3
+ "version": "3.0.5",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "require": "./dist/index.js"
25
25
  },
26
26
  "./rsc": {
27
- "types": "./rsc/dist/rsc-types.d.ts",
27
+ "types": "./rsc/dist/index.d.mts",
28
28
  "react-server": "./rsc/dist/rsc-server.mjs",
29
29
  "import": "./rsc/dist/rsc-client.mjs",
30
30
  "module": "./rsc/dist/rsc-client.mjs"
@@ -73,7 +73,7 @@
73
73
  "zod-to-json-schema": "^3.22.4"
74
74
  },
75
75
  "devDependencies": {
76
- "@anthropic-ai/sdk": "0.12.0",
76
+ "@anthropic-ai/sdk": "0.15.0",
77
77
  "@aws-sdk/client-bedrock-runtime": "3.451.0",
78
78
  "@edge-runtime/vm": "^3.1.7",
79
79
  "@google/generative-ai": "0.1.1",
@@ -97,7 +97,7 @@
97
97
  "openai": "4.28.4",
98
98
  "react-dom": "^18.2.0",
99
99
  "solid-js": "^1.8.7",
100
- "tsup": "^6.7.0",
100
+ "tsup": "^7.2.0",
101
101
  "typescript": "5.1.3",
102
102
  "vite-plugin-solid": "2.7.2",
103
103
  "@vercel/ai-tsconfig": "0.0.0",
@@ -0,0 +1,242 @@
1
+ interface FunctionCall {
2
+ /**
3
+ * The arguments to call the function with, as generated by the model in JSON
4
+ * format. Note that the model does not always generate valid JSON, and may
5
+ * hallucinate parameters not defined by your function schema. Validate the
6
+ * arguments in your code before calling your function.
7
+ */
8
+ arguments?: string;
9
+ /**
10
+ * The name of the function to call.
11
+ */
12
+ name?: string;
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
+ * Shared types between the API and UI packages.
27
+ */
28
+ interface Message {
29
+ id: string;
30
+ tool_call_id?: string;
31
+ createdAt?: Date;
32
+ content: string;
33
+ ui?: string | JSX.Element | JSX.Element[] | null | undefined;
34
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
35
+ /**
36
+ * If the message has a role of `function`, the `name` field is the name of the function.
37
+ * Otherwise, the name field should not be set.
38
+ */
39
+ name?: string;
40
+ /**
41
+ * If the assistant role makes a function call, the `function_call` field
42
+ * contains the function call name and arguments. Otherwise, the field should
43
+ * not be set. (Deprecated and replaced by tool_calls.)
44
+ */
45
+ function_call?: string | FunctionCall;
46
+ data?: JSONValue;
47
+ /**
48
+ * If the assistant role makes a tool call, the `tool_calls` field contains
49
+ * the tool call name and arguments. Otherwise, the field should not be set.
50
+ */
51
+ tool_calls?: string | ToolCall[];
52
+ /**
53
+ * Additional message-specific information added on the server via StreamData
54
+ */
55
+ annotations?: JSONValue[] | undefined;
56
+ }
57
+ type JSONValue = null | string | number | boolean | {
58
+ [x: string]: JSONValue;
59
+ } | Array<JSONValue>;
60
+
61
+ /**
62
+ * A prompt constructor for Anthropic models.
63
+ * Does not support `function` messages.
64
+ * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api
65
+ */
66
+ declare function experimental_buildAnthropicPrompt(messages: Pick<Message, 'content' | 'role'>[]): string;
67
+
68
+ /**
69
+ * A prompt constructor for the HuggingFace StarChat Beta model.
70
+ * Does not support `function` messages.
71
+ * @see https://huggingface.co/HuggingFaceH4/starchat-beta
72
+ */
73
+ declare function experimental_buildStarChatBetaPrompt(messages: Pick<Message, 'content' | 'role'>[]): string;
74
+ /**
75
+ * A prompt constructor for HuggingFace OpenAssistant models.
76
+ * Does not support `function` or `system` messages.
77
+ * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5
78
+ */
79
+ declare function experimental_buildOpenAssistantPrompt(messages: Pick<Message, 'content' | 'role'>[]): string;
80
+ /**
81
+ * A prompt constructor for HuggingFace LLama 2 chat models.
82
+ * Does not support `function` messages.
83
+ * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2
84
+ */
85
+ declare function experimental_buildLlama2Prompt(messages: Pick<Message, 'content' | 'role'>[]): string;
86
+
87
+ declare function experimental_buildOpenAIMessages(messages: Message[]): ChatCompletionMessageParam[];
88
+ type ChatCompletionMessageParam = ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionAssistantMessageParam | ChatCompletionToolMessageParam | ChatCompletionFunctionMessageParam;
89
+ interface ChatCompletionSystemMessageParam {
90
+ /**
91
+ * The contents of the system message.
92
+ */
93
+ content: string | null;
94
+ /**
95
+ * The role of the messages author, in this case `system`.
96
+ */
97
+ role: 'system';
98
+ }
99
+ interface ChatCompletionUserMessageParam {
100
+ /**
101
+ * The contents of the user message.
102
+ */
103
+ content: string | Array<ChatCompletionContentPart> | null;
104
+ /**
105
+ * The role of the messages author, in this case `user`.
106
+ */
107
+ role: 'user';
108
+ }
109
+ type ChatCompletionContentPart = ChatCompletionContentPartText | ChatCompletionContentPartImage;
110
+ interface ChatCompletionContentPartText {
111
+ /**
112
+ * The text content.
113
+ */
114
+ text: string;
115
+ /**
116
+ * The type of the content part.
117
+ */
118
+ type: 'text';
119
+ }
120
+ interface ChatCompletionContentPartImage {
121
+ image_url: ChatCompletionContentPartImage.ImageURL;
122
+ /**
123
+ * The type of the content part.
124
+ */
125
+ type: 'image_url';
126
+ }
127
+ declare namespace ChatCompletionContentPartImage {
128
+ interface ImageURL {
129
+ /**
130
+ * Specifies the detail level of the image.
131
+ */
132
+ detail?: 'auto' | 'low' | 'high';
133
+ /**
134
+ * Either a URL of the image or the base64 encoded image data.
135
+ */
136
+ url?: string;
137
+ }
138
+ }
139
+ interface ChatCompletionAssistantMessageParam {
140
+ /**
141
+ * The contents of the assistant message.
142
+ */
143
+ content: string | null;
144
+ /**
145
+ * The role of the messages author, in this case `assistant`.
146
+ */
147
+ role: 'assistant';
148
+ /**
149
+ * Deprecated and replaced by `tool_calls`. The name and arguments of a function
150
+ * that should be called, as generated by the model.
151
+ */
152
+ function_call?: ChatCompletionAssistantMessageParam.FunctionCall;
153
+ /**
154
+ * The tool calls generated by the model, such as function calls.
155
+ */
156
+ tool_calls?: Array<ChatCompletionMessageToolCall>;
157
+ }
158
+ declare namespace ChatCompletionAssistantMessageParam {
159
+ /**
160
+ * Deprecated and replaced by `tool_calls`. The name and arguments of a function
161
+ * that should be called, as generated by the model.
162
+ */
163
+ interface FunctionCall {
164
+ /**
165
+ * The arguments to call the function with, as generated by the model in JSON
166
+ * format. Note that the model does not always generate valid JSON, and may
167
+ * hallucinate parameters not defined by your function schema. Validate the
168
+ * arguments in your code before calling your function.
169
+ */
170
+ arguments: string;
171
+ /**
172
+ * The name of the function to call.
173
+ */
174
+ name: string;
175
+ }
176
+ }
177
+ interface ChatCompletionMessageToolCall {
178
+ /**
179
+ * The ID of the tool call.
180
+ */
181
+ id: string;
182
+ /**
183
+ * The function that the model called.
184
+ */
185
+ function: ChatCompletionMessageToolCall.Function;
186
+ /**
187
+ * The type of the tool. Currently, only `function` is supported.
188
+ */
189
+ type: 'function';
190
+ }
191
+ declare namespace ChatCompletionMessageToolCall {
192
+ /**
193
+ * The function that the model called.
194
+ */
195
+ interface Function {
196
+ /**
197
+ * The arguments to call the function with, as generated by the model in JSON
198
+ * format. Note that the model does not always generate valid JSON, and may
199
+ * hallucinate parameters not defined by your function schema. Validate the
200
+ * arguments in your code before calling your function.
201
+ */
202
+ arguments: string;
203
+ /**
204
+ * The name of the function to call.
205
+ */
206
+ name: string;
207
+ }
208
+ }
209
+ interface ChatCompletionToolMessageParam {
210
+ /**
211
+ * The contents of the tool message.
212
+ */
213
+ content: string | null;
214
+ /**
215
+ * The role of the messages author, in this case `tool`.
216
+ */
217
+ role: 'tool';
218
+ /**
219
+ * The name of the function that this message is responding to.
220
+ */
221
+ name: string;
222
+ /**
223
+ * Tool call that this message is responding to.
224
+ */
225
+ tool_call_id: string;
226
+ }
227
+ interface ChatCompletionFunctionMessageParam {
228
+ /**
229
+ * The return value from the function call, to return to the model.
230
+ */
231
+ content: string | null;
232
+ /**
233
+ * The name of the function to call.
234
+ */
235
+ name: string;
236
+ /**
237
+ * The role of the messages author, in this case `function`.
238
+ */
239
+ role: 'function';
240
+ }
241
+
242
+ export { ChatCompletionAssistantMessageParam, ChatCompletionContentPart, ChatCompletionContentPartImage, ChatCompletionContentPartText, ChatCompletionFunctionMessageParam, ChatCompletionMessageParam, ChatCompletionMessageToolCall, ChatCompletionSystemMessageParam, ChatCompletionToolMessageParam, ChatCompletionUserMessageParam, experimental_buildAnthropicPrompt, experimental_buildLlama2Prompt, experimental_buildOpenAIMessages, experimental_buildOpenAssistantPrompt, experimental_buildStarChatBetaPrompt };
@@ -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 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,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,OAAO;A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AA,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":[]}
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,SACE,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,MAAM;AAClC,QAAI,SAAS,QAAQ;AACnB,aAAO;AAAA;AAAA,SAAc,OAAO;AAAA,IAC9B,OAAO;AACL,aAAO;AAAA;AAAA,aAAkB,OAAO;AAAA,IAClC;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,OAAO;AAAA;AAAA,IAC7B,WAAW,SAAS,aAAa;AAC/B,aAAO;AAAA,EAAkB,OAAO;AAAA;AAAA,IAClC,WAAW,SAAS,UAAU;AAC5B,aAAO;AAAA,EAAe,OAAO;AAAA;AAAA,IAC/B,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,OAAO;AAAA,IAC/B,WAAW,SAAS,YAAY;AAC9B,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE,WAAW,SAAS,UAAU;AAC5B,YAAM,IAAI,MAAM,iDAAiD;AAAA,IACnE,OAAO;AACL,aAAO,gBAAgB,OAAO;AAAA,IAChC;AAAA,EACF,CAAC,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,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":[]}
@@ -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 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,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,OAAO;AACL,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF,CAAC,EACA,KAAK,EAAE,IAAI;AAElB;AAOO,SAAS,+BACd,UACA;AACA,QAAM,cAAc;AACpB,QAAM,YAAY;AAClB,QAAM,eAAe,SAAS,IAAI,CAAC,EAAE,SAAS,KAAK,GAAG,UAAU;AAC9D,QAAI,SAAS,QAAQ;AACnB,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M,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":[]}
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,Q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