Skip to content

Commit

Permalink
feat (ai/core): custom request headers (#2116)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Jun 27, 2024
1 parent caa198b commit 5edc611
Show file tree
Hide file tree
Showing 58 changed files with 811 additions and 442 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-flowers-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/provider': patch
---

feat (provider): add headers support to language and embedding model spec
13 changes: 13 additions & 0 deletions .changeset/fast-peas-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@ai-sdk/amazon-bedrock': patch
'@ai-sdk/google-vertex': patch
'@ai-sdk/anthropic': patch
'@ai-sdk/mistral': patch
'@ai-sdk/cohere': patch
'@ai-sdk/google': patch
'@ai-sdk/openai': patch
'@ai-sdk/azure': patch
'ai': patch
---

feat (ai/core): add custom request header support
5 changes: 5 additions & 0 deletions .changeset/stupid-deers-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/provider-utils': major
---

feat (provider-utils): change getRequestHeader() test helper to return Record (breaking change)
5 changes: 5 additions & 0 deletions .changeset/ten-plants-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/provider-utils': patch
---

feat (provider-utils): add combineHeaders helper
4 changes: 4 additions & 0 deletions content/docs/03-ai-sdk-core/25-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ Maximum number of retries. Set to 0 to disable retries. Default: `2`.
### `abortSignal`

An optional abort signal that can be used to cancel the call.

### `headers`

Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
7 changes: 7 additions & 0 deletions content/docs/07-reference/ai-sdk-core/01-generate-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ console.log(text);
description:
'An optional abort signal that can be used to cancel the call.',
},
{
name: 'headers',
type: 'Record<string, string>',
isOptional: true,
description:
'Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.',
},
{
name: 'maxAutomaticRoundtrips',
type: 'number',
Expand Down
7 changes: 7 additions & 0 deletions content/docs/07-reference/ai-sdk-core/02-stream-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ for await (const textPart of textStream) {
description:
'An optional abort signal that can be used to cancel the call.',
},
{
name: 'headers',
type: 'Record<string, string>',
isOptional: true,
description:
'Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.',
},
{
name: 'onFinish',
type: '(result: OnFinishResult) => void',
Expand Down
136 changes: 71 additions & 65 deletions content/docs/07-reference/ai-sdk-core/03-generate-object.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,30 @@ console.log(JSON.stringify(object, null, 2));
{
name: 'model',
type: 'LanguageModel',
description: `The language model to use. Example: openai('gpt-4-turbo')`
description: `The language model to use. Example: openai('gpt-4-turbo')`,
},
{
name: 'mode',
type: "'auto' | 'json' | 'grammar' | 'tool'",
description:
"The mode to use for object generation. Not every model supports all modes. Defaults to 'auto'."
"The mode to use for object generation. Not every model supports all modes. Defaults to 'auto'.",
},
{
name: 'schema',
type: 'Zod Schema',
description: 'The schema that describes the shape of the object to generate. It is sent to the model to generate the object and used to validate the output.'
name: 'schema',
type: 'Zod Schema',
description:
'The schema that describes the shape of the object to generate. It is sent to the model to generate the object and used to validate the output.',
},
{
name: 'system',
type: 'string',
description:
'The system prompt to use that specifies the behavior of the model.'
'The system prompt to use that specifies the behavior of the model.',
},
{
name: 'prompt',
type: 'string',
description: 'The input prompt to generate the text from.'
description: 'The input prompt to generate the text from.',
},
{
name: 'messages',
Expand All @@ -77,22 +78,22 @@ console.log(JSON.stringify(object, null, 2));
{
name: 'role',
type: "'system'",
description: 'The role for the system message.'
description: 'The role for the system message.',
},
{
name: 'content',
type: 'string',
description: 'The content of the message.'
}
]
description: 'The content of the message.',
},
],
},
{
type: 'CoreUserMessage',
parameters: [
{
name: 'role',
type: "'user'",
description: 'The role for the user message.'
description: 'The role for the user message.',
},
{
name: 'content',
Expand All @@ -105,42 +106,42 @@ console.log(JSON.stringify(object, null, 2));
{
name: 'type',
type: "'text'",
description: 'The type of the message part.'
description: 'The type of the message part.',
},
{
name: 'text',
type: 'string',
description: 'The text content of the message part.'
}
]
description: 'The text content of the message part.',
},
],
},
{
type: 'ImagePart',
parameters: [
{
name: 'type',
type: "'image'",
description: 'The type of the message part.'
description: 'The type of the message part.',
},
{
name: 'image',
type: 'string | Uint8Array | Buffer | ArrayBuffer | URL',
description:
'The image content of the message part. String are either base64 encoded content, base64 data URLs, or http(s) URLs.'
}
]
}
]
}
]
'The image content of the message part. String are either base64 encoded content, base64 data URLs, or http(s) URLs.',
},
],
},
],
},
],
},
{
type: 'CoreAssistantMessage',
parameters: [
{
name: 'role',
type: "'assistant'",
description: 'The role for the assistant message.'
description: 'The role for the assistant message.',
},
{
name: 'content',
Expand All @@ -153,53 +154,53 @@ console.log(JSON.stringify(object, null, 2));
{
name: 'type',
type: "'text'",
description: 'The type of the message part.'
description: 'The type of the message part.',
},
{
name: 'text',
type: 'string',
description: 'The text content of the message part.'
}
]
description: 'The text content of the message part.',
},
],
},
{
type: 'ToolCallPart',
parameters: [
{
name: 'type',
type: "'tool-call'",
description: 'The type of the message part.'
description: 'The type of the message part.',
},
{
name: 'toolCallId',
type: 'string',
description: 'The id of the tool call.'
description: 'The id of the tool call.',
},
{
name: 'toolName',
type: 'string',
description:
'The name of the tool, which typically would be the name of the function.'
'The name of the tool, which typically would be the name of the function.',
},
{
name: 'args',
type: 'object based on zod schema',
description:
'Parameters generated by the model to be used by the tool.'
}
]
}
]
}
]
'Parameters generated by the model to be used by the tool.',
},
],
},
],
},
],
},
{
type: 'CoreToolMessage',
parameters: [
{
name: 'role',
type: "'tool'",
description: 'The role for the assistant message.'
description: 'The role for the assistant message.',
},
{
name: 'content',
Expand All @@ -212,99 +213,104 @@ console.log(JSON.stringify(object, null, 2));
{
name: 'type',
type: "'tool-result'",
description: 'The type of the message part.'
description: 'The type of the message part.',
},
{
name: 'toolCallId',
type: 'string',
description:
'The id of the tool call the result corresponds to.'
'The id of the tool call the result corresponds to.',
},
{
name: 'toolName',
type: 'string',
description:
'The name of the tool the result corresponds to.'
'The name of the tool the result corresponds to.',
},
{
name: 'result',
type: 'unknown',
description:
'The result returned by the tool after execution.'
'The result returned by the tool after execution.',
},
{
name: 'isError',
type: 'boolean',
isOptional: true,
description:
'Whether the result is an error or an error message.'
}
]
}
]
}
]
}
]
'Whether the result is an error or an error message.',
},
],
},
],
},
],
},
],
},

{
name: 'maxTokens',
type: 'number',
isOptional: true,
description: 'Maximum number of tokens to generate.'
description: 'Maximum number of tokens to generate.',
},
{
name: 'temperature',
type: 'number',
isOptional: true,
description:
'Temperature setting. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either `temperature` or `topP`, but not both.'
'Temperature setting. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either `temperature` or `topP`, but not both.',
},
{
name: 'topP',
type: 'number',
isOptional: true,
description:
'Nucleus sampling. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either `temperature` or `topP`, but not both.'
'Nucleus sampling. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either `temperature` or `topP`, but not both.',
},
{
name: 'presencePenalty',
type: 'number',
isOptional: true,
description:
'Presence penalty setting. It affects the likelihood of the model to repeat information that is already in the prompt. The value is passed through to the provider. The range depends on the provider and model.'
'Presence penalty setting. It affects the likelihood of the model to repeat information that is already in the prompt. The value is passed through to the provider. The range depends on the provider and model.',
},
{
name: 'frequencyPenalty',
type: 'number',
isOptional: true,
description:
'Frequency penalty setting. It affects the likelihood of the model to repeatedly use the same words or phrases. The value is passed through to the provider. The range depends on the provider and model.'
'Frequency penalty setting. It affects the likelihood of the model to repeatedly use the same words or phrases. The value is passed through to the provider. The range depends on the provider and model.',
},
{
name: 'seed',
type: 'number',
isOptional: true,
description:
'The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results.'
'The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results.',
},
{
name: 'maxRetries',
type: 'number',
isOptional: true,
description:
'Maximum number of retries. Set to 0 to disable retries. Default: 2.'
'Maximum number of retries. Set to 0 to disable retries. Default: 2.',
},
{
name: 'abortSignal',
type: 'AbortSignal',
isOptional: true,
description:
'An optional abort signal that can be used to cancel the call.'
}

]}
'An optional abort signal that can be used to cancel the call.',
},
{
name: 'headers',
type: 'Record<string, string>',
isOptional: true,
description:
'Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.',
},
]}
/>

### Returns
Expand Down
Loading

0 comments on commit 5edc611

Please sign in to comment.