> For the complete documentation index, see [llms.txt](https://docs.ipwo.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-openai-sdk-zhong-shi-yong-ipwo-dai-li.md).

# 如何在 OpenAI SDK 中使用 IPWO 代理

在部分网络环境下，开发者可能需要通过代理服务器访问 OpenAI API。通过配置 IPWO 住宅代理，可以为 OpenAI SDK 提供稳定的网络连接，并根据业务需求选择不同国家或地区的出口 IP。

本文将介绍如何在 Python 和 Node.js 环境中为 OpenAI SDK 配置 IPWO 代理。

***

### 环境准备

在开始之前，请确保您已经：

* 拥有 [IPWO 住宅代理账号](/readme/zhu-ce-deng-lu.md)
* [获取代理用户名和密码](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/huo-qu-dai-li-xin-xi.md)
* 获取代理服务器地址和端口
* 拥有 OpenAI API Key

示例代理信息：

```
Proxy Host: us.ipwo.net
Proxy Port: 7878

Username: username
Password: password
```

***

### Python 示例

安装依赖：

```bash
pip install openai httpx
```

创建客户端：

```python
from openai import OpenAI
import httpx

proxy_url = "http://username:password@us.ipwo.net:7878"

http_client = httpx.Client(
    proxy=proxy_url
)

client = OpenAI(
    api_key="YOUR_OPENAI_API_KEY",
    http_client=http_client
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "user",
            "content": "Hello"
        }
    ]
)

print(response.choices[0].message.content)
```

***

### Python 异步示例

```python
from openai import AsyncOpenAI
import httpx
import asyncio

proxy_url = "http://username:password@us.ipwo.net:7878"

async def main():
    async_client = httpx.AsyncClient(
        proxy=proxy_url
    )

    client = AsyncOpenAI(
        api_key="YOUR_OPENAI_API_KEY",
        http_client=async_client
    )

    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "user",
                "content": "Hello"
            }
        ]
    )

    print(response.choices[0].message.content)

asyncio.run(main())
```

***

### Node.js 示例

安装依赖：

```bash
npm install openai https-proxy-agent
```

创建客户端：

```javascript
import OpenAI from "openai";
import { HttpsProxyAgent } from "https-proxy-agent";

const proxyAgent = new HttpsProxyAgent(
  "http://username:password@us.ipwo.net:7878"
);

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  httpAgent: proxyAgent
});

const response = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    {
      role: "user",
      content: "Hello"
    }
  ]
});

console.log(response.choices[0].message.content);
```

***

### 使用环境变量配置代理

部分应用程序会自动读取系统代理配置。

Linux/macOS：

```bash
export HTTP_PROXY=http://username:password@us.ipwo.net:7878
export HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

Windows：

```cmd
set HTTP_PROXY=http://username:password@us.ipwo.net:7878
set HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

***

### [验证代理](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/kuai-su-yan-zheng.md)是否生效

在调用 OpenAI API 之前，可以先验证代理连接是否正常。

使用 curl 测试：

```bash
curl -x "us.ipwo.net:7878" \
-U "username:password" \
ipinfo.io
```

返回结果中应显示代理出口 IP 的地理位置和网络运营商信息。

***

### 常见问题

#### 出现 Connection Timeout

可能原因：

* 本地网络无法访问代理节点
* 代理账号已过期
* 防火墙拦截连接

建议：

* 检查代理账号状态
* 更换网络环境后重试
* 使用 curl 测试代理连接

***

#### 出现 Authentication Failed

可能原因：

* 用户名错误
* 密码错误
* 代理认证信息格式不正确

建议检查：

```
username
password
host
port
```

是否与控制台提供的信息一致。

***

#### OpenAI API 返回 401

此错误通常与 OpenAI API Key 有关，而非代理配置问题。

请检查：

```
OPENAI_API_KEY
```

是否有效，并确认账户具备对应模型的调用权限。

***

### 最佳实践

* 长时间运行的 AI 应用建议使用 Sticky Session 保持连接稳定。
* 高并发请求建议合理控制请求频率，避免短时间内建立大量连接。
* 在生产环境中，建议通过环境变量管理 API Key 和代理凭证，避免硬编码敏感信息。
* 部署在 Docker、云服务器或自动化工作流平台时，可统一使用系统代理配置，简化维护成本。

***

### 相关阅读

如果您正在构建 AI 应用或自动化工作流，以下内容可能对您有所帮助：

* [获取和配置住宅代理](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/huo-qu-dai-li-xin-xi.md)
* [如何检测代理是否生效](/dai-li-jian-ce-yu-yin-si-an-quan/ru-he-jian-ce-dai-li-shi-fou-sheng-xiao.md)
* [Claude API 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-claude-api-zhong-shi-yong-ipwo-dai-li.md)
* [Gemini API 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-gemini-api-zhong-shi-yong-ipwo-dai-li.md)
* [Python 使用住宅代理](/kai-fa-zhe-wen-dang/dai-ma-shi-li/python.md)
* [Node.js 使用住宅代理](/kai-fa-zhe-wen-dang/dai-ma-shi-li/node.js.md)
* [cURL 使用住宅代理](/kai-fa-zhe-wen-dang/dai-ma-shi-li/curl-dai-ma-shi-li.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-openai-sdk-zhong-shi-yong-ipwo-dai-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
