From f1078d035e05ef4d9cb82c82889b469e1cdc10cc Mon Sep 17 00:00:00 2001 From: "lianchen.zhang" Date: Wed, 8 Nov 2023 19:46:16 +0800 Subject: [PATCH] =?UTF-8?q?#1.browse=5Fwebsite=E9=85=8D=E7=BD=AE=E4=BB=A3?= =?UTF-8?q?=E7=90=86=20#2.=E4=B8=AD=E6=96=87=E6=8F=90=E7=A4=BA=E8=AF=8D?= =?UTF-8?q?=EF=BC=88=E5=AF=B9=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C=E6=9C=89?= =?UTF-8?q?=E4=BA=9B=E5=BD=B1=E5=93=8D=E3=80=81token=E6=B6=88=E8=80=97?= =?UTF-8?q?=E5=A4=A7=EF=BC=8C=E6=9A=82=E6=97=B6=E6=B3=A8=E6=8E=89=EF=BC=89?= =?UTF-8?q?=20#3.=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=BE=85?= =?UTF-8?q?=E9=80=82=E9=85=8D=E6=96=87=E6=9C=AC=E8=BD=AC=E5=90=91=E9=87=8F?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 16 +++++----- autogpt/app/setup.py | 12 ++++++++ autogpt/commands/web_search.py | 13 +++------ autogpt/commands/web_selenium.py | 5 ++-- autogpt/core/configuration/__init__.py | 10 +++++++ autogpt/llm/providers/openai.py | 4 +-- autogpt/memory/vector/utils.py | 2 ++ autogpt/prompts/prompt.py | 1 + autogpt/qunar/api_resources/__init__.py | 0 .../abstract/EngineApiResource.py} | 9 ++---- .../qunar/api_resources/abstract/__init__.py | 0 .../qunar/api_resources/q_chat_completion.py | 29 +++++++++++++++++++ autogpt/qunar/qunar_requestor.py | 3 -- docker-compose.yml | 2 +- 14 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 autogpt/qunar/api_resources/__init__.py rename autogpt/qunar/{qunar_gpt.py => api_resources/abstract/EngineApiResource.py} (95%) create mode 100644 autogpt/qunar/api_resources/abstract/__init__.py create mode 100644 autogpt/qunar/api_resources/q_chat_completion.py diff --git a/.env b/.env index eea98794f8e..b4866630610 100644 --- a/.env +++ b/.env @@ -5,7 +5,7 @@ ################################################################################ ## OPENAI_API_KEY - OpenAI API Key (Example: my-openai-api-key) -OPENAI_API_KEY=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 +OPENAI_API_KEY=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 ## EXECUTE_LOCAL_COMMANDS - Allow local command execution (Default: False) # EXECUTE_LOCAL_COMMANDS=False @@ -215,12 +215,12 @@ GOOGLE_CUSTOM_SEARCH_ENGINE_ID=62da02a91306a4c88 ################################################################################ ### QUUNAR config ################################################################################ -QUNAR_API_BASE = http://llm.api.corp.qunar.com/algo/llm/api +QUNAR_API_BASE=http://llm.api.corp.qunar.com/algo/llm/api -QUNAR_GPT_MODEL = gpt-4 -QUNAR_KEY = qunar_hackathon_41 -QUNAR_PWD = 1MVb45 -QUNAR_USER = lianchen.zhang +QUNAR_GPT_MODEL=gpt-35-turbo +QUNAR_KEY=qunar-flightCheckin-prod +QUNAR_PWD=04b446c5-10e3-4664-8a12-b2e7729b9293 +QUNAR_USER=lianchen.zhang -QUNAR_PROXY_HOST = proxy.corp.qunar.com -QUNAR_PROXY_PORT = 10080 \ No newline at end of file +QUNAR_PROXY_HOST=proxy.corp.qunar.com +QUNAR_PROXY_PORT=10080 \ No newline at end of file diff --git a/autogpt/app/setup.py b/autogpt/app/setup.py index f2879af6b36..b025d15c2a5 100644 --- a/autogpt/app/setup.py +++ b/autogpt/app/setup.py @@ -233,6 +233,18 @@ def generate_aiconfig_automatic(user_prompt: str, config: Config) -> AIConfig: .group(1) .strip() ) + + # 中文格式解析 + # ai_name = re.search(r"名称(?:\s*):(?:\s*)(.*)", output, re.IGNORECASE).group(1) + # ai_role = ( + # re.search( + # r"描述(?:\s*):(?:\s*)(.*?)(?:(?:\n)|目标)", + # output, + # re.IGNORECASE | re.DOTALL, + # ) + # .group(1) + # .strip() + # ) ai_goals = re.findall(r"(?<=\n)-\s*(.*)", output) api_budget = 0.0 # TODO: parse api budget using a regular expression diff --git a/autogpt/commands/web_search.py b/autogpt/commands/web_search.py index 30aabb5d583..e54ab4e5e33 100644 --- a/autogpt/commands/web_search.py +++ b/autogpt/commands/web_search.py @@ -7,6 +7,8 @@ import httplib2 +from autogpt.core.configuration import * + COMMAND_CATEGORY = "web_search" COMMAND_CATEGORY_TITLE = "Web Search" @@ -21,14 +23,7 @@ DUCKDUCKGO_MAX_ATTEMPTS = 3 -QUNAR_PROXY_HOST = os.environ.get("QUNAR_PROXY_HOST") -QUNAR_PROXY_PORT = os.environ.get("QUNAR_PROXY_PORT") -QUNAR_PROXY_USR = os.environ.get("QUNAR_PROXY_USR") -QUNAR_PROXY_PWD = os.environ.get("QUNAR_PROXY_PWD") -proxies = { - 'http': f'http://{QUNAR_PROXY_HOST}:{QUNAR_PROXY_PORT}', - 'https': f'http://{QUNAR_PROXY_HOST}:{QUNAR_PROXY_PORT}' -} + @command( @@ -201,7 +196,7 @@ def proxy_info_from_url(method="http", noproxy=None): pi = httplib2.ProxyInfo( proxy_type=proxy_type, proxy_host=QUNAR_PROXY_HOST, - proxy_port=QUNAR_PROXY_PORT or dict(https=443, http=80)[method], + proxy_port=int(QUNAR_PROXY_PORT) or dict(https=443, http=80)[method], proxy_user=QUNAR_PROXY_USR or None, proxy_pass=QUNAR_PROXY_PWD or None, proxy_headers=None, diff --git a/autogpt/commands/web_selenium.py b/autogpt/commands/web_selenium.py index 92aa5bece0e..825d29207e8 100644 --- a/autogpt/commands/web_selenium.py +++ b/autogpt/commands/web_selenium.py @@ -40,7 +40,7 @@ from autogpt.memory.vector import MemoryItem, get_memory from autogpt.processing.html import extract_hyperlinks, format_hyperlinks from autogpt.url_utils.validators import validate_url - +from autogpt.core.configuration import * FILE_DIR = Path(__file__).parent.parent TOKENS_TO_TRIGGER_SUMMARY = 50 LINKS_TO_RETURN = 20 @@ -115,7 +115,8 @@ def scrape_text_with_selenium(url: str, agent: Agent) -> tuple[WebDriver, str]: options.add_argument( "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.49 Safari/537.36" ) - + # options.add_argument(f'--proxy-server={QUNAR_PROXY_HOST}:{QUNAR_PROXY_PORT}') + options.add_argument(f'--proxy-server={proxies["http"]}') if agent.config.selenium_web_browser == "firefox": if agent.config.selenium_headless: options.headless = True diff --git a/autogpt/core/configuration/__init__.py b/autogpt/core/configuration/__init__.py index b6fee749393..7dc3c5d65c7 100644 --- a/autogpt/core/configuration/__init__.py +++ b/autogpt/core/configuration/__init__.py @@ -1,7 +1,17 @@ """The configuration encapsulates settings for all Agent subsystems.""" +import os + from autogpt.core.configuration.schema import ( Configurable, SystemConfiguration, SystemSettings, UserConfigurable, ) +QUNAR_PROXY_HOST = os.environ.get("QUNAR_PROXY_HOST") +QUNAR_PROXY_PORT = os.environ.get("QUNAR_PROXY_PORT") +QUNAR_PROXY_USR = os.environ.get("QUNAR_PROXY_USR") +QUNAR_PROXY_PWD = os.environ.get("QUNAR_PROXY_PWD") +proxies = { + 'http': f'http://{QUNAR_PROXY_HOST}:{QUNAR_PROXY_PORT}', + 'https': f'http://{QUNAR_PROXY_HOST}:{QUNAR_PROXY_PORT}' +} \ No newline at end of file diff --git a/autogpt/llm/providers/openai.py b/autogpt/llm/providers/openai.py index 65149088ca8..b7084f49675 100644 --- a/autogpt/llm/providers/openai.py +++ b/autogpt/llm/providers/openai.py @@ -21,7 +21,7 @@ ) from autogpt.logs import logger from autogpt.models.command_registry import CommandRegistry -from autogpt.qunar.qunar_gpt import QunarGPT +from autogpt.qunar.api_resources.q_chat_completion import QChatCompletion MAX_TOKENS = 40960 @@ -242,7 +242,7 @@ def create_chat_completion( # **kwargs, # ) - completion: OpenAIObject = QunarGPT.create( + completion: OpenAIObject = QChatCompletion.create( messages=messages, **kwargs, ) diff --git a/autogpt/memory/vector/utils.py b/autogpt/memory/vector/utils.py index bee5c27bc70..5c86c639a61 100644 --- a/autogpt/memory/vector/utils.py +++ b/autogpt/memory/vector/utils.py @@ -34,6 +34,7 @@ def get_embedding( Returns: List[float]: The embedding. """ + return [] multiple = isinstance(input, list) and all(not isinstance(i, int) for i in input) if isinstance(input, str): @@ -58,6 +59,7 @@ def get_embedding( + (f" via Azure deployment '{kwargs['engine']}'" if config.use_azure else "") ) +# embeddings = iopenai.create_embedding( input, **kwargs, diff --git a/autogpt/prompts/prompt.py b/autogpt/prompts/prompt.py index 627b6c50f18..6b5494a948c 100644 --- a/autogpt/prompts/prompt.py +++ b/autogpt/prompts/prompt.py @@ -3,6 +3,7 @@ from autogpt.prompts.generator import PromptGenerator DEFAULT_TRIGGERING_PROMPT = ( + # "Your answers must be in Chinese. " "Determine exactly one command to use based on the given goals " "and the progress you have made so far, " "and respond using the JSON schema specified previously:" diff --git a/autogpt/qunar/api_resources/__init__.py b/autogpt/qunar/api_resources/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/autogpt/qunar/qunar_gpt.py b/autogpt/qunar/api_resources/abstract/EngineApiResource.py similarity index 95% rename from autogpt/qunar/qunar_gpt.py rename to autogpt/qunar/api_resources/abstract/EngineApiResource.py index 39a87bb9784..0bf3306db4c 100644 --- a/autogpt/qunar/qunar_gpt.py +++ b/autogpt/qunar/api_resources/abstract/EngineApiResource.py @@ -1,17 +1,12 @@ -import os - -import openai -from openai import api_requestor, error, util -from openai.api_resources.abstract.api_resource import APIResource +from openai import error, util from openai.openai_response import OpenAIResponse -from openai.util import ApiType from autogpt.qunar.qunar_requestor import QunarRequestor MAX_TIMEOUT = 20 -class QunarGPT: +class EngineAPIResource: plain_old_data = False @classmethod diff --git a/autogpt/qunar/api_resources/abstract/__init__.py b/autogpt/qunar/api_resources/abstract/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/autogpt/qunar/api_resources/q_chat_completion.py b/autogpt/qunar/api_resources/q_chat_completion.py new file mode 100644 index 00000000000..1696ee52137 --- /dev/null +++ b/autogpt/qunar/api_resources/q_chat_completion.py @@ -0,0 +1,29 @@ +import time + +from openai import util +from openai.error import TryAgain + +from autogpt.qunar.api_resources.abstract.EngineApiResource import EngineAPIResource + + +class QChatCompletion(EngineAPIResource): + + @classmethod + def create(cls, *args, **kwargs): + """ + Creates a new chat completion for the provided messages and parameters. + + See https://platform.openai.com/docs/api-reference/chat-completions/create + for a list of valid parameters. + """ + start = time.time() + timeout = kwargs.pop("timeout", None) + + while True: + try: + return super().create(*args, **kwargs) + except TryAgain as e: + if timeout is not None and time.time() > start + timeout: + raise + + util.log_info("Waiting for model to warm up", error=e) diff --git a/autogpt/qunar/qunar_requestor.py b/autogpt/qunar/qunar_requestor.py index 74d3a105a04..4cfeef619bd 100644 --- a/autogpt/qunar/qunar_requestor.py +++ b/autogpt/qunar/qunar_requestor.py @@ -5,15 +5,12 @@ import warnings from json import JSONDecodeError from typing import ( - AsyncGenerator, - AsyncIterator, Callable, Dict, Iterator, Optional, Tuple, Union, - overload, ) import openai diff --git a/docker-compose.yml b/docker-compose.yml index 945f969b747..eca75eb7b3f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ version: "3.9" services: - auto-gpt: + qunar-auto-gpt: build: ./ env_file: - .env