feat: Initialize test-ai-sdk project with Bun
- Add README.md with installation and usage instructions. - Create bun.lock and pnpm-lock.yaml for dependency management. - Implement main functionality in index.ts to test AI providers. - Add opencode.json configuration for various AI providers. - Create package.json to define project dependencies and scripts. - Add TypeScript configuration in tsconfig.json for project setup. - Implement test scripts for different AI providers in src directory.
This commit is contained in:
96
index.ts
Normal file
96
index.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { createAnthropic } from '@ai-sdk/anthropic';
|
||||
import { generateText } from 'ai';
|
||||
import 'dotenv/config';
|
||||
interface ProviderConfig {
|
||||
npm: string;
|
||||
name: string;
|
||||
models: Record<string, { name: string }>;
|
||||
options: {
|
||||
baseURL?: string;
|
||||
apiKey: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface OpenCodeConfig {
|
||||
provider: Record<string, ProviderConfig>;
|
||||
}
|
||||
|
||||
// 解析环境变量占位符 {env:VAR_NAME}
|
||||
function resolveEnvVars(value: string): string {
|
||||
return value.replace(/{env:([^}]+)}/g, (_, varName) => {
|
||||
const envValue = process.env[varName];
|
||||
if (!envValue) {
|
||||
throw new Error(`Environment variable ${varName} is not set`);
|
||||
}
|
||||
return envValue;
|
||||
});
|
||||
}
|
||||
|
||||
async function testProvider(providerName: string, config: ProviderConfig) {
|
||||
console.log(`\nTesting provider: ${config.name}`);
|
||||
|
||||
const apiKey = resolveEnvVars(config.options.apiKey);
|
||||
const baseURL = config.options.baseURL ? resolveEnvVars(config.options.baseURL) : undefined;
|
||||
|
||||
let provider;
|
||||
console.log('baseURL:', baseURL);
|
||||
console.log('apiKey:', apiKey ? '[REDACTED]' : '[MISSING]');
|
||||
if (config.npm === '@ai-sdk/openai-compatible') {
|
||||
provider = createOpenAICompatible({
|
||||
baseURL: baseURL!,
|
||||
name: providerName,
|
||||
apiKey,
|
||||
});
|
||||
} else if (config.npm === '@ai-sdk/anthropic') {
|
||||
provider = createAnthropic({
|
||||
baseURL: baseURL,
|
||||
apiKey,
|
||||
});
|
||||
} else {
|
||||
throw new Error(`Unsupported npm package: ${config.npm}`);
|
||||
}
|
||||
|
||||
const modelKeys = Object.keys(config.models);
|
||||
if (modelKeys.length === 0) {
|
||||
throw new Error('No models configured for this provider');
|
||||
}
|
||||
const modelName = modelKeys[0];
|
||||
|
||||
try {
|
||||
const result = await generateText({
|
||||
model: provider(modelName),
|
||||
prompt: 'Say hello in one sentence.',
|
||||
});
|
||||
|
||||
console.log(`Model: ${modelName}`);
|
||||
console.log(`Response: ${result.text}`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const config: OpenCodeConfig = await Bun.file('opencode.json').json();
|
||||
|
||||
console.log('Loaded opencode.json configuration');
|
||||
console.log(`Found ${Object.keys(config.provider).length} provider(s)`);
|
||||
|
||||
const results = await Promise.all(
|
||||
Object.entries(config.provider).map(([name, config]) =>
|
||||
testProvider(name, config).catch((error) => {
|
||||
console.error(`Provider ${name} failed: ${error}`);
|
||||
return false;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
console.log('\n=== Summary ===');
|
||||
Object.keys(config.provider).forEach((name, i) => {
|
||||
console.log(`${name}: ${results[i] ? '✓ Success' : '✗ Failed'}`);
|
||||
});
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user