Skip to content

Commit

Permalink
Feat:
Browse files Browse the repository at this point in the history
- Add pytest for load_config_from_json for yaml format
- Add docs for website
- Add pyyaml to setup.py
  • Loading branch information
Mai0313 committed May 6, 2024
1 parent 1a42c46 commit b97b99d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Disallowing 2.6.0 can be removed when this is fixed https://github.com/pydantic/pydantic/issues/8705
"pydantic>=1.10,<3,!=2.6.0", # could be both V1 and V2
"docker",
"pyyaml",
]

jupyter_executor = [
Expand Down
34 changes: 27 additions & 7 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from unittest.mock import patch

import pytest
import yaml
from conftest import MOCK_OPEN_AI_API_KEY

import autogen # noqa: E402
Expand Down Expand Up @@ -65,30 +66,49 @@
]
"""

YAML_SAMPLE = """
- model: gpt-3.5-turbo
api_type: openai
- model: gpt-4
api_type: openai
- model: gpt-35-turbo-v0301
tags:
- gpt-3.5-turbo
- gpt35_turbo
api_key: "111113fc7e8a46419bfac511bb301111"
base_url: "https://1111.openai.azure.com"
api_type: azure
api_version: "2024-02-15-preview"
- model: gpt
api_key: not-needed
base_url: "http://localhost:1234/v1"
"""


@pytest.fixture
def mock_os_environ():
with mock.patch.dict(os.environ, ENV_VARS):
yield


def test_config_list_from_json():
@pytest.mark.parametrize("config_example", [JSON_SAMPLE, YAML_SAMPLE], ids=["from_json", "from_yaml"])
def test_config_list_from_json(config_example):
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as tmp_file:
json_data = json.loads(JSON_SAMPLE)
tmp_file.write(JSON_SAMPLE)
config_data = yaml.safe_load(config_example)
tmp_file.write(config_example)
tmp_file.flush()
config_list = autogen.config_list_from_json(tmp_file.name)

assert len(config_list) == len(json_data)
assert len(config_list) == len(config_data)
i = 0
for config in config_list:
assert isinstance(config, dict)
for key in config:
assert key in json_data[i]
assert config[key] == json_data[i][key]
assert key in config_data[i]
assert config[key] == config_data[i][key]
i += 1

os.environ["config_list_test"] = JSON_SAMPLE
os.environ["config_list_test"] = config_example
config_list_2 = autogen.config_list_from_json("config_list_test")
assert config_list == config_list_2

Expand Down
60 changes: 59 additions & 1 deletion website/docs/topics/llm_configuration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,57 @@
"</Tabs>\n",
"````\n",
"\n",
"There is another way to specify the `config_list` using a YAML file. This is useful when you have multiple models and want to keep the configuration separate from the code. The YAML file should be formatted as follows:\n",
"\n",
"````{=mdx}\n",
"import Tabs from '@theme/Tabs';\n",
"import TabItem from '@theme/TabItem';\n",
"\n",
"<Tabs>\n",
" <TabItem value=\"openai\" label=\"OpenAI\" default>\n",
" - `model` (str, required): The identifier of the model to be used, such as 'gpt-4', 'gpt-3.5-turbo'.\n",
" - `api_key` (str, optional): The API key required for authenticating requests to the model's API endpoint.\n",
" - `base_url` (str, optional): The base URL of the API endpoint. This is the root address where API calls are directed.\n",
" - `tags` (List[str], optional): Tags which can be used for filtering.\n",
"\n",
" Example:\n",
" ```yaml\n",
" - model: gpt-4\n",
" api_key: <your OpenAI API key here>\n",
" ```\n",
" </TabItem>\n",
" <TabItem value=\"azureopenai\" label=\"Azure OpenAI\">\n",
" - `model` (str, required): The deployment to be used. The model corresponds to the deployment name on Azure OpenAI.\n",
" - `api_key` (str, optional): The API key required for authenticating requests to the model's API endpoint.\n",
" - `api_type`: `azure`\n",
" - `base_url` (str, optional): The base URL of the API endpoint. This is the root address where API calls are directed.\n",
" - `api_version` (str, optional): The version of the Azure API you wish to use.\n",
" - `tags` (List[str], optional): Tags which can be used for filtering.\n",
"\n",
" Example:\n",
" ```yaml\n",
" - model: my-gpt-4-deployment\n",
" api_type: azure\n",
" api_key: <your Azure OpenAI API key here>\n",
" base_url: https://ENDPOINT.openai.azure.com/\n",
" api_version: 2024-02-15-preview\n",
" ```\n",
" </TabItem>\n",
" <TabItem value=\"other\" label=\"Other OpenAI compatible\">\n",
" - `model` (str, required): The identifier of the model to be used, such as 'llama-7B'.\n",
" - `api_key` (str, optional): The API key required for authenticating requests to the model's API endpoint.\n",
" - `base_url` (str, optional): The base URL of the API endpoint. This is the root address where API calls are directed.\n",
" - `tags` (List[str], optional): Tags which can be used for filtering.\n",
"\n",
" Example:\n",
" ```yaml\n",
" - model: llama-7B\n",
" base_url: http://localhost:1234\n",
" ```\n",
" </TabItem>\n",
"</Tabs>\n",
"````\n",
"\n",
"---\n",
"\n",
"````{=mdx}\n",
Expand All @@ -126,9 +177,16 @@
"\n",
"### `OAI_CONFIG_LIST` pattern\n",
"\n",
"A common, useful pattern used is to define this `config_list` is via JSON (specified as a file or an environment variable set to a JSON-formatted string) and then use the [`config_list_from_json`](/docs/reference/oai/openai_utils#config_list_from_json) helper function to load it:"
"A common, useful pattern used is to define this `config_list` is via JSON/YAML (specified as a file or an environment variable set to a JSON-formatted string) and then use the [`config_list_from_json`](/docs/reference/oai/openai_utils#config_list_from_json) helper function to load it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit b97b99d

Please sign in to comment.