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:
16
src/index.ts
Normal file
16
src/index.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
// 使用自定义 doubao provider
|
||||
const doubao = createOpenAICompatible({
|
||||
baseURL: 'https://ark.cn-beijing.volces.com/api/coding/v3',
|
||||
name: 'custom-doubao',
|
||||
apiKey: process.env.DOUBAO_API_KEY!,
|
||||
});
|
||||
|
||||
const { text } = await generateText({
|
||||
model: doubao('ark-code-latest'),
|
||||
prompt: 'What is an agent?',
|
||||
});
|
||||
|
||||
console.log(text);
|
||||
33
src/test-bailian-debug.ts
Normal file
33
src/test-bailian-debug.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'dotenv/config';
|
||||
|
||||
const endpoint = 'https://coding.dashscope.aliyuncs.com/apps/anthropic/messages';
|
||||
|
||||
// 测试不同的认证方式
|
||||
const authMethods = [
|
||||
{ name: 'x-api-key', headers: { 'x-api-key': process.env.BAILIAN_CODE_API_KEY! } },
|
||||
{ name: 'Authorization Bearer', headers: { 'Authorization': `Bearer ${process.env.BAILIAN_CODE_API_KEY!}` } },
|
||||
{ name: 'Authorization Bearer sk-', headers: { 'Authorization': `Bearer sk-${process.env.BAILIAN_CODE_API_KEY!}` } },
|
||||
{ name: 'Authorization sk-', headers: { 'Authorization': `sk-${process.env.BAILIAN_CODE_API_KEY!}` } },
|
||||
];
|
||||
|
||||
for (const { name, headers } of authMethods) {
|
||||
console.log(`\nTesting: ${name}`);
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'qwen3-coder-plus',
|
||||
max_tokens: 100,
|
||||
messages: [{ role: 'user', content: 'Hello' }],
|
||||
}),
|
||||
});
|
||||
|
||||
console.log(`Status: ${response.status}`);
|
||||
const text = await response.text();
|
||||
if (text) {
|
||||
console.log(`Response: ${text.substring(0, 300)}`);
|
||||
}
|
||||
}
|
||||
23
src/test-bailian.ts
Normal file
23
src/test-bailian.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { createAnthropic } from '@ai-sdk/anthropic';
|
||||
import { generateText } from 'ai';
|
||||
import 'dotenv/config';
|
||||
|
||||
// 尝试不同的端点配置
|
||||
const bailian = createOpenAICompatible({
|
||||
baseURL: 'https://coding.dashscope.aliyuncs.com/v1',
|
||||
name: 'custom-bailian',
|
||||
apiKey: process.env.BAILIAN_CODE_API_KEY!,
|
||||
});
|
||||
// const bailian = createAnthropic({
|
||||
// baseURL: 'https://coding.dashscope.aliyuncs.com/apps/anthropic/messages',
|
||||
// name: 'custom-bailian',
|
||||
// apiKey: process.env.BAILIAN_CODE_API_KEY!,
|
||||
// });
|
||||
|
||||
const { text } = await generateText({
|
||||
model: bailian('qwen3-coder-plus'),
|
||||
prompt: 'What is an agent?',
|
||||
});
|
||||
|
||||
console.log('Response:', text);
|
||||
46
src/test-cnb-debug.ts
Normal file
46
src/test-cnb-debug.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'dotenv/config';
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
const endpoint = 'https://cnb.cool/kevisual/cnb/-/ai/chat/completions';
|
||||
const apiKey = process.env.CNB_API_KEY!;
|
||||
|
||||
console.log('Endpoint:', endpoint);
|
||||
console.log('API Key:', apiKey ? 'Set' : 'Not set');
|
||||
console.log('API Key length:', apiKey.length);
|
||||
console.log('API Key prefix:', apiKey.substring(0, 10) + '...');
|
||||
|
||||
// 尝试不同的认证头
|
||||
const authHeaders = [
|
||||
{ name: 'Authorization Bearer', headers: { 'Authorization': `Bearer ${apiKey}` } },
|
||||
{ name: 'Authorization (no Bearer)', headers: { 'Authorization': apiKey } },
|
||||
{ name: 'x-api-key', headers: { 'x-api-key': apiKey } },
|
||||
{ name: 'api-key', headers: { 'api-key': apiKey } },
|
||||
];
|
||||
|
||||
for (const { name, headers } of authHeaders) {
|
||||
console.log(`\n=== Testing: ${name} ===`);
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'hunyuan-a13b',
|
||||
messages: [{ role: 'user', content: '你好' }],
|
||||
}),
|
||||
});
|
||||
|
||||
console.log(`Status: ${response.status}`);
|
||||
const text = await response.text();
|
||||
console.log('Response:', text);
|
||||
}
|
||||
28
src/test-cnb.ts
Normal file
28
src/test-cnb.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { generateText } from 'ai';
|
||||
import 'dotenv/config';
|
||||
|
||||
// 解析环境变量占位符
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
// 使用 custom-cnb provider
|
||||
const cnb = createOpenAICompatible({
|
||||
baseURL: resolveEnvVars('{env:CNB_API_ENDPOINT}/{env:CNB_REPO_SLUG}/-/ai/'),
|
||||
name: 'custom-cnb',
|
||||
apiKey: process.env.CNB_API_KEY!,
|
||||
});
|
||||
|
||||
const { text } = await generateText({
|
||||
model: cnb('hunyuan-a13b'),
|
||||
prompt: 'What is an agent?',
|
||||
});
|
||||
|
||||
console.log('Response:', text);
|
||||
26
src/test-kevisual.ts
Normal file
26
src/test-kevisual.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
||||
import { generateText } from 'ai';
|
||||
import 'dotenv/config';
|
||||
|
||||
// 使用 custom-kevisual provider (聚合了多个模型)
|
||||
const kevisual = createOpenAICompatible({
|
||||
baseURL: 'https://newapi.kevisual.cn/v1',
|
||||
name: 'custom-kevisual',
|
||||
apiKey: process.env.KEVISUAL_NEW_API_KEY!,
|
||||
});
|
||||
|
||||
// 测试所有可用模型
|
||||
const models = ['qwen3-coder-plus', 'ark-code-latest', 'GLM-4.7', 'MiniMax-M2.1'];
|
||||
|
||||
for (const model of models) {
|
||||
console.log(`\n=== Testing model: ${model} ===`);
|
||||
try {
|
||||
const { text } = await generateText({
|
||||
model: kevisual(model),
|
||||
prompt: 'Say hello in one sentence.',
|
||||
});
|
||||
console.log(`Response: ${text}`);
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user