Spaces:
Paused
Paused
Merge branch 'master' of https://github.com/sperjar/gpt_academic into sperjar-master
Browse files- .github/workflows/docker-image.yml +45 -0
- toolbox.py +51 -2
.github/workflows/docker-image.yml
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# https://docs.github.com/en/actions/publishing-packages/publishing-docker-images#publishing-images-to-github-packages
|
2 |
+
name: Create and publish a Docker image
|
3 |
+
|
4 |
+
on:
|
5 |
+
push:
|
6 |
+
branches:
|
7 |
+
- 'master'
|
8 |
+
tags:
|
9 |
+
- 'v*'
|
10 |
+
|
11 |
+
env:
|
12 |
+
REGISTRY: ghcr.io
|
13 |
+
IMAGE_NAME: ${{ github.repository }}
|
14 |
+
|
15 |
+
jobs:
|
16 |
+
build-and-push-image:
|
17 |
+
runs-on: ubuntu-latest
|
18 |
+
permissions:
|
19 |
+
contents: read
|
20 |
+
packages: write
|
21 |
+
|
22 |
+
steps:
|
23 |
+
- name: Checkout repository
|
24 |
+
uses: actions/checkout@v3
|
25 |
+
|
26 |
+
- name: Log in to the Container registry
|
27 |
+
uses: docker/login-action@v2
|
28 |
+
with:
|
29 |
+
registry: ${{ env.REGISTRY }}
|
30 |
+
username: ${{ github.actor }}
|
31 |
+
password: ${{ secrets.GITHUB_TOKEN }}
|
32 |
+
|
33 |
+
- name: Extract metadata (tags, labels) for Docker
|
34 |
+
id: meta
|
35 |
+
uses: docker/metadata-action@v4
|
36 |
+
with:
|
37 |
+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
38 |
+
|
39 |
+
- name: Build and push Docker image
|
40 |
+
uses: docker/build-push-action@v4
|
41 |
+
with:
|
42 |
+
context: .
|
43 |
+
push: true
|
44 |
+
tags: ${{ steps.meta.outputs.tags }}
|
45 |
+
labels: ${{ steps.meta.outputs.labels }}
|
toolbox.py
CHANGED
@@ -3,6 +3,7 @@ import importlib
|
|
3 |
import traceback
|
4 |
import inspect
|
5 |
import re
|
|
|
6 |
from latex2mathml.converter import convert as tex2mathml
|
7 |
from functools import wraps, lru_cache
|
8 |
|
@@ -517,13 +518,61 @@ def select_api_key(keys, llm_model):
|
|
517 |
api_key = random.choice(avail_key_list) # 随机负载均衡
|
518 |
return api_key
|
519 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
520 |
@lru_cache(maxsize=128)
|
521 |
def read_single_conf_with_lru_cache(arg):
|
522 |
from colorful import print亮红, print亮绿, print亮蓝
|
|
|
523 |
try:
|
524 |
-
r =
|
525 |
except:
|
526 |
-
|
|
|
|
|
|
|
527 |
# 在读取API_KEY时,检查一下是不是忘了改config
|
528 |
if arg == 'API_KEY':
|
529 |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
|
|
|
3 |
import traceback
|
4 |
import inspect
|
5 |
import re
|
6 |
+
import os
|
7 |
from latex2mathml.converter import convert as tex2mathml
|
8 |
from functools import wraps, lru_cache
|
9 |
|
|
|
518 |
api_key = random.choice(avail_key_list) # 随机负载均衡
|
519 |
return api_key
|
520 |
|
521 |
+
def read_single_conf_from_env(arg, default_value):
|
522 |
+
ENV_PREFIX = "GPT_ACADEMIC_" # 环境变量的前缀
|
523 |
+
env_arg = ENV_PREFIX + arg # 环境变量的KEY
|
524 |
+
if arg == "proxies":
|
525 |
+
# 对于proxies,我们使用多个环境变量来配置
|
526 |
+
# HTTP_PROXY: 对应http代理
|
527 |
+
# HTTPS_PROXY: 对应https代理
|
528 |
+
# ALL_PROXY: 对应http和https代理,优先级较HTTP_PROXY和HTTPS_PROXY更低
|
529 |
+
http_proxy = os.environ.get(ENV_PREFIX + "HTTP_PROXY") or os.environ.get("ALL_PROXY")
|
530 |
+
assert http_proxy is not None, f"请设置环境变量{ENV_PREFIX + 'HTTP_PROXY'}"
|
531 |
+
https_proxy = os.environ.get(ENV_PREFIX + "HTTPS_PROXY") or os.environ.get("ALL_PROXY")
|
532 |
+
assert https_proxy is not None, f"请设置环境变量{ENV_PREFIX + 'HTTPS_PROXY'}"
|
533 |
+
r = {
|
534 |
+
"http": http_proxy,
|
535 |
+
"https": https_proxy
|
536 |
+
}
|
537 |
+
elif arg == "AVAIL_LLM_MODELS":
|
538 |
+
r = []
|
539 |
+
# 对于AVAIL_LLM_MODELS的环境变量配置,我们允许用户使用;分隔多个模型
|
540 |
+
for item in os.environ[env_arg].split(";"):
|
541 |
+
r.append(item)
|
542 |
+
elif arg == "AUTHENTICATION":
|
543 |
+
r = []
|
544 |
+
# 对于AUTHENTICATION的环境变量配置,我们允许用户使用;分隔多个账号
|
545 |
+
# 格式为:username1:password1;username2:password2
|
546 |
+
for item in os.environ[env_arg].split(";"):
|
547 |
+
r.append(tuple(item.split(":")))
|
548 |
+
elif arg == "API_URL_REDIRECT":
|
549 |
+
# 对于API_URL_REDIRECT的环境变量,我们允许用户使用json格式配置多个url重定向
|
550 |
+
# 格式为一个json字符串,例如:{"https://api.openai.com/v1/chat/completions": "https://ai.open.com/api/conversation"}
|
551 |
+
import json
|
552 |
+
r = json.loads(os.environ[env_arg])
|
553 |
+
elif isinstance(default_value, bool):
|
554 |
+
r = bool(os.environ[env_arg])
|
555 |
+
elif isinstance(default_value, int):
|
556 |
+
r = int(os.environ[env_arg])
|
557 |
+
elif isinstance(default_value, float):
|
558 |
+
r = float(os.environ[env_arg])
|
559 |
+
elif isinstance(default_value, str):
|
560 |
+
r = os.environ[env_arg]
|
561 |
+
else:
|
562 |
+
raise RuntimeError(f"[CONFIG] 环境变量{arg}不支持自动转换到{type(default_value)}类型")
|
563 |
+
return r
|
564 |
+
|
565 |
@lru_cache(maxsize=128)
|
566 |
def read_single_conf_with_lru_cache(arg):
|
567 |
from colorful import print亮红, print亮绿, print亮蓝
|
568 |
+
default_r = getattr(importlib.import_module('config'), arg)
|
569 |
try:
|
570 |
+
r = read_single_conf_from_env(arg, default_r) # 优先获取环境变量作为配置
|
571 |
except:
|
572 |
+
try:
|
573 |
+
r = getattr(importlib.import_module('config_private'), arg)
|
574 |
+
except:
|
575 |
+
r = default_r
|
576 |
# 在读取API_KEY时,检查一下是不是忘了改config
|
577 |
if arg == 'API_KEY':
|
578 |
print亮蓝(f"[API_KEY] 本项目现已支持OpenAI和API2D的api-key。也支持同时填写多个api-key,如API_KEY=\"openai-key1,openai-key2,api2d-key3\"")
|