---
title: "OpenAI 连接器"
description: "完整的 OpenAI 集成指南，通过 Tajo 利用 AI 驱动的内容生成、客户洞察和智能营销自动化与 Brevo 协同工作。"
url: https://tajo.io/zh-cn/docs/connectors/custom/openai/
markdown: https://tajo.io/zh-cn/docs/connectors/custom/openai.md
lang: zh-CN
robots: noindex
---

申请抢先体验

请输入您的名字和公司电子邮箱地址。我们会发送确认 Tajo 访问申请的链接。

# OpenAI 连接器

连接器状态: 计划中 尚未构建。Tajo 目前不运行此连接器。

| 来源字段 | 目标字段 | 示例值 |
| --- | --- | --- |
| `record.id` | `source_id` | `record_1042` |
| `record.updated_at` | `updated_at` | `2026-10-18T08:00:00Z` |
| `record.value` | `value` | `sample_value` |

通过 Tajo 将 OpenAI 连接到 Brevo，借助 AI 驱动的内容生成、客户情感分析、智能细分和预测分析，提升您的营销自动化工作流。

## 概览

| 属性 | 值 |
| --- | --- |
| **平台** | OpenAI |
| **类别** | AI / ML（自定义） |
| **设置复杂度** | 中等 |
| **官方集成** | 否 |
| **同步数据** | 内容、向量嵌入、洞察、预测 |
| **认证方式** | API 密钥（Bearer 令牌） |

## 功能

- **AI 内容生成** - 使用 GPT 模型生成邮件主题行、正文内容和行动号召
- **客户情感分析** - 分析支持工单和反馈以获得情感评分
- **智能细分** - 使用向量嵌入按行为模式聚类客户
- **预测分析** - 预测流失、LTV 和购买倾向
- **多语言内容** - 以任何支持的语言生成营销内容
- **图像生成** - 通过 DALL-E 集成创建活动视觉素材

## 前提条件

开始之前，请确保您已具备：

1. 具有 API 访问权限的 OpenAI 账户
2. 来自 [OpenAI 控制台](https://platform.openai.com/api-keys) 的 API 密钥
3. 具有 API 访问权限的 Brevo 账户
4. 具有连接器权限的 Tajo 账户
5. 足够的 OpenAI API 额度以满足预期用量

## 认证

### API 密钥认证

OpenAI 对所有 API 请求使用 Bearer 令牌认证：

```bash
# Set your API keys
export OPENAI_API_KEY=sk-your-api-key
export TAJO_API_KEY=your_tajo_api_key
export BREVO_API_KEY=your_brevo_api_key
```

```javascript
// All requests require the Authorization header
const headers = {
  'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
  'Content-Type': 'application/json'
};

// For organization-scoped access
const orgHeaders = {
  ...headers,
  'OpenAI-Organization': process.env.OPENAI_ORG_ID,
  'OpenAI-Project': process.env.OPENAI_PROJECT_ID
};
```

API 密钥安全

切勿在客户端代码中暴露您的 OpenAI API 密钥。始终使用环境变量和服务器端请求。定期通过 OpenAI 控制台轮换密钥。

## 配置

### 基础设置

```yaml
connectors:
  openai:
    enabled: true
    model: "gpt-4o"
    embedding_model: "text-embedding-3-small"
    image_model: "dall-e-3"

    features:
      content_generation: true
      sentiment_analysis: true
      smart_segmentation: true
      predictive_analytics: true

    limits:
      max_tokens_per_request: 4096
      max_requests_per_minute: 60
      temperature: 0.7
```

### 内容生成模板

```yaml
templates:
  email_subject:
    model: "gpt-4o"
    system_prompt: |
      You are an expert email marketer. Generate compelling
      subject lines that drive open rates.
    max_tokens: 100
    temperature: 0.8

  email_body:
    model: "gpt-4o"
    system_prompt: |
      Generate personalized email content based on customer
      data and campaign objectives.
    max_tokens: 2048
    temperature: 0.7
```

## API 端点

| 端点 | 方法 | 描述 |
| --- | --- | --- |
| `https://api.openai.com/v1/responses` | POST | 创建 AI 响应（Responses API） |
| `https://api.openai.com/v1/chat/completions` | POST | 生成文本补全 |
| `https://api.openai.com/v1/embeddings` | POST | 创建文本向量嵌入 |
| `https://api.openai.com/v1/images/generations` | POST | 生成图像 |
| `https://api.openai.com/v1/audio/speech` | POST | 文字转语音 |
| `https://api.openai.com/v1/audio/transcriptions` | POST | 语音转文字 |
| `https://api.openai.com/v1/moderations` | POST | 内容审核 |
| `https://api.openai.com/v1/models` | GET | 列出可用模型 |

## 代码示例

### 初始化连接器

```javascript
import { TajoClient } from '@tajo/sdk';

const tajo = new TajoClient({
  apiKey: process.env.TAJO_API_KEY,
  brevoApiKey: process.env.BREVO_API_KEY
});

await tajo.connectors.connect('openai', {
  apiKey: process.env.OPENAI_API_KEY,
  defaultModel: 'gpt-4o'
});
```

### 生成邮件内容

```javascript
// Generate personalized email subject lines
const response = await fetch('https://api.openai.com/v1/chat/completions', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model: 'gpt-4o',
    messages: [
      {
        role: 'system',
        content: 'Generate 5 compelling email subject lines for a product launch.'
      },
      {
        role: 'user',
        content: `Product: ${product.name}. Target: ${segment.description}.`
      }
    ],
    max_tokens: 200,
    temperature: 0.8
  })
});

const result = await response.json();
const subjectLines = result.choices[0].message.content;
```

### 客户情感分析

```javascript
// Analyze customer feedback sentiment
const sentimentAnalysis = await fetch(
  'https://api.openai.com/v1/chat/completions',
  {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'gpt-4o',
      messages: [
        {
          role: 'system',
          content: 'Analyze sentiment. Return JSON: {score: -1 to 1, label: string, topics: string[]}'
        },
        { role: 'user', content: customerFeedback }
      ],
      response_format: { type: 'json_object' },
      max_tokens: 150
    })
  }
);

const sentiment = await sentimentAnalysis.json();
await tajo.contacts.update(email, {
  attributes: { SENTIMENT_SCORE: JSON.parse(sentiment.choices[0].message.content).score }
});
```

### 基于向量嵌入的智能细分

```javascript
// Generate embeddings for customer clustering
const embeddingResponse = await fetch(
  'https://api.openai.com/v1/embeddings',
  {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'text-embedding-3-small',
      input: customerDescriptions,
      dimensions: 256
    })
  }
);

const embeddings = await embeddingResponse.json();
// Use embeddings for similarity-based customer clustering
```

## 速率限制

| 模型 | RPM（请求/分钟） | TPM（令牌/分钟） | RPD（请求/天） |
| --- | --- | --- | --- |
| gpt-4o | 500 | 30,000 | 10,000 |
| gpt-4o-mini | 500 | 200,000 | 10,000 |
| text-embedding-3-small | 500 | 1,000,000 | 10,000 |
| dall-e-3 | 5 | N/A | 200 |

速率限制头部

监控 API 响应中的速率限制头部（`x-ratelimit-remaining-requests`、`x-ratelimit-remaining-tokens`），实施主动限流以避免 429 错误。

## 故障排除

| 问题 | 原因 | 解决方案 |
| --- | --- | --- |
| 401 Unauthorized | API 密钥无效 | 在 OpenAI 控制台中验证密钥 |
| 429 Rate Limited | 请求过多 | 实施指数退避 |
| 500 Server Error | OpenAI 故障 | 检查 status.openai.com 并重试 |
| 响应被截断 | max\_tokens 过低 | 增加 max\_tokens 参数 |
| 内容质量差 | 温度过高 | 降低温度以提高一致性 |

### 调试模式

```yaml
connectors:
  openai:
    debug: true
    log_level: verbose
    log_prompts: false  # Don't log prompts in production
    log_usage: true
```

## 最佳实践

1. **缓存响应** - 存储生成的内容以减少 API 调用和成本
2. **使用结构化输出** - 请求 JSON 响应以便可靠解析
3. **实施重试逻辑** - 使用指数退避处理速率限制
4. **监控令牌用量** - 追踪消耗量以控制成本
5. **使用合适的模型** - 简单任务用 gpt-4o-mini，复杂任务用 gpt-4o
6. **验证输出** - 在向客户发送前始终验证 AI 生成的内容

## 安全

- **Bearer 令牌认证** - API 密钥通过 Authorization 头部传输
- **仅服务器端** - 切勿在客户端代码中暴露 API 密钥
- **密钥轮换** - 定期通过 OpenAI 控制台轮换 API 密钥
- **用量监控** - 在 OpenAI 计费设置中设置消费限制
- **内容审核** - 使用 Moderations API 过滤不安全内容
- **数据隐私** - 根据使用场景审阅 OpenAI 的数据使用政策

## 相关资源

- [Brevo 连接器](https://tajo.io/zh-cn/docs/connectors/marketing/brevo)
- [客户同步技能](https://tajo.io/zh-cn/docs/skills/data-sync/customer-sync)
- [邮件营销技能](https://tajo.io/zh-cn/docs/skills/email-marketing)
- [OpenAI API 参考文档](https://platform.openai.com/docs/api-reference)
