diff --git a/.env_azure b/.env_azure new file mode 100644 index 000000000..4ede1ac5e --- /dev/null +++ b/.env_azure @@ -0,0 +1,40 @@ +EXTERNAL_FOLDER=~/.deeppavlov/agent/ +SENTRY_DSN= +DP_AGENT_SENTRY_DSN= +DB_NAME=dream-dev +DB_HOST=mongo +DB_PORT=27017 +TFIDF_BAD_FILTER=1 +USE_TFIDF_COBOT= +USE_ASSESSMENT=1 +KEEP_ALIVE=True +KEEP_ALIVE_TIMEOUT=35 +REQUEST_TIMEOUT=2 +RESPONSE_TIMEOUT=2 +CUSTOM_DIALOG_FILE=data/test_data.json +DB_CONFIG=db_conf.json +PIPELINE_CONFIG=pipeline_conf.json +TIME_LIMIT=30 +KBQA_URL=http://kbqa:8072/model +TEXT_QA_URL=http://text-qa:8078/model +BADLIST_ANNOTATOR_URL=http://badlisted-words:8018/badlisted_words_batch +COMET_ATOMIC_SERVICE_URL=http://comet-atomic:8053/comet +COMET_CONCEPTNET_SERVICE_URL=http://comet-conceptnet:8065/comet +MASKED_LM_SERVICE_URL=http://masked-lm:8102/respond +DP_WIKIDATA_URL=http://wiki-parser:8077/model +DP_ENTITY_LINKING_URL=http://entity-linking:8075/model +KNOWLEDGE_GROUNDING_SERVICE_URL=http://knowledge-grounding:8083/respond +WIKIDATA_DIALOGUE_SERVICE_URL=http://wikidata-dial-service:8092/model +NEWS_API_ANNOTATOR_URL=http://news-api-annotator:8112/respond +WIKI_FACTS_URL=http://wiki-facts:8116/respond +FACT_RANDOM_SERVICE_URL=http://fact-random:8119/respond +INFILLING_SERVICE_URL=http://infilling:8106/respond +DIALOGPT_CONTINUE_SERVICE_URL=http://dialogpt:8125/continue +PROMPT_STORYGPT_SERVICE_URL=http://prompt-storygpt:8127/respond +STORYGPT_SERVICE_URL=http://storygpt:8126/respond +FILE_SERVER_URL=http://files:3000 +SUMMARIZATION_SERVICE_URL=http://dialog-summarizer:8059/respond_batch +# Comment following three variables if you want to use api.openai.com instead of azure +OPENAI_API_BASE=https://sentius-swe.openai.azure.com/ +OPENAI_API_TYPE=azure +OPENAI_API_VERSION=2023-05-15 diff --git a/.github/workflows/alpha.yml b/.github/workflows/alpha.yml index c806bdc7f..672cd1181 100644 --- a/.github/workflows/alpha.yml +++ b/.github/workflows/alpha.yml @@ -44,3 +44,7 @@ jobs: REGISTRY_URL: ${{ secrets.REGISTRY_URL }} PROXY_HOST: ${{ secrets.PROXY_HOST }} SENTRY_DSN: ${{ secrets.SENTRY_DSN }} + AZURE_API_KEY: ${{ secrets.AZURE_API_KEY }} + OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }} + OPENAI_API_VERSION: ${{ secrets.OPENAI_API_VERSION }} + OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }} diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 2b64f7f58..52d36624c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -47,3 +47,7 @@ jobs: REGISTRY_URL: ${{ secrets.REGISTRY_URL }} PROXY_HOST: ${{ secrets.PROXY_HOST }} SENTRY_DSN: ${{ secrets.SENTRY_DSN }} + AZURE_API_KEY: ${{ secrets.AZURE_API_KEY }} + OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }} + OPENAI_API_VERSION: ${{ secrets.OPENAI_API_VERSION }} + OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 1eff1ccf7..467ebb048 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -47,3 +47,7 @@ jobs: REGISTRY_URL: ${{ secrets.REGISTRY_URL }} PROXY_HOST: ${{ secrets.PROXY_HOST }} SENTRY_DSN: ${{ secrets.SENTRY_DSN }} + AZURE_API_KEY: ${{ secrets.AZURE_API_KEY }} + OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }} + OPENAI_API_VERSION: ${{ secrets.OPENAI_API_VERSION }} + OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d11ce94a9..544b0c2bf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v3 - name: Add .env_secret file - run: touch .env_secret + run: touch .env_secret .env_secret_azure - name: Add google api keys run: ./tests/test.sh MODE=add-google @@ -63,3 +63,7 @@ jobs: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }} GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }} + AZURE_API_KEY: ${{ secrets.AZURE_API_KEY }} + OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }} + OPENAI_API_VERSION: ${{ secrets.OPENAI_API_VERSION }} + OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }} diff --git a/.gitignore b/.gitignore index f3cf4bc53..400d6064f 100644 --- a/.gitignore +++ b/.gitignore @@ -146,3 +146,4 @@ docker-compose-one-replica.yml # personal env keys and tokens *.env_secret *.env_secret_ru +*.env_secret_azure diff --git a/assistant_dists/management_assistant/docker-compose.override.yml b/assistant_dists/management_assistant/docker-compose.override.yml index 8ab153e6d..094b23a7e 100644 --- a/assistant_dists/management_assistant/docker-compose.override.yml +++ b/assistant_dists/management_assistant/docker-compose.override.yml @@ -16,7 +16,7 @@ services: image: julienmeerschart/simple-file-upload-download-server llm-based-skill-selector: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8182 @@ -77,7 +77,7 @@ services: memory: 2G llm-based-response-selector-gpt4: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8003 @@ -141,7 +141,7 @@ services: memory: 3G openai-api-chatgpt: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8145 @@ -161,7 +161,7 @@ services: memory: 100M openai-api-gpt4: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8159 @@ -178,9 +178,9 @@ services: memory: 500M reservations: memory: 100M - + dff-general-pm-prompted-skill: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8189 @@ -200,7 +200,7 @@ services: memory: 128M dff-meeting-analysis-skill: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8186 diff --git a/assistant_dists/management_assistant_extended/docker-compose.override.yml b/assistant_dists/management_assistant_extended/docker-compose.override.yml index 23989dd21..6081fa136 100644 --- a/assistant_dists/management_assistant_extended/docker-compose.override.yml +++ b/assistant_dists/management_assistant_extended/docker-compose.override.yml @@ -39,7 +39,7 @@ services: memory: 10G openai-api-gpt4: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8159 @@ -58,7 +58,7 @@ services: memory: 100M llm-based-skill-selector: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8182 @@ -119,7 +119,7 @@ services: memory: 2G llm-based-response-selector-gpt4: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8003 @@ -183,7 +183,7 @@ services: memory: 3G openai-api-chatgpt: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8145 @@ -203,7 +203,7 @@ services: memory: 100M dff-roles-prompted-skill: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8185 @@ -223,7 +223,7 @@ services: memory: 128M dff-meeting-analysis-skill: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8186 @@ -246,7 +246,7 @@ services: memory: 500M dff-document-qa-llm-skill: - env_file: [ .env, .env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8166 diff --git a/assistant_dists/multiskill_ai_assistant/docker-compose.override.yml b/assistant_dists/multiskill_ai_assistant/docker-compose.override.yml index b13fec168..f32a6a068 100644 --- a/assistant_dists/multiskill_ai_assistant/docker-compose.override.yml +++ b/assistant_dists/multiskill_ai_assistant/docker-compose.override.yml @@ -34,7 +34,7 @@ services: memory: 2G llm-based-skill-selector: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8182 @@ -57,7 +57,7 @@ services: memory: 100M llm-based-response-selector: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8003 @@ -121,7 +121,7 @@ services: memory: 3G openai-api-chatgpt: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8145 @@ -141,7 +141,7 @@ services: memory: 100M dff-dream-persona-chatgpt-prompted-skill: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8137 @@ -161,7 +161,7 @@ services: memory: 128M dff-casual-email-prompted-skill: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8154 @@ -181,7 +181,7 @@ services: memory: 128M dff-meeting-notes-prompted-skill: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8155 @@ -201,7 +201,7 @@ services: memory: 128M dff-official-email-prompted-skill: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8156 @@ -221,7 +221,7 @@ services: memory: 128M dff-plan-for-article-prompted-skill: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8157 @@ -241,7 +241,7 @@ services: memory: 128M dff-google-api-skill: - env_file: [ .env,.env_secret ] + env_file: [ .env, .env_secret_azure ] build: args: SERVICE_PORT: 8162 diff --git a/assistant_dists/universal_prompted_assistant/docker-compose.override.yml b/assistant_dists/universal_prompted_assistant/docker-compose.override.yml index 2d0f419aa..c3b80bf28 100644 --- a/assistant_dists/universal_prompted_assistant/docker-compose.override.yml +++ b/assistant_dists/universal_prompted_assistant/docker-compose.override.yml @@ -70,7 +70,7 @@ services: memory: 3G openai-api-chatgpt: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8145 @@ -89,12 +89,12 @@ services: memory: 100M openai-api-davinci3: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8131 SERVICE_NAME: openai_api_davinci3 - PRETRAINED_MODEL_NAME_OR_PATH: text-davinci-003 + PRETRAINED_MODEL_NAME_OR_PATH: davinci-002 context: . dockerfile: ./services/openai_api_lm/Dockerfile command: flask run -h 0.0.0.0 -p 8131 @@ -108,7 +108,7 @@ services: memory: 100M openai-api-gpt4: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8159 @@ -127,7 +127,7 @@ services: memory: 100M openai-api-gpt4-32k: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8160 @@ -146,7 +146,7 @@ services: memory: 100M openai-api-chatgpt-16k: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8167 diff --git a/assistant_dists/universal_selectors_assistant/docker-compose.override.yml b/assistant_dists/universal_selectors_assistant/docker-compose.override.yml index b99ad0fc3..e7297cb4a 100644 --- a/assistant_dists/universal_selectors_assistant/docker-compose.override.yml +++ b/assistant_dists/universal_selectors_assistant/docker-compose.override.yml @@ -95,7 +95,7 @@ services: memory: 3G openai-api-chatgpt: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8145 @@ -114,12 +114,12 @@ services: memory: 100M openai-api-davinci3: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8131 SERVICE_NAME: openai_api_davinci3 - PRETRAINED_MODEL_NAME_OR_PATH: text-davinci-003 + PRETRAINED_MODEL_NAME_OR_PATH: davinci-002 context: . dockerfile: ./services/openai_api_lm/Dockerfile command: flask run -h 0.0.0.0 -p 8131 @@ -133,7 +133,7 @@ services: memory: 100M openai-api-gpt4: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8159 @@ -152,7 +152,7 @@ services: memory: 100M openai-api-gpt4-32k: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8160 @@ -171,7 +171,7 @@ services: memory: 100M openai-api-chatgpt-16k: - env_file: [ .env ] + env_file: [ .env_azure ] build: args: SERVICE_PORT: 8167 diff --git a/common/generative_configs/davinci-002.json b/common/generative_configs/davinci-002.json new file mode 100644 index 000000000..fbe2dacce --- /dev/null +++ b/common/generative_configs/davinci-002.json @@ -0,0 +1,7 @@ +{ + "max_tokens": 64, + "temperature": 0.4, + "top_p": 1.0, + "frequency_penalty": 0, + "presence_penalty": 0 +} diff --git a/services/openai_api_lm/server.py b/services/openai_api_lm/server.py index 8a3f1efe3..8633c7302 100644 --- a/services/openai_api_lm/server.py +++ b/services/openai_api_lm/server.py @@ -27,6 +27,7 @@ logging.getLogger("werkzeug").setLevel("WARNING") DEFAULT_CONFIGS = { "text-davinci-003": json.load(open("common/generative_configs/openai-text-davinci-003.json", "r")), + "davinci-002": json.load(open("common/generative_configs/davinci-002.json", "r")), "gpt-3.5-turbo": json.load(open("common/generative_configs/openai-chatgpt.json", "r")), "gpt-3.5-turbo-16k": json.load(open("common/generative_configs/openai-chatgpt.json", "r")), "gpt-4": json.load(open("common/generative_configs/openai-chatgpt.json", "r")), @@ -35,6 +36,7 @@ CHAT_COMPLETION_MODELS = ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "gpt-4-32k"] MAX_TOKENS = { "text-davinci-003": 4097, + "davinci-002": 4097, "gpt-3.5-turbo": 4096, "gpt-3.5-turbo-16k": 16384, "gpt-4": 8192, @@ -61,7 +63,12 @@ def generate_responses(context, openai_api_key, openai_org, prompt, generation_p len_context = len(ENCODER.encode(prompt)) + sum([len(ENCODER.encode(uttr)) for uttr in context]) if _max_tokens and len_context + _max_tokens >= MAX_TOKENS[PRETRAINED_MODEL_NAME_OR_PATH]: _max_tokens = None - + if openai.api_type == "azure": + if "." in PRETRAINED_MODEL_NAME_OR_PATH: + logger.info(f'Removing dots from model name "{PRETRAINED_MODEL_NAME_OR_PATH}".') + generation_params["engine"] = PRETRAINED_MODEL_NAME_OR_PATH.replace(".", "") + else: + generation_params["model"] = PRETRAINED_MODEL_NAME_OR_PATH if PRETRAINED_MODEL_NAME_OR_PATH in CHAT_COMPLETION_MODELS: logger.info("Use special chat completion endpoint") s = len(context) % 2 @@ -76,9 +83,7 @@ def generate_responses(context, openai_api_key, openai_org, prompt, generation_p for uttr_id, uttr in enumerate(context) ] logger.info(f"context inside generate_responses seen as: {messages}") - response = openai.ChatCompletion.create( - model=PRETRAINED_MODEL_NAME_OR_PATH, messages=messages, max_tokens=_max_tokens, **generation_params - ) + response = openai.ChatCompletion.create(messages=messages, max_tokens=_max_tokens, **generation_params) else: dialog_context = "" if prompt: @@ -90,18 +95,17 @@ def generate_responses(context, openai_api_key, openai_org, prompt, generation_p else: dialog_context += "\n".join(context) + f"\n{NAMING[0]}:" logger.info(f"context inside generate_responses seen as: {dialog_context}") - response = openai.Completion.create( - model=PRETRAINED_MODEL_NAME_OR_PATH, - prompt=dialog_context, - max_tokens=_max_tokens, - **generation_params, - ) + response = openai.Completion.create(prompt=dialog_context, max_tokens=_max_tokens, **generation_params) if isinstance(response, dict) and "choices" in response: - outputs = [ - resp["message"]["content"].strip() if "message" in resp else resp.get("text", "").strip() - for resp in response["choices"] - ] + try: + outputs = [ + resp["message"]["content"].strip() if "message" in resp else resp.get("text", "").strip() + for resp in response["choices"] + ] + except KeyError as e: + logger.error(f"response: {response}") + raise e elif isinstance(response, str): outputs = [response.strip()] diff --git a/tests/test.py b/tests/test.py index ac116ea40..471b3c471 100644 --- a/tests/test.py +++ b/tests/test.py @@ -55,7 +55,7 @@ def check_universal_assistant(lm_services): BOT_URL, json={ "user_id": f"test-user-{random.randint(100, 1000)}", - "payload": "What are the most interesting games for a 5-year old girl?", + "payload": "What song should I sing to my baby?", "api_keys": {"openai_api_key": OPENAI_API_KEY}, "skills": [ { @@ -151,7 +151,8 @@ def check_universal_selectors_assistant(lm_services): print("Success!") else: raise ValueError( - f"\nERROR: `Universal Selectors Assistant` returned `{result['text']}` with lm service {lm_service}\n" + f"\nERROR: `Universal Selectors Assistant` returned `{result['response']}` " + f"with lm service {lm_service}\n" ) sleep(5) @@ -202,7 +203,7 @@ def main(): elif assistant == "universal_prompted_assistant": check_universal_assistant(["ChatGPT", "GPT-3.5"]) # TODO: add "GPT-4 32k", "GPT-4" and "ChatGPT 16k" elif assistant == "universal_selectors_assistant": - check_universal_selectors_assistant(["ChatGPT", "GPT-3.5"]) # TODO: add "GPT-4 32k", "GPT-4" and "ChatGPT 16k" + check_universal_selectors_assistant(["ChatGPT"]) # TODO: add "GPT-4 32k", "GPT-4" and "ChatGPT 16k" if __name__ == "__main__": diff --git a/utils/deploy/deploy.py b/utils/deploy/deploy.py index 83cbe7c37..6e5bfe3e6 100644 --- a/utils/deploy/deploy.py +++ b/utils/deploy/deploy.py @@ -24,6 +24,10 @@ class Settings(BaseSettings): google_cse_id: str google_api_key: str sentry_dsn: str + openai_api_base: str + openai_api_type: str + openai_api_version: str + azure_api_key: str settings = Settings() @@ -170,6 +174,12 @@ def main(): dotenv.set_key(DREAM_ROOT_PATH / ".env", "GOOGLE_CSE_ID", settings.google_cse_id) dotenv.set_key(DREAM_ROOT_PATH / ".env", "GOOGLE_API_KEY", settings.google_api_key) dotenv.set_key(DREAM_ROOT_PATH / ".env", "SENTRY_DSN", settings.sentry_dsn) + + dotenv.set_key(DREAM_ROOT_PATH / ".env_secret_azure", "OPENAI_API_BASE", settings.openai_api_base) + dotenv.set_key(DREAM_ROOT_PATH / ".env_secret_azure", "OPENAI_API_TYPE", settings.openai_api_type) + dotenv.set_key(DREAM_ROOT_PATH / ".env_secret_azure", "OPENAI_API_VERSION", settings.openai_api_version) + dotenv.set_key(DREAM_ROOT_PATH / ".env_secret_azure", "OPENAI_API_KEY", settings.azure_api_key) + deploy(universal_bot) dotenv.set_key(DREAM_ROOT_PATH / ".env", "OPENAI_API_KEY", settings.openai_api_key) deploy(multi_bot)