diff --git a/.env.example b/.env.example index 9a80a884..15976a3f 100644 --- a/.env.example +++ b/.env.example @@ -1,15 +1,30 @@ +""" +This file is a template for the .env file. + +Please copy this file to .env and fill in the values. + +For more information about configuration options, please refer to the documentation + +""" + # Global configs: +USE_AZURE=False +USE_AZURE_TOKEN_PROVIDER=False MAX_RETRY=10 RETRY_WAIT_SECONDS=20 -# api key -OPENAI_API_KEY=your_api_key - -# embedding model configs: -EMBEDDING_MODEL=text-embedding-3-small - -# chat model configs: -CHAT_MODEL=your_model_version +# LLM API Setting: +OPENAI_API_KEY= +CHAT_MODEL=gpt-4-turbo CHAT_MAX_TOKENS=3000 CHAT_TEMPERATURE=0.7 -CHAT_STREAM=True +# CHAT_AZURE_API_BASE= +# CHAT_AZURE_API_VERSION= + +EMBEDDING_MODEL=text-embedding-3-small +# EMBEDDING_AZURE_API_BASE= +# EMBEDDING_AZURE_API_VERSION= + +# Cache Setting (Optional): + +# Senario Configs: \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index a8abb5a6..31b03cdb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -11,7 +11,7 @@ Welcome to RDAgent's documentation! :caption: Doctree: demo_and_introduction - installation + installation_and_configuration scenarios_and_quickstart project_framework_introduction development diff --git a/docs/installation.rst b/docs/installation.rst deleted file mode 100644 index 25725ad3..00000000 --- a/docs/installation.rst +++ /dev/null @@ -1,72 +0,0 @@ -========================= -Installation -========================= - -Installation -============ - -For different scenarios -- for purely users: -- for dev users: link to development - -Configuration -============= - -To run the application, please create a `.env` file in the root directory of the project and add environment variables according to your requirements. - -The standard configuration options for the user using the OpenAI API are provided in the `.env.example` file. - -Here are some other configuration options that you can use: - -OpenAI API ------------- - -You can use different OpenAI API keys for embedding model and chat model. - - .. code-block:: Properties - - EMBEDDING_OPENAI_API_KEY= - EMBEDDING_MODEL=text-embedding-3-small - - CHAT_OPENAI_API_KEY= - CHAT_MODEL=gpt-4-turbo - -Azure OpenAI ------------- - -The following environment variables are standard configuration options for the user using the OpenAI API. - - .. code-block:: Properties - - USE_AZURE=True - - OPENAI_API_KEY= - - EMBEDDING_MODEL=text-embedding-3-small - EMBEDDING_AZURE_API_BASE= # The base URL for the Azure OpenAI API. - EMBEDDING_AZURE_API_VERSION = # The version of the Azure OpenAI API. - - CHAT_MODEL=gpt-4-turbo - CHAT_AZURE_API_VERSION = # The version of the Azure OpenAI API. - -Use Azure Token Provider ------------------------- - -If you are using the Azure token provider, you need to set the `USE_AZURE_TOKEN_PROVIDER` environment variable to `True`. then -use the environment variables provided in the `Azure Configuration section `_. - -☁️ Azure Configuration -- Install Azure CLI: - - ```sh - curl -L https://aka.ms/InstallAzureCli | bash - ``` - -- Log in to Azure: - - ```sh - az login --use-device-code - ``` - -- `exit` and re-login to your environment (this step may not be necessary). - diff --git a/docs/installation_and_configuration.rst b/docs/installation_and_configuration.rst new file mode 100644 index 00000000..1f4c776c --- /dev/null +++ b/docs/installation_and_configuration.rst @@ -0,0 +1,142 @@ +============================== +Installation and Configuration +============================== + +Installation +============ + +For different scenarios +- for purely users: +- for dev users: `See development `_ + +Configuration +============= + +To run the application, please create a `.env` file in the root directory of the project and add environment variables according to your requirements. + +The standard configuration options for the user using the OpenAI API are provided in the `.env.example` file. + +Here are some other configuration options that you can use: + +OpenAI API +------------ + +Here is a standard configuration for the user using the OpenAI API. + + .. code-block:: Properties + + OPENAI_API_KEY= + EMBEDDING_MODEL=text-embedding-3-small + CHAT_MODEL=gpt-4-turbo + +Azure OpenAI +------------ + +The following environment variables are standard configuration options for the user using the OpenAI API. + + .. code-block:: Properties + + USE_AZURE=True + + OPENAI_API_KEY= + + EMBEDDING_MODEL=text-embedding-3-small + EMBEDDING_AZURE_API_BASE= # The base URL for the Azure OpenAI API. + EMBEDDING_AZURE_API_VERSION = # The version of the Azure OpenAI API. + + CHAT_MODEL=gpt-4-turbo + CHAT_AZURE_API_VERSION = # The version of the Azure OpenAI API. + +Use Azure Token Provider +------------------------ + +If you are using the Azure token provider, you need to set the `USE_AZURE_TOKEN_PROVIDER` environment variable to `True`. then +use the environment variables provided in the `Azure Configuration section `_. + + +☁️ Azure Configuration +- Install Azure CLI: + + ```sh + curl -L https://aka.ms/InstallAzureCli | bash + ``` + +- Log in to Azure: + + ```sh + az login --use-device-code + ``` + +- `exit` and re-login to your environment (this step may not be necessary). + + +Configuration List +------------------ + +.. TODO: use `autodoc-pydantic` . + +- OpenAI API Setting + ++-----------------------------+--------------------------------------------------+-------------------------+ +| Configuration Option | Meaning | Default Value | ++=============================+==================================================+=========================+ +| OPENAI_API_KEY | API key for both chat and embedding models | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| EMBEDDING_OPENAI_API_KEY | Use a different API key for embedding model | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| CHAT_OPENAI_API_KEY | Set to use a different API key for chat model | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| EMBEDDING_MODEL | Name of the embedding model | text-embedding-3-small | ++-----------------------------+--------------------------------------------------+-------------------------+ +| CHAT_MODEL | Name of the chat model | gpt-4-turbo | ++-----------------------------+--------------------------------------------------+-------------------------+ +| EMBEDDING_AZURE_API_BASE | Base URL for the Azure OpenAI API | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| EMBEDDING_AZURE_API_VERSION | Version of the Azure OpenAI API | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| CHAT_AZURE_API_BASE | Base URL for the Azure OpenAI API | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| CHAT_AZURE_API_VERSION | Version of the Azure OpenAI API | None | ++-----------------------------+--------------------------------------------------+-------------------------+ +| USE_AZURE | True if you are using Azure OpenAI | False | ++-----------------------------+--------------------------------------------------+-------------------------+ +| USE_AZURE_TOKEN_PROVIDER | True if you are using a Azure Token Provider | False | ++-----------------------------+--------------------------------------------------+-------------------------+ + +- Globol Setting + ++-----------------------------+--------------------------------------------------+-------------------------+ +| Configuration Option | Meaning | Default Value | ++=============================+==================================================+=========================+ +| max_retry | Maximum number of times to retry | 10 | ++-----------------------------+--------------------------------------------------+-------------------------+ +| retry_wait_seconds | Number of seconds to wait before retrying | 1 | ++-----------------------------+--------------------------------------------------+-------------------------+ ++ log_trace_path | Path to log trace file | None | ++-----------------------------+--------------------------------------------------+-------------------------+ ++ log_llm_chat_content | Flag to indicate if chat content is logged | True | ++-----------------------------+--------------------------------------------------+-------------------------+ + + +- Cache Setting + +.. TODO: update Meaning for caches + ++------------------------------+--------------------------------------------------+-------------------------+ +| Configuration Option | Meaning | Default Value | ++==============================+==================================================+=========================+ +| dump_chat_cache | Flag to indicate if chat cache is dumped | False | ++------------------------------+--------------------------------------------------+-------------------------+ +| dump_embedding_cache | Flag to indicate if embedding cache is dumped | False | ++------------------------------+--------------------------------------------------+-------------------------+ +| use_chat_cache | Flag to indicate if chat cache is used | False | ++------------------------------+--------------------------------------------------+-------------------------+ +| use_embedding_cache | Flag to indicate if embedding cache is used | False | ++------------------------------+--------------------------------------------------+-------------------------+ +| prompt_cache_path | Path to prompt cache | ./prompt_cache.db | ++------------------------------+--------------------------------------------------+-------------------------+ +| session_cache_folder_location| Path to session cache | ./session_cache_folder | ++------------------------------+--------------------------------------------------+-------------------------+ +| max_past_message_include | Maximum number of past messages to include | 10 | ++------------------------------+--------------------------------------------------+-------------------------+ + diff --git a/rdagent/core/conf.py b/rdagent/core/conf.py index 975cad47..2e5e8613 100644 --- a/rdagent/core/conf.py +++ b/rdagent/core/conf.py @@ -20,7 +20,7 @@ class RDAgentSettings(BaseSettings): log_trace_path: str | None = None log_llm_chat_content: bool = True - use_azure: bool = True + use_azure: bool = False use_azure_token_provider: bool = False managed_identity_client_id: str | None = None max_retry: int = 10 @@ -48,7 +48,7 @@ class RDAgentSettings(BaseSettings): chat_token_limit: int = ( 100000 # 100000 is the maximum limit of gpt4, which might increase in the future version of gpt ) - default_system_prompt: str = "You are an AI assistant who helps to answer user's questions about finance." + default_system_prompt: str = "You are an AI assistant who helps to answer user's questions." # Embedding configs embedding_openai_api_key: str = "" diff --git a/rdagent/oai/llm_utils.py b/rdagent/oai/llm_utils.py index fdd30a19..c10c32d3 100644 --- a/rdagent/oai/llm_utils.py +++ b/rdagent/oai/llm_utils.py @@ -298,14 +298,21 @@ class APIBackend: self.use_azure_token_provider = self.cfg.use_azure_token_provider self.managed_identity_client_id = self.cfg.managed_identity_client_id - if self.cfg.openai_api_key: - self.chat_api_key = self.cfg.openai_api_key - self.embedding_api_key = self.cfg.openai_api_key - else: - self.chat_api_key = self.cfg.chat_openai_api_key if chat_api_key is None else chat_api_key - self.embedding_api_key = ( - self.cfg.embedding_openai_api_key if embedding_api_key is None else embedding_api_key - ) + # Priority: chat_api_key/embedding_api_key > openai_api_key > os.environ.get("OPENAI_API_KEY") + # TODO: Simplify the key design. Consider Pandatic's field alias & priority. + self.chat_api_key = ( + chat_api_key + or self.cfg.chat_openai_api_key + or self.cfg.openai_api_key + or os.environ.get("OPENAI_API_KEY") + ) + self.embedding_api_key = ( + embedding_api_key + or self.cfg.embedding_openai_api_key + or self.cfg.openai_api_key + or os.environ.get("OPENAI_API_KEY") + ) + self.chat_model = self.cfg.chat_model if chat_model is None else chat_model self.encoder = tiktoken.encoding_for_model(self.chat_model) self.chat_api_base = self.cfg.chat_azure_api_base if chat_api_base is None else chat_api_base