Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

quick-start.js

174 lines4.8 KB

1#!/usr/bin/env node
2​
3// Quick Start Example for @tanstack/ai
4// This demonstrates how to use the library with different providers
5​
6import { AI } from "../packages/ai/dist/index.js";
7import { OpenAIAdapter } from "../packages/ai-openai/dist/index.js";
8import { AnthropicAdapter } from "../packages/ai-anthropic/dist/index.js";
9import { OllamaAdapter } from "../packages/ai-ollama/dist/index.js";
10import { GeminiAdapter } from "../packages/ai-gemini/dist/index.js";
11​
12// Example 1: Using OpenAI
13async function openAIExample() {
14 console.log("🚀 OpenAI Example");
15 console.log("-----------------");
16​
17 const ai = new AI(
18 new OpenAIAdapter({
19 apiKey: process.env.OPENAI_API_KEY || "your-api-key",
20 })
21 );
22​
23 try {
24 // Simple chat
25 const response = await ai.chat({
26 model: "gpt-3.5-turbo",
27 messages: [
28 { role: "system", content: "You are a helpful assistant." },
29 { role: "user", content: "What is the capital of France?" },
30 ],
31 maxTokens: 100,
32 });
33​
34 console.log("Chat Response:", response.content);
35 console.log("Tokens Used:", response.usage.totalTokens);
36 } catch (error) {
37 console.log("OpenAI not configured or available:", error.message);
38 }
40​
41// Example 2: Using Anthropic Claude
42async function anthropicExample() {
43 console.log("\n🤖 Anthropic Claude Example");
44 console.log("---------------------------");
45​
46 const ai = new AI(
47 new AnthropicAdapter({
48 apiKey: process.env.ANTHROPIC_API_KEY || "your-api-key",
49 })
50 );
51​
52 try {
53 // Text generation
54 const result = await ai.generateText({
55 model: "claude-3-sonnet-20240229",
56 prompt: "Write a haiku about programming",
57 maxTokens: 100,
58 });
59​
60 console.log("Generated Haiku:", result.text);
61 } catch (error) {
62 console.log("Anthropic not configured or available:", error.message);
63 }
65​
66// Example 3: Using Ollama (Local)
67async function ollamaExample() {
68 console.log("\n🦙 Ollama (Local) Example");
69 console.log("-------------------------");
70​
71 const ai = new AI(
72 new OllamaAdapter({
73 host: "http://localhost:11434",
74 })
75 );
76​
77 try {
78 // Chat with local model
79 const response = await ai.chat({
80 model: "llama2",
81 messages: [
82 { role: "user", content: "Explain quantum computing in one sentence." },
83 ],
84 });
85​
86 console.log("Ollama Response:", response.content);
87 } catch (error) {
88 console.log("Ollama not running or available:", error.message);
89 }
91​
92// Example 4: Using Google Gemini
93async function geminiExample() {
94 console.log("\n💎 Google Gemini Example");
95 console.log("------------------------");
96​
97 const ai = new AI(
98 new GeminiAdapter({
99 apiKey: process.env.GOOGLE_API_KEY || "your-api-key",
100 })
101 );
102​
103 try {
104 // Summarization
105 const summary = await ai.summarize({
106 model: "gemini-pro",
107 text: `The quick brown fox jumps over the lazy dog. This pangram sentence
108 contains every letter of the English alphabet at least once. It's commonly
109 used for testing typewriters, computer keyboards, and fonts.`,
110 style: "concise",
111 });
112​
113 console.log("Summary:", summary.summary);
114 } catch (error) {
115 console.log("Gemini not configured or available:", error.message);
116 }
118​
119// Example 5: Switching Providers Dynamically
120async function switchProvidersExample() {
121 console.log("\n🔄 Dynamic Provider Switching");
122 console.log("-----------------------------");
123​
124 // Start with one provider
125 const ai = new AI(
126 new OpenAIAdapter({
127 apiKey: process.env.OPENAI_API_KEY || "demo",
128 })
129 );
130​
131 console.log("Current adapter:", ai.adapterName);
132​
133 // Switch to another provider
134 ai.setAdapter(
135 new AnthropicAdapter({
136 apiKey: process.env.ANTHROPIC_API_KEY || "demo",
137 })
138 );
139​
140 console.log("Switched to:", ai.adapterName);
141​
142 // Switch to local Ollama
143 ai.setAdapter(new OllamaAdapter());
144 console.log("Switched to:", ai.adapterName);
146​
147// Run all examples
148async function main() {
149 console.log("================================================");
150 console.log(" @tanstack/ai - Quick Start Examples ");
151 console.log("================================================\n");
152​
153 console.log("Note: Set environment variables for API keys:");
154 console.log(" - OPENAI_API_KEY");
155 console.log(" - ANTHROPIC_API_KEY");
156 console.log(" - GOOGLE_API_KEY");
157 console.log(" - For Ollama, ensure it's running locally\n");
158​
159 await openAIExample();
160 await anthropicExample();
161 await ollamaExample();
162 await geminiExample();
163 await switchProvidersExample();
164​
165 console.log("\n================================================");
166 console.log(" Examples Complete! Happy Coding! 🎉 ");
167 console.log("================================================\n");
168​
169 console.log("Try the interactive CLI:");
170 console.log(" cd examples/cli");
171 console.log(" pnpm start chat --provider openai");
173​
174main().catch(console.error);