From 16cf2bf653987b23fd63634b7591b30f8cf2933e Mon Sep 17 00:00:00 2001 From: fatwang2 Date: Mon, 18 Mar 2024 00:30:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 25 ------------------------- README.md | 2 +- requirements.txt | 5 +++-- search4all.py | 24 +++++++++++++----------- 4 files changed, 17 insertions(+), 39 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 02f192a..0000000 --- a/Dockerfile +++ /dev/null @@ -1,25 +0,0 @@ -# 使用官方 Python 基础镜像 -FROM python:3.9 - -# 安装 Node.js -RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - -RUN apt-get install -y nodejs - -# 设置工作目录 -WORKDIR / - -# 将当前目录内容复制到容器的 / 目录 -COPY . / - -# 安装 Python 依赖 -RUN pip install --no-cache-dir -r requirements.txt - -# 切换到 web 目录,安装 Node.js 依赖并构建 web 应用 -WORKDIR /web -RUN npm install && npm run build - -# 切换回根目录 -WORKDIR / - -# 运行 Python 服务器 -CMD ["python", "search4all.py"] \ No newline at end of file diff --git a/README.md b/README.md index 86cc214..ed6da7a 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ You have three options for Google Search: you can use the [SearchApi Google Sear ```shell -pip3 install requirements.txt +pip3 install -r requirements.txt ``` diff --git a/requirements.txt b/requirements.txt index a23a5ca..f64975d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ requests httpx -openai +openai==1.14.1 loguru sanic -sqlitedict \ No newline at end of file +sqlitedict +python-dotenv \ No newline at end of file diff --git a/search4all.py b/search4all.py index cd1d705..b3bce4f 100644 --- a/search4all.py +++ b/search4all.py @@ -9,6 +9,8 @@ from typing import Annotated, List, Generator from openai import AsyncOpenAI from openai import AsyncOpenAI from loguru import logger +from dotenv import load_dotenv +load_dotenv() import sanic from sanic import Sanic @@ -321,8 +323,8 @@ def search_with_searchapi(query: str, subscription_key: str): def new_async_client(_app): return AsyncOpenAI( - api_key=os.environ["OPENAI_API_KEY"], - base_url=os.environ["OPENAI_BASE_URL"], + api_key=os.getenv["OPENAI_API_KEY"], + base_url=os.getenv["OPENAI_BASE_URL"], http_client=_app.ctx.http_session, ) @@ -332,44 +334,44 @@ async def server_init(_app, loop): """ Initializes global configs. """ - _app.ctx.backend = os.environ["BACKEND"].upper() + _app.ctx.backend = os.getenv["BACKEND"].upper() # if _app.ctx.backend == "LEPTON": # from leptonai import Client # _app.ctx.leptonsearch_client = Client( # "https://search-api.lepton.run/", - # token=os.environ.get("LEPTON_WORKSPACE_TOKEN"), + # token=os.getenv.get("LEPTON_WORKSPACE_TOKEN"), # stream=True, # timeout=httpx.Timeout(connect=10, read=120, write=120, pool=10), # ) if _app.ctx.backend == "BING": - _app.ctx.search_api_key = os.environ["BING_SEARCH_V7_SUBSCRIPTION_KEY"] + _app.ctx.search_api_key = os.getenv["BING_SEARCH_V7_SUBSCRIPTION_KEY"] _app.ctx.search_function = lambda query: search_with_bing( query, _app.ctx.search_api_key, ) elif _app.ctx.backend == "GOOGLE": - _app.ctx.search_api_key = os.environ["GOOGLE_SEARCH_API_KEY"] + _app.ctx.search_api_key = os.getenv["GOOGLE_SEARCH_API_KEY"] _app.ctx.search_function = lambda query: search_with_google( query, _app.ctx.search_api_key, - os.environ["GOOGLE_SEARCH_CX"], + os.getenv["GOOGLE_SEARCH_CX"], ) elif _app.ctx.backend == "SERPER": - _app.ctx.search_api_key = os.environ["SERPER_SEARCH_API_KEY"] + _app.ctx.search_api_key = os.getenv["SERPER_SEARCH_API_KEY"] _app.ctx.search_function = lambda query: search_with_serper( query, _app.ctx.search_api_key, ) elif _app.ctx.backend == "SEARCHAPI": - _app.ctx.search_api_key = os.environ["SEARCHAPI_API_KEY"] + _app.ctx.search_api_key = os.getenv["SEARCHAPI_API_KEY"] _app.ctx.search_function = lambda query: search_with_searchapi( query, _app.ctx.search_api_key, ) else: raise RuntimeError("Backend must be BING, GOOGLE, SERPER or SEARCHAPI.") - _app.ctx.model = os.environ["LLM_MODEL"] + _app.ctx.model = os.getenv["LLM_MODEL"] _app.ctx.handler_max_concurrency = 16 # An executor to carry out async tasks, such as uploading to KV. _app.ctx.executor = concurrent.futures.ThreadPoolExecutor( @@ -380,7 +382,7 @@ async def server_init(_app, loop): _app.ctx.kv = KVWrapper(os.getenv("KV_NAME") or "search.db") # whether we should generate related questions. _app.ctx.should_do_related_questions = bool( - os.environ["RELATED_QUESTIONS"] in ("1", "yes", "true") + os.getenv["RELATED_QUESTIONS"] in ("1", "yes", "true") ) # Create httpx Session _app.ctx.http_session = httpx.AsyncClient(