Skip to content

第三方API‐KEY接入指南

binary-husky edited this page Aug 19, 2024 · 32 revisions

Important

高危设置! 常规情况下不要修改!
通过修改以下设置,您将把您的API-KEY和对话隐私完全暴露给您设定的中间人!
如果您不理解其中的风险,请不要修改!

flowchart LR
    A["请使用且仅使用一种方法配置本项目"]
    A --> D["(最最高优先级)修改环境变量"]
    D --> F["直接运行:通过当前终端环境的环境变量"]
    F --> F2["Linux下使用export命令"]
    F --> F1["Windows下使用set命令"]
    D --> E["Docker运行:修改docker-compose.yml(等价于修改容器内部的环境变量)"]
    A --> C["(高优先级)创建并修改config_private.py"]
    A --> B["(低优先级)直接修改config.py"]
Loading

一、格式解释

flowchart LR
    A["api-key"]
    A --> D1["情况1:符合官方格式或azure格式"] 
    D1 --> API_KEY --> B1["OpenAI的秘钥"]
    D1 --> API_URL_REDIRECT --> B2["终结点Endpoint的重新定向"]
    A --> D2["情况2:不符合官方格式或azure格式"]
    D2 --> API_KEY
    D2 --> API_URL_REDIRECT
    D2 --> CUSTOM_API_KEY_PATTERN --> Bx2["API_KEY的正则表达式,或者重复填写一遍API_KEY亦可"]
    D2 --> D3["API_ORG"] --> Bx3["极少数情况下,openai的官方KEY需要伴随组织编码"]
    D1 --> D3["API_ORG"]
Loading

1. 如果api-key符合官方格式或azure格式

解释:apikey满足openai或者azure官方格式

  • 修改config.py或config_private.py
# (通过修改config.py或config_private.py)
LLM_MODEL = "gpt-3.5-turbo"
API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions":"https://xxxx.yyyy.zzzz/v1/chat/completions"}
# 极少数情况下,openai的官方KEY需要伴随组织编码(格式如org-xxxxxxxxxxxxxxxxxxxxxxxx)使用
API_ORG = ""
flowchart LR
    A["配置选项"]
    A --> D1["API_KEY"] --> B1["OpenAI的秘钥"]
    A --> D2["API_URL_REDIRECT"] --> B2["终结点Endpoint的重新定向"]
    A --> D3["API_ORG"] --> B3["极少数情况下,openai的官方KEY需要伴随组织编码"]
Loading
  • 或者修改docker-compose.yml
# (通过修改docker-compose.yml)
version: '3'
services:
  gpt_academic_full_capability:
    image: ghcr.io/binary-husky/gpt_academic_with_all_capacity:master
    environment:
      # 请查阅 `config.py`或者 github wiki 以查看所有的配置信息
      LLM_MODEL: 'gpt-3.5-turbo'
      API_KEY: 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      API_URL_REDIRECT: '{"https://api.openai.com/v1/chat/completions": "https://xxxx.yyyy.zzzz/v1/chat/completions"}'
      API_ORG: ''

有些官方key是API_KEY = "sess-wg61ZafYHpNz7FFwIH7HGZlbVqUVaeV5tatHCWpl"格式的, 这样的APIKEY可以通过修改CUSTOM_API_KEY_PATTERN实现兼容:

# (通过修改config.py或config_private.py)
LLM_MODEL = "gpt-3.5-turbo"
API_KEY = 'sess-wg61ZafYHpNz7FFwIH7HGZlbVqUVaeV5tatHCWpl'
CUSTOM_API_KEY_PATTERN = r"sess-[a-zA-Z0-9]{40}"
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions":"https://xxxx.yyyy.zzzz/v1/chat/completions"}

2. 如果api-key不符合官方格式(或者openai推出了新的密钥格式)

解释:apikey不满足官方格式或azure格式

# (通过修改config.py或config_private.py)
LLM_MODEL = "gpt-3.5-turbo"

API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
CUSTOM_API_KEY_PATTERN = "用于匹配异形APIKEY的正则表达式"
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions":"https://xxxx.yyyy.zzzz/v1/chat/completions"}
flowchart LR
    A["配置选项"]
    A --> D1["API_KEY"] --> B1["OpenAI的秘钥"]
    A --> D2["CUSTOM_API_KEY_PATTERN"] --> B2["API_KEY的正则表达式,或者重复填写一遍API_KEY亦可"]
    A --> D3["API_URL_REDIRECT"] --> B3["终结点Endpoint的重新定向"]
Loading
# (通过修改docker-compose.yml)
version: '3'
services:
  gpt_academic_full_capability:
    image: ghcr.io/binary-husky/gpt_academic_with_all_capacity:master
    environment:
      # 请查阅 `config.py`或者 github wiki 以查看所有的配置信息
      LLM_MODEL: 'gpt-3.5-turbo'
      API_KEY: 'abcdefg-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      CUSTOM_API_KEY_PATTERN: 'your_regex_expression__or__simply_repeat_your_key_here'
      API_URL_REDIRECT: '{"https://api.openai.com/v1/chat/completions": "https://xxxx.yyyy.zzzz/v1/chat/completions"}'

二、举例

例. 第三方接口 bltcy

# (通过修改config.py或config_private.py)
LLM_MODEL = "gpt-3.5-turbo"

API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions": "https://one-api.bltcy.top/v1/chat/completions"}
# (通过修改docker-compose.yml)
version: '3'
services:
  gpt_academic_full_capability:
    image: ghcr.io/binary-husky/gpt_academic_with_all_capacity:master
    environment:
      # 请查阅 `config.py`或者 github wiki 以查看所有的配置信息
      LLM_MODEL: 'gpt-3.5-turbo'
      API_KEY: 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      API_URL_REDIRECT: '{"https://api.openai.com/v1/chat/completions": "https://one-api.bltcy.top/v1/chat/completions"}'

例. 第三方接口 API2D

# (通过修改config.py或config_private.py)
LLM_MODEL = "gpt-3.5-turbo"

API_KEY = 'fk195831-IaP9Pa3WaaCMUIbQwVX6MaSiyawqybyS'
CUSTOM_API_KEY_PATTERN = "fk[a-zA-Z0-9]{6}-[a-zA-Z0-9]{32}$"
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions":"https://openai.api2d.net/v1/chat/completions"}

例:第三方接口 fakeopen

# (通过修改config.py或config_private.py)
API_KEY                = "fk-77ZFXnp2DMjJm9609uI54zNwFrzqf77-UM1kAHIhoqk"
CUSTOM_API_KEY_PATTERN = "fk-77ZFXnp2DMjJm9609uI54zNwFrzqf77-UM1kAHIhoqk"
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions": "https://ai.fakeopen.com/v1/chat/completions"}

例. 第三方接口 zeroai

# (通过修改config.py或config_private.py)
LLM_MODEL = "gpt-3.5-turbo"

API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
API_URL_REDIRECT = {"https://api.openai.com/v1/chat/completions": "https://api.zeroai.link/v1/chat/completions"}