Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update config_list_from_json function to support YAML parsing #2560

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
21 changes: 21 additions & 0 deletions OAI_CONFIG_LIST_yaml_sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Please modify the content, remove these four lines of comment and rename this file to OAI_CONFIG_LIST to run the sample code.
# If using pyautogen v0.1.x with Azure OpenAI, please replace "base_url" with "api_base" (line 13 and line 19 below). Use "pip list" to check version of pyautogen installed.

# NOTE: This configuration lists GPT-4 as the default model, as this represents our current recommendation, and is known to work well with AutoGen. If you use a model other than GPT-4, you may need to revise various system prompts (especially if using weaker models like GPT-3.5-turbo). Moreover, if you use models other than those hosted by OpenAI or Azure, you may incur additional risks related to alignment and safety. Proceed with caution if updating this default.
- model: gpt-4
api_key: <your OpenAI API key here>
tags:
- gpt-4
- tool

- model: <your Azure OpenAI deployment name>
api_key: <your Azure OpenAI API key here>
base_url: <your Azure OpenAI API base here>
api_type: azure
api_version: 2024-02-15-preview

- model: <your Azure OpenAI deployment name>
api_key: <your Azure OpenAI API key here>
base_url: <your Azure OpenAI API base here>
api_type: azure
api_version: 2024-02-15-preview
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ The easiest way to start playing is

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/microsoft/autogen?quickstart=1)

2. Copy OAI_CONFIG_LIST_sample to ./notebook folder, name to OAI_CONFIG_LIST, and set the correct configuration.
2. Copy OAI_CONFIG_LIST_sample or OAI_CONFIG_LIST_yaml_sample to ./notebook folder, name to OAI_CONFIG_LIST, and set the correct configuration.
3. Start playing with the notebooks!

*NOTE*: OAI_CONFIG_LIST_sample lists GPT-4 as the default model, as this represents our current recommendation, and is known to work well with AutoGen. If you use a model other than GPT-4, you may need to revise various system prompts (especially if using weaker models like GPT-3.5-turbo). Moreover, if you use models other than those hosted by OpenAI or Azure, you may incur additional risks related to alignment and safety. Proceed with caution if updating this default.
*NOTE*: OAI_CONFIG_LIST_sample/OAI_CONFIG_LIST_yaml_sample lists GPT-4 as the default model, as this represents our current recommendation, and is known to work well with AutoGen. If you use a model other than GPT-4, you may need to revise various system prompts (especially if using weaker models like GPT-3.5-turbo). Moreover, if you use models other than those hosted by OpenAI or Azure, you may incur additional risks related to alignment and safety. Proceed with caution if updating this default.

<p align="right" style="font-size: 14px; color: #555; margin-top: 20px;">
<a href="#readme-top" style="text-decoration: none; color: blue; font-weight: bold;">
Expand Down Expand Up @@ -152,7 +152,7 @@ For [example](https://github.com/microsoft/autogen/blob/main/test/twoagent.py),
from autogen import AssistantAgent, UserProxyAgent, config_list_from_json
# Load LLM inference endpoints from an env variable or a file
# See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints
# and OAI_CONFIG_LIST_sample
# and OAI_CONFIG_LIST_sample/OAI_CONFIG_LIST_yaml_sample
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
# You can also set config_list directly as a list, for example, config_list = [{'model': 'gpt-4', 'api_key': '<your OpenAI API key here>'},]
assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})
Expand Down
50 changes: 34 additions & 16 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Union

import yaml
from dotenv import find_dotenv, load_dotenv
from openai import OpenAI
from openai.types.beta.assistant import Assistant
Expand Down Expand Up @@ -462,10 +463,10 @@ def config_list_from_json(
filter_dict: Optional[Dict[str, Union[List[Union[str, None]], Set[Union[str, None]]]]] = None,
) -> List[Dict[str, Any]]:
"""
Retrieves a list of API configurations from a JSON stored in an environment variable or a file.
Retrieves a list of API configurations from a JSON/YAML stored in an environment variable or a file.

This function attempts to parse JSON data from the given `env_or_file` parameter. If `env_or_file` is an
environment variable containing JSON data, it will be used directly. Otherwise, it is assumed to be a filename,
This function attempts to parse JSON/YAML data from the given `env_or_file` parameter. If `env_or_file` is an
environment variable containing JSON/YAML data, it will be used directly. Otherwise, it is assumed to be a filename,
and the function will attempt to read the file from the specified `file_location`.

The `filter_dict` parameter allows for filtering the configurations based on specified criteria. Each key in the
Expand All @@ -475,21 +476,38 @@ def config_list_from_json(

Args:
env_or_file (str): The name of the environment variable, the filename, or the environment variable of the filename
that containing the JSON data.
that containing the JSON/YAML data.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add explanation for installing pyyaml.

file_location (str, optional): The directory path where the file is located, if `env_or_file` is a filename.
filter_dict (dict, optional): A dictionary specifying the filtering criteria for the configurations, with
keys representing field names and values being lists or sets of acceptable values for those fields.

Example:
```python
# Suppose we have an environment variable 'CONFIG_JSON' with the following content:
# '[{"model": "gpt-3.5-turbo", "api_type": "azure"}, {"model": "gpt-4"}]'

# We can retrieve a filtered list of configurations like this:
filter_criteria = {"model": ["gpt-3.5-turbo"]}
configs = config_list_from_json('CONFIG_JSON', filter_dict=filter_criteria)
# The 'configs' variable will now contain only the configurations that match the filter criteria.
```
JSON:
```python
# Suppose we have an environment variable 'CONFIG_JSON' with the following content:
# '[{"model": "gpt-3.5-turbo", "api_type": "azure"}, {"model": "gpt-4"}]'

# We can retrieve a filtered list of configurations like this:
filter_criteria = {"model": ["gpt-3.5-turbo"]}
configs = config_list_from_json('CONFIG_JSON', filter_dict=filter_criteria)
# The 'configs' variable will now contain only the configurations that match the filter criteria.
```

YAML:
```python
# Suppose we have an environment variable 'CONFIG_YAML' with the following content:
'''
- model: gpt-3.5-turbo
api_type: azure
- model: gpt-4
'''

# We can retrieve a filtered list of configurations like this:
filter_criteria = {"model": ["gpt-3.5-turbo"]}
configs = config_list_from_json('CONFIG_YAML', filter_dict=filter_criteria)
# The 'configs' variable will now contain only the configurations that match the filter criteria.
```

Returns:
List[Dict]: A list of configuration dictionaries that match the filtering criteria specified in `filter_dict`.
Expand All @@ -504,11 +522,11 @@ def config_list_from_json(
if os.path.exists(env_str):
# It is a file location, and we need to load the json from the file.
with open(env_str, "r") as file:
json_str = file.read()
json_or_yaml_str = file.read()
else:
# Else, it should be a JSON string by itself.
json_str = env_str
config_list = json.loads(json_str)
json_or_yaml_str = env_str
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the yaml dependency is not installed, we can fall back to use json module. We can do this by checking for yaml import error during import, and use that information to choose whether to use yaml load or json load.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, it sounds good

config_list = yaml.safe_load(json_or_yaml_str)
else:
# The environment variable does not exist.
# So, `env_or_file` is a filename. We should use the file location.
Expand All @@ -518,7 +536,7 @@ def config_list_from_json(
config_list_path = env_or_file

with open(config_list_path) as json_file:
config_list = json.load(json_file)
config_list = yaml.safe_load(json_file)
return filter_config(config_list, filter_dict)


Expand Down
2 changes: 1 addition & 1 deletion website/blog/2023-10-26-TeachableAgent/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ from autogen import ConversableAgent # As an example
```python
# Load LLM inference endpoints from an env variable or a file
# See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints
# and OAI_CONFIG_LIST_sample
# and OAI_CONFIG_LIST_sample/OAI_CONFIG_LIST_yaml_sample
filter_dict = {"model": ["gpt-4"]} # GPT-3.5 is less reliable than GPT-4 at learning from user feedback.
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST", filter_dict=filter_dict)
llm_config={"config_list": config_list, "timeout": 120}
Expand Down