From 9e0abc60be01f3228755343d2e9b8e974168b766 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Tue, 26 Sep 2023 11:45:09 +0800 Subject: [PATCH] (feat)(llm) expose config for Open API base url (#112) --- chat/core/src/main/python/run_config.py | 1 + chat/core/src/main/python/util/llm_instance.py | 9 ++++++--- chat/core/src/main/python/util/stringutils.py | 10 ++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 chat/core/src/main/python/util/stringutils.py diff --git a/chat/core/src/main/python/run_config.py b/chat/core/src/main/python/run_config.py index 0b909b77d..f49a0dfeb 100644 --- a/chat/core/src/main/python/run_config.py +++ b/chat/core/src/main/python/run_config.py @@ -8,6 +8,7 @@ LLMPARSER_PORT = 9092 MODEL_NAME = "gpt-3.5-turbo-16k" OPENAI_API_KEY = "YOUR_API_KEY" +OPENAI_API_BASE = "" TEMPERATURE = 0.0 diff --git a/chat/core/src/main/python/util/llm_instance.py b/chat/core/src/main/python/util/llm_instance.py index 15a277408..07cf8586f 100644 --- a/chat/core/src/main/python/util/llm_instance.py +++ b/chat/core/src/main/python/util/llm_instance.py @@ -1,9 +1,12 @@ # -*- coding:utf-8 -*- from langchain.llms import OpenAI -from run_config import MODEL_NAME, OPENAI_API_KEY, TEMPERATURE - +from run_config import * +from util.stringutils import * llm = OpenAI( - openai_api_key=OPENAI_API_KEY, model_name=MODEL_NAME, temperature=TEMPERATURE + model_name=MODEL_NAME, + openai_api_key=OPENAI_API_KEY, + openai_api_base=default_if_blank(OPENAI_API_BASE), + temperature=TEMPERATURE, ) diff --git a/chat/core/src/main/python/util/stringutils.py b/chat/core/src/main/python/util/stringutils.py new file mode 100644 index 000000000..5b02b792d --- /dev/null +++ b/chat/core/src/main/python/util/stringutils.py @@ -0,0 +1,10 @@ +def is_blank(s: str) -> bool: + return not (s and s.strip()) + + +def is_not_blank(s: str) -> bool: + return not is_blank(s) + + +def default_if_blank(s: str, default: str = None) -> str: + return s if is_not_blank(s) else default