-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
81 lines (71 loc) · 2.77 KB
/
Copy pathexample.js
File metadata and controls
81 lines (71 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Examples using @withmartian/ai-sdk-provider with the AI SDK
*/
import { generateText, generateObject } from 'ai';
import { martianProvider } from '@withmartian/ai-sdk-provider';
import { z } from 'zod';
async function main() {
// ============================================
// Basic text generation
// ============================================
console.log('\n=== Basic Text Generation ===\n');
const basic = await generateText({
model: martianProvider('gpt-4o-mini'),
prompt: 'Write a haiku about coding.',
});
console.log('[gpt-4o-mini]', basic.text);
// ============================================
// Tool calling with Claude
// ============================================
console.log('\n=== Tool Calling (Claude) ===\n');
const { text, toolCalls } = await generateText({
model: martianProvider('anthropic/claude-sonnet-4-20250514'),
prompt: "What's the weather in Boston?",
tools: {
getWeather: {
description: 'Get weather for a city',
inputSchema: z.object({ city: z.string() }),
execute: async ({ city }) => ({ city, temp: 72, conditions: 'sunny' }),
},
},
maxSteps: 3,
});
console.log('[claude] Tool calls:', toolCalls?.map(t => t.toolName));
console.log('[claude] Response:', text);
// ============================================
// Advanced generation with Gemini
// ============================================
console.log('\n=== Advanced Generation (Gemini) ===\n');
const { text: geminiText, finishReason, usage } = await generateText({
model: martianProvider('google/gemini-2.0-flash'),
system: 'You are a creative writing assistant.',
messages: [
{ role: 'user', content: 'Write a short story about a robot.' },
{ role: 'assistant', content: 'Once upon a time, there was a robot named Bolt...' },
{ role: 'user', content: 'Continue with a twist ending in 2 sentences.' },
],
temperature: 0.9,
topP: 0.95,
maxTokens: 200,
});
console.log('[gemini]', geminiText);
console.log('[gemini] Finish reason:', finishReason);
console.log('[gemini] Tokens:', usage?.totalTokens);
// ============================================
// Structured output
// ============================================
console.log('\n=== Structured Output (GPT-4o-mini) ===\n');
const { object } = await generateObject({
model: martianProvider('gpt-4o-mini'),
schema: z.object({
recipe: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
prompt: 'Generate a simple 3-ingredient sandwich recipe.',
});
console.log('[gpt-4o-mini] Recipe:', object.recipe);
console.log('[gpt-4o-mini] Ingredients:', object.ingredients);
console.log('[gpt-4o-mini] Steps:', object.steps);
}
main().catch(console.error);