
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
@cherrystudio/ai-core
Advanced tools
Cherry Studio AI Core - Unified AI Provider Interface Based on Vercel AI SDK
Cherry Studio AI Core 是一个基于 Vercel AI SDK 的统一 AI Provider 接口包。
核心 Providers(内置支持):
扩展 Providers(通过注册API支持):
npm install @cherrystudio/ai-core ai
如果你在 React Native 项目中使用此包,需要在 metro.config.js
中添加以下配置:
// metro.config.js
const { getDefaultConfig } = require('expo/metro-config')
const config = getDefaultConfig(__dirname)
// 添加对 @cherrystudio/ai-core 的支持
config.resolver.resolverMainFields = ['react-native', 'browser', 'main']
config.resolver.platforms = ['ios', 'android', 'native', 'web']
module.exports = config
还需要安装你要使用的 AI SDK provider:
npm install @ai-sdk/openai @ai-sdk/anthropic @ai-sdk/google
import { AiCore } from '@cherrystudio/ai-core'
// 创建 OpenAI executor
const executor = AiCore.create('openai', {
apiKey: 'your-api-key'
})
// 流式生成
const result = await executor.streamText('gpt-4', {
messages: [{ role: 'user', content: 'Hello!' }]
})
// 非流式生成
const response = await executor.generateText('gpt-4', {
messages: [{ role: 'user', content: 'Hello!' }]
})
import { createOpenAIExecutor } from '@cherrystudio/ai-core'
// 快速创建 OpenAI executor
const executor = createOpenAIExecutor({
apiKey: 'your-api-key'
})
// 使用 executor
const result = await executor.streamText('gpt-4', {
messages: [{ role: 'user', content: 'Hello!' }]
})
import { AiCore } from '@cherrystudio/ai-core'
// 支持多种 AI providers
const openaiExecutor = AiCore.create('openai', { apiKey: 'openai-key' })
const anthropicExecutor = AiCore.create('anthropic', { apiKey: 'anthropic-key' })
const googleExecutor = AiCore.create('google', { apiKey: 'google-key' })
const xaiExecutor = AiCore.create('xai', { apiKey: 'xai-key' })
对于非内置的 providers,可以通过注册 API 扩展支持:
import { registerProvider, AiCore } from '@cherrystudio/ai-core'
// 方式一:导入并注册第三方 provider
import { createGroq } from '@ai-sdk/groq'
registerProvider({
id: 'groq',
name: 'Groq',
creator: createGroq,
supportsImageGeneration: false
})
// 现在可以使用 Groq
const groqExecutor = AiCore.create('groq', { apiKey: 'groq-key' })
// 方式二:动态导入方式注册
registerProvider({
id: 'mistral',
name: 'Mistral AI',
import: () => import('@ai-sdk/mistral'),
creatorFunctionName: 'createMistral'
})
const mistralExecutor = AiCore.create('mistral', { apiKey: 'mistral-key' })
https://ai-sdk.dev/docs/reference/ai-sdk-core/provider-registry
除了使用内建的 provider 管理,你还可以使用 AI SDK 原生的 createProviderRegistry
来构建自己的 provider 注册表。
import { createClient } from '@cherrystudio/ai-core'
import { createProviderRegistry } from 'ai'
import { createOpenAI } from '@ai-sdk/openai'
import { anthropic } from '@ai-sdk/anthropic'
// 1. 创建 AI SDK 原生注册表
export const registry = createProviderRegistry({
// register provider with prefix and default setup:
anthropic,
// register provider with prefix and custom setup:
openai: createOpenAI({
apiKey: process.env.OPENAI_API_KEY
})
})
// 2. 创建client,'openai'可以传空或者传providerId(内建的provider)
const client = PluginEnabledAiClient.create('openai', {
apiKey: process.env.OPENAI_API_KEY
})
// 3. 方式1:使用内建逻辑(传统方式)
const result1 = await client.streamText('gpt-4', {
messages: [{ role: 'user', content: 'Hello with built-in logic!' }]
})
// 4. 方式2:使用自定义注册表(灵活方式)
const result2 = await client.streamText({
model: registry.languageModel('openai:gpt-4'),
messages: [{ role: 'user', content: 'Hello with custom registry!' }]
})
// 5. 支持的重载方法
await client.generateObject({
model: registry.languageModel('openai:gpt-4'),
schema: z.object({ name: z.string() }),
messages: [{ role: 'user', content: 'Generate a user' }]
})
await client.streamObject({
model: registry.languageModel('anthropic:claude-3-opus-20240229'),
schema: z.object({ items: z.array(z.string()) }),
messages: [{ role: 'user', content: 'Generate a list' }]
})
更强大的是,你还可以将自定义注册表与 Cherry Studio 的插件系统结合使用:
import { PluginEnabledAiClient } from '@cherrystudio/ai-core'
import { createProviderRegistry } from 'ai'
import { createOpenAI } from '@ai-sdk/openai'
import { anthropic } from '@ai-sdk/anthropic'
// 1. 创建带插件的客户端
const client = PluginEnabledAiClient.create(
'openai',
{
apiKey: process.env.OPENAI_API_KEY
},
[LoggingPlugin, RetryPlugin]
)
// 2. 创建自定义注册表
const registry = createProviderRegistry({
openai: createOpenAI({ apiKey: process.env.OPENAI_API_KEY }),
anthropic: anthropic({ apiKey: process.env.ANTHROPIC_API_KEY })
})
// 3. 方式1:使用内建逻辑 + 完整插件系统
await client.streamText('gpt-4', {
messages: [{ role: 'user', content: 'Hello with plugins!' }]
})
// 4. 方式2:使用自定义注册表 + 有限插件支持
await client.streamText({
model: registry.languageModel('anthropic:claude-3-opus-20240229'),
messages: [{ role: 'user', content: 'Hello from Claude!' }]
})
// 5. 支持的方法
await client.generateObject({
model: registry.languageModel('openai:gpt-4'),
schema: z.object({ name: z.string() }),
messages: [{ role: 'user', content: 'Generate a user' }]
})
await client.streamObject({
model: registry.languageModel('openai:gpt-4'),
schema: z.object({ items: z.array(z.string()) }),
messages: [{ role: 'user', content: 'Generate a list' }]
})
createProviderRegistry
APIMIT
FAQs
Cherry Studio AI Core - Unified AI Provider Interface Based on Vercel AI SDK
The npm package @cherrystudio/ai-core receives a total of 132 weekly downloads. As such, @cherrystudio/ai-core popularity was classified as not popular.
We found that @cherrystudio/ai-core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.