Skip to content

Commit 030c003

Browse files
committed
x
1 parent 120319a commit 030c003

20 files changed

+743
-0
lines changed

content/docs/02-foundations/02-providers-and-models.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The AI SDK comes with a wide range of providers that you can use to interact wit
3737
- [Fireworks Provider](/providers/ai-sdk-providers/fireworks) (`@ai-sdk/fireworks`)
3838
- [DeepInfra Provider](/providers/ai-sdk-providers/deepinfra) (`@ai-sdk/deepinfra`)
3939
- [DeepSeek Provider](/providers/ai-sdk-providers/deepseek) (`@ai-sdk/deepseek`)
40+
- [Moonshot Provider](/providers/ai-sdk-providers/moonshot) (`@ai-sdk/moonshot`)
4041
- [Cerebras Provider](/providers/ai-sdk-providers/cerebras) (`@ai-sdk/cerebras`)
4142
- [Groq Provider](/providers/ai-sdk-providers/groq) (`@ai-sdk/groq`)
4243
- [Perplexity Provider](/providers/ai-sdk-providers/perplexity) (`@ai-sdk/perplexity`)
@@ -137,6 +138,8 @@ Here are the capabilities of popular models:
137138
| [Google Vertex](/providers/ai-sdk-providers/google-vertex) | `gemini-1.5-pro` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
138139
| [DeepSeek](/providers/ai-sdk-providers/deepseek) | `deepseek-chat` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
139140
| [DeepSeek](/providers/ai-sdk-providers/deepseek) | `deepseek-reasoner` | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
141+
| [Moonshot](/providers/ai-sdk-providers/moonshot) | `kimi-k2-base` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
142+
| [Moonshot](/providers/ai-sdk-providers/moonshot) | `kimi-k2-instruct` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
140143
| [Cerebras](/providers/ai-sdk-providers/cerebras) | `llama3.1-8b` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
141144
| [Cerebras](/providers/ai-sdk-providers/cerebras) | `llama3.1-70b` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
142145
| [Cerebras](/providers/ai-sdk-providers/cerebras) | `llama3.3-70b` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: Moonshot
3+
description: Learn how to use Moonshot's models with the AI SDK.
4+
---
5+
6+
# Moonshot Provider
7+
8+
The [Moonshot](https://moonshot.ai) provider offers access to powerful language models through the Moonshot API, including their Kimi K2 model with advanced reasoning and coding capabilities.
9+
10+
API keys can be obtained from the [Moonshot Platform](https://platform.moonshot.ai).
11+
12+
## Setup
13+
14+
The Moonshot provider is available via the `@ai-sdk/moonshot` module. You can install it with:
15+
16+
<Tabs items={['pnpm', 'npm', 'yarn']}>
17+
<Tab>
18+
<Snippet text="pnpm add @ai-sdk/moonshot" dark />
19+
</Tab>
20+
<Tab>
21+
<Snippet text="npm install @ai-sdk/moonshot" dark />
22+
</Tab>
23+
<Tab>
24+
<Snippet text="yarn add @ai-sdk/moonshot" dark />
25+
</Tab>
26+
</Tabs>
27+
28+
## Provider Instance
29+
30+
You can import the default provider instance `moonshot` from `@ai-sdk/moonshot`:
31+
32+
```ts
33+
import { moonshot } from '@ai-sdk/moonshot';
34+
```
35+
36+
For custom configuration, you can import `createMoonshot` and create a provider instance with your settings:
37+
38+
```ts
39+
import { createMoonshot } from '@ai-sdk/moonshot';
40+
41+
const moonshot = createMoonshot({
42+
apiKey: process.env.MOONSHOT_API_KEY ?? '',
43+
});
44+
```
45+
46+
You can use the following optional settings to customize the Moonshot provider instance:
47+
48+
- **baseURL** _string_
49+
50+
Use a different URL prefix for API calls.
51+
The default prefix is `https://api.moonshot.ai/v1`.
52+
53+
- **apiKey** _string_
54+
55+
API key that is being sent using the `Authorization` header. It defaults to
56+
the `MOONSHOT_API_KEY` environment variable.
57+
58+
- **headers** _Record&lt;string,string&gt;_
59+
60+
Custom headers to include in the requests.
61+
62+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_
63+
64+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
65+
66+
## Language Models
67+
68+
You can create language models using a provider instance:
69+
70+
```ts
71+
import { moonshot } from '@ai-sdk/moonshot';
72+
import { generateText } from 'ai';
73+
74+
const { text } = await generateText({
75+
model: moonshot('kimi-k2-0711-preview'),
76+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
77+
});
78+
```
79+
80+
Moonshot language models can be used in the `streamText` function
81+
(see [AI SDK Core](/docs/ai-sdk-core)).
82+
83+
You can create Moonshot language models using a provider instance. The first argument is the model ID, e.g. `kimi-k2-0711-preview`:
84+
85+
```ts
86+
const model = moonshot('kimi-k2-0711-preview');
87+
```
88+
89+
You can also use the `.languageModel()` and `.chat()` methods:
90+
91+
```ts
92+
const model = moonshot.languageModel('kimi-k2-0711-preview');
93+
const model = moonshot.chat('kimi-k2-0711-preview');
94+
```
95+
96+
## Model Capabilities
97+
98+
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
99+
| ---------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
100+
| `kimi-k2-0711-preview` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
101+
102+
<Note>
103+
Please see the [Moonshot docs](https://platform.moonshot.ai/docs) for more
104+
details about the available models. You can also pass any available provider
105+
model ID as a string if needed.
106+
</Note>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { moonshot } from '@ai-sdk/moonshot';
2+
import { generateText, tool } from 'ai';
3+
import 'dotenv/config';
4+
import * as readline from 'node:readline/promises';
5+
import { z } from 'zod/v4';
6+
7+
const terminal = readline.createInterface({
8+
input: process.stdin,
9+
output: process.stdout,
10+
});
11+
12+
async function main() {
13+
const messages = [];
14+
15+
while (true) {
16+
const userInput = await terminal.question('You: ');
17+
messages.push({ role: 'user', content: userInput });
18+
19+
const { text } = await generateText({
20+
model: moonshot('kimi-k2-0711-preview'),
21+
tools: {
22+
weather: tool({
23+
description: 'Get the weather in a location',
24+
inputSchema: z.object({
25+
location: z
26+
.string()
27+
.describe('The location to get the weather for'),
28+
}),
29+
execute: async ({ location }) => ({
30+
location,
31+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
32+
}),
33+
}),
34+
},
35+
messages,
36+
});
37+
38+
console.log(`Assistant: ${text}`);
39+
messages.push({ role: 'assistant', content: text });
40+
}
41+
}
42+
43+
main().catch(console.error);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { moonshot } from '@ai-sdk/moonshot';
2+
import { generateText, tool } from 'ai';
3+
import 'dotenv/config';
4+
import { z } from 'zod/v4';
5+
6+
async function main() {
7+
const { text } = await generateText({
8+
model: moonshot('kimi-k2-0711-preview'),
9+
tools: {
10+
weather: tool({
11+
description: 'Get the weather in a location',
12+
inputSchema: z.object({
13+
location: z.string().describe('The location to get the weather for'),
14+
}),
15+
execute: async ({ location }) => ({
16+
location,
17+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
18+
}),
19+
}),
20+
},
21+
prompt: 'What is the weather in Tokyo?',
22+
});
23+
24+
console.log(text);
25+
}
26+
27+
main().catch(console.error);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { moonshot } from '@ai-sdk/moonshot';
2+
import { generateText } from 'ai';
3+
import 'dotenv/config';
4+
5+
async function main() {
6+
const { text } = await generateText({
7+
model: moonshot('kimi-k2-0711-preview'),
8+
prompt: 'Count from 1 to 3 and say hello.',
9+
temperature: 0.6, // Recommended temperature for Kimi K2
10+
});
11+
12+
console.log(text);
13+
}
14+
15+
main().catch(console.error);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { moonshot } from '@ai-sdk/moonshot';
2+
import { streamText, ModelMessage, ToolCallPart, ToolResultPart } from 'ai';
3+
import 'dotenv/config';
4+
import { weatherTool } from '../tools/weather-tool';
5+
6+
const messages: ModelMessage[] = [];
7+
8+
async function main() {
9+
let toolResponseAvailable = false;
10+
11+
const result = streamText({
12+
model: moonshot('kimi-k2-0711-preview'),
13+
maxOutputTokens: 512,
14+
tools: {
15+
weather: weatherTool,
16+
},
17+
toolChoice: 'auto',
18+
prompt: 'What is the weather in San Francisco?',
19+
});
20+
21+
let fullResponse = '';
22+
const toolCalls: ToolCallPart[] = [];
23+
const toolResponses: ToolResultPart[] = [];
24+
25+
for await (const delta of result.fullStream) {
26+
switch (delta.type) {
27+
case 'text': {
28+
fullResponse += delta.text;
29+
process.stdout.write(delta.text);
30+
break;
31+
}
32+
33+
case 'tool-call': {
34+
toolCalls.push(delta);
35+
36+
process.stdout.write(
37+
`\nTool call: '${delta.toolName}' ${JSON.stringify(delta.input)}`,
38+
);
39+
break;
40+
}
41+
42+
case 'tool-result': {
43+
const transformedDelta: ToolResultPart = {
44+
...delta,
45+
output: { type: 'json', value: delta.output },
46+
};
47+
toolResponses.push(transformedDelta);
48+
49+
process.stdout.write(
50+
`\nTool response: '${delta.toolName}' ${JSON.stringify(
51+
delta.output,
52+
)}`,
53+
);
54+
break;
55+
}
56+
}
57+
}
58+
process.stdout.write('\n\n');
59+
60+
messages.push({
61+
role: 'assistant',
62+
content: [{ type: 'text', text: fullResponse }, ...toolCalls],
63+
});
64+
65+
if (toolResponses.length > 0) {
66+
messages.push({ role: 'tool', content: toolResponses });
67+
}
68+
69+
toolResponseAvailable = toolCalls.length > 0;
70+
console.log('Messages:', messages[0].content);
71+
}
72+
73+
main().catch(console.error);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { moonshot } from '@ai-sdk/moonshot';
2+
import { streamText } from 'ai';
3+
import 'dotenv/config';
4+
5+
async function main() {
6+
const result = streamText({
7+
model: moonshot('kimi-k2-0711-preview'),
8+
prompt: "How many r's is there in strawberry?",
9+
temperature: 0.6, // Recommended temperature for Kimi K2
10+
});
11+
12+
for await (const textPart of result.textStream) {
13+
process.stdout.write(textPart);
14+
}
15+
}
16+
17+
main().catch(console.error);

packages/moonshot/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# @ai-sdk/moonshot
2+
3+
## 1.0.0-beta.1
4+
5+
### Major Changes
6+
7+
- Initial release of the Moonshot provider for AI SDK
8+
- Support for Kimi K2 models (kimi-k2-base and kimi-k2-instruct)
9+
- OpenAI-compatible API integration
10+
- Tool calling support for agentic capabilities

packages/moonshot/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# AI SDK - Moonshot Provider
2+
3+
The **[Moonshot provider](https://ai-sdk.dev/providers/ai-sdk-providers/moonshot)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Moonshot AI](https://api.moonshot.ai) platform.
4+
5+
## Setup
6+
7+
The Moonshot provider is available in the `@ai-sdk/moonshot` module. You can install it with
8+
9+
```bash
10+
npm i @ai-sdk/moonshot
11+
```
12+
13+
## Provider Instance
14+
15+
You can import the default provider instance `moonshot` from `@ai-sdk/moonshot`:
16+
17+
```ts
18+
import { moonshot } from '@ai-sdk/moonshot';
19+
```
20+
21+
## Example
22+
23+
```ts
24+
import { moonshot } from '@ai-sdk/moonshot';
25+
import { generateText } from 'ai';
26+
27+
const { text } = await generateText({
28+
model: moonshot('kimi-k2-0711-preview'),
29+
prompt: 'Write a JavaScript function that sorts a list:',
30+
temperature: 0.6, // Recommended temperature for Kimi K2
31+
});
32+
```
33+
34+
## Models
35+
36+
The Moonshot provider supports the following models:
37+
38+
### Generation Models
39+
40+
- `kimi-k2-0711-preview`: Mixture-of-Experts model with exceptional coding and agent capabilities
41+
42+
## Configuration
43+
44+
You can configure the Moonshot provider with your API key and other settings:
45+
46+
```ts
47+
import { createMoonshot } from '@ai-sdk/moonshot';
48+
49+
const moonshot = createMoonshot({
50+
apiKey: 'your-moonshot-api-key', // defaults to process.env.MOONSHOT_API_KEY
51+
baseURL: 'https://api.moonshot.ai/v1', // optional, defaults to Moonshot's API
52+
});
53+
```
54+
55+
## Documentation
56+
57+
Please check out the **[Moonshot provider](https://ai-sdk.dev/providers/ai-sdk-providers/moonshot)** for more information.

0 commit comments

Comments
 (0)