Skip to content

Commit

Permalink
feat (ai/core): add system message support in messages list (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored May 10, 2024
1 parent 8e29de3 commit 5b01c13
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/dirty-falcons-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ai-sdk/anthropic': patch
'ai': patch
---

feat (ai/core): add system message support in messages list
19 changes: 19 additions & 0 deletions examples/ai-core/src/generate-text/openai-system-message-a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await generateText({
model: openai('gpt-3.5-turbo'),
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' },
],
});

console.log(result.text);
}

main().catch(console.error);
17 changes: 17 additions & 0 deletions examples/ai-core/src/generate-text/openai-system-message-b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await generateText({
model: openai('gpt-3.5-turbo'),
system: 'You are a helpful assistant.',
messages: [{ role: 'user', content: 'What is the capital of France?' }],
});

console.log(result.text);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import {
export function convertToAnthropicMessagesPrompt(
prompt: LanguageModelV1Prompt,
): AnthropicMessagesPrompt {
let system: string | undefined;
let system: string | undefined = undefined;
const messages: AnthropicMessage[] = [];

for (const { role, content } of prompt) {
switch (role) {
case 'system': {
if (system != null) {
throw new UnsupportedFunctionalityError({
functionality: 'Multiple system messages',
});
}

system = content;
break;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/core/prompt/convert-to-language-model-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export function convertToLanguageModelPrompt(
languageModelMessages.push(
...prompt.messages.map((message): LanguageModelV1Message => {
switch (message.role) {
case 'system': {
return { role: 'system', content: message.content };
}

case 'user': {
if (typeof message.content === 'string') {
return {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/core/prompt/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ A message that can be used in the `messages` field of a prompt.
It can be a user message, an assistant message, or a tool message.
*/
export type CoreMessage =
| CoreSystemMessage
| CoreUserMessage
| CoreAssistantMessage
| CoreToolMessage;

/**
A system message. It can contain system information.
Note: using the "system" part of the prompt is strongly preferred
to increase the resilience against prompt injection attacks,
and because not all providers support several system messages.
*/
export type CoreSystemMessage = { role: 'system'; content: string };

/**
* @deprecated Use `CoreMessage` instead.
*/
Expand Down

0 comments on commit 5b01c13

Please sign in to comment.