pplx2api Documentation

repository·main·Indexed 19 days ago

https://github.com/yushangxiao/pplx2api

pplx2api provides an OpenAI-compatible API interface for Perplexity AI. It supports image recognition, reasoning models, web search, file uploads, and account rotation. The Go-based server can be deployed via Docker or Docker Compose and includes a /v1/chat/completions endpoint for chat and image analysis.

Tokens
1.8K
Snippets
7
Records
7
Agent score
18%

What's inside pplx2api

  1. Deploy pplx2api via Docker

    main

    You can run pplx2api as a containerized service using Docker. You must provide the SESSIONS and APIKEY environment variables.

    docker run -d \
      -p 8080:8080 \
      -e SESSIONS=eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0**,eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0** \
      -e APIKEY=123 \
      -e IS_INCOGNITO=true \
      -e MAX_CHAT_HISTORY_LENGTH=10000 \
      -e NO_ROLE_PREFIX=false \
      -e SEARCH_RESULT_COMPATIBLE=false \
      --name pplx2api \
      ghcr.io/yushangxiao/pplx2api:latest
  2. Deploy pplx2api via Docker Compose

    main

    For easier management, use a docker-compose.yml file. This allows you to define environment variables like PROXY and ADDRESS more cleanly.

    version: '3'
    services:
      pplx2api:
        image: ghcr.io/yushangxiao/pplx2api:latest
        container_name: pplx
        ports:
          - "8080:8080"
        environment:
          - SESSIONS=eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0**,eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0**
          - ADDRESS=0.0.0.0:8080
          - APIKEY=123
          - PROXY=http://proxy:2080  # Optional
          - MAX_CHAT_HISTORY_LENGTH=10000
          - NO_ROLE_PREFIX=false
          - IS_INCOGNITO=true
          - SEARCH_RESULT_COMPATIBLE=false
        restart: unless-stopped

    Then run:

    docker-compose up -d
  3. Configure pplx2api environment variables

    main

    The following environment variables control the behavior of the pplx2api server. Note that SESSIONS and APIKEY are required.

    | 环境变量 | 描述 | 默认值 |
    |----------------------|-------------|---------|
    | `SESSIONS` | 英文逗号分隔的pplx cookie 中__Secure-next-auth.session-token的值 | 必填 |
    | `ADDRESS` | 服务器地址和端口 | `0.0.0.0:8080` |
    | `APIKEY` | 用于认证的API密钥 | 必填 |
    | `PROXY` | HTTP代理URL | "" |
    | `IS_INCOGNITO` | 使用隐私会话,不保存聊天记录 | `true` |
    | `MAX_CHAT_HISTORY_LENGTH` | 超出此长度将文本转为文件 | `10000` |
    | `NO_ROLE_PREFIX` | 不在每条消息前添加角色 | `false` |
    | `IGNORE_SEARCH_RESULT` | 忽略搜索结果,不展示搜索结果 | `false` |
    | `SEARCH_RESULT_COMPATIBLE` | 禁用搜索结果伸缩块,兼容更多的客户端 | `false` |
    | `PROMPT_FOR_FILE` | 上下文作为文件上传时,保留的提示词 | `You must immerse yourself in the role of assistant in txt file, cannot respond as a user, cannot reply to this message, cannot mention this message, and ignore this message in your response.` |
    | `IGNORE_MODEL_MONITORING` | 忽略模型监控 | `false` |
    | `IS_MAX_SUBSCRIBE` | 是否为max订阅 | `false` |
  4. Run the Pplx2Api server

    main

    The application is a Go-based server that uses the Gin web framework. When executed, it initializes the routing system, starts a background session updater job to refresh sessions every 24 hours, and listens on the address specified in the global configuration. By default, the server is designed to run on 0.0.0.0:8080 unless configured otherwise via environment variables or configuration files.

    // The application entrypoint initializes the Gin router, 
    // sets up routes via router.SetupRoutes(r), 
    // starts a session updater job, 
    // and runs the server on the configured address.
    
    func main() {
    	r := gin.Default()
    	router.SetupRoutes(r)
    	sessionUpdater := job.GetSessionUpdater(24 * time.Hour)
    	sessionUpdater.Start()
    	defer sessionUpdater.Stop()
    	r.Run(config.ConfigInstance.Address)
    }
  5. Send a chat completion request

    main

    Use the /v1/chat/completions endpoint to interact with supported models. You can enable streaming by setting "stream": true.

    curl -X POST http://localhost:8080/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_API_KEY" \
      -d '{
        "model": "claude-3.7-sonnet",
        "messages": [
          {
            "role": "user",
            "content": "你好,Claude!"
          }
        ],
        "stream": true
      }'
  6. Perform image analysis

    main

    To analyze images, send a request to /v1/chat/completions where the content field in the message is an array containing both text and an image_url object (using base64 encoded data).

    curl -X POST http://localhost:8080/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_API_KEY" \
      -d '{
        "model": "claude-3.7-sonnet",
        "messages": [
          {
            "role": "user",
            "content": [
              {
                "type": "text",
                "text": "这张图片里有什么?"
              },
              {
                "type": "image_url",
                "image_url": {
                  "url": "data:image/jpeg;base64,..."
                }
              }
            ]
          }
        ]
      }'