-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
c4166cd
ad32f63
3e04d2c
1a42c46
b97b99d
6abc696
1f0f407
15e4896
2410ebc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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. | ||
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`. | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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) | ||
|
||
|
||
|
There was a problem hiding this comment.
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
.