-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Zhou-Shilin/v2ui-config
feat: Add config edit feature to v2-ui
- Loading branch information
Showing
4 changed files
with
150 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,55 @@ | ||
import yaml | ||
from log_writer import logger | ||
|
||
with open("config.yaml", "r") as conf: | ||
config_content = yaml.safe_load(conf) | ||
for key, value in config_content.items(): | ||
if key == "CODING_MODEL" and value == "gpt-4": | ||
globals()[key] = "gpt-4-turbo-preview" # Force using gpt-4-turbo-preview if the user set the CODING_MODEL to gpt-4. Because gpt-4 is not longer supports json modes. | ||
globals()[key] = value | ||
logger(f"config: {key} -> {value}") | ||
def load_config(): | ||
""" | ||
Loads the configuration from the 'config.yaml' file and sets the global variables accordingly. | ||
If the 'GENERATE_MODEL' key in the configuration is set to 'gpt-4', it forces the use of 'gpt-4-turbo-preview' | ||
as the value for the 'GENERATE_MODEL' key, since 'gpt-4' no longer supports json modes. | ||
Returns: | ||
None | ||
""" | ||
with open("config.yaml", "r") as conf: | ||
config_content = yaml.safe_load(conf) | ||
for key, value in config_content.items(): | ||
if key == "GENERATE_MODEL" and value == "gpt-4": | ||
globals()[key] = "gpt-4-turbo-preview" # Force using gpt-4-turbo-preview if the user set the GENERATE_MODEL to gpt-4. Because gpt-4 is not longer supports json modes. | ||
globals()[key] = value | ||
logger(f"config: {key} -> {value}") | ||
|
||
def edit_config(key, value): | ||
with open("config.yaml", "r") as file: | ||
lines = file.readlines() | ||
""" | ||
Edits the config file. | ||
Args: | ||
key (str): The key to edit. | ||
value (str): The value to set. | ||
Returns: | ||
bool: True | ||
""" | ||
|
||
for i, line in enumerate(lines): | ||
if f"{key}:" in line: | ||
lines[i] = line.replace(line.split(":")[1].strip().strip('"'), f"{value}") | ||
with open("config.yaml", "r") as conf: | ||
config_content = conf.readlines() | ||
|
||
with open("config.yaml", "w") as conf: | ||
for line in config_content: | ||
if line.startswith(key): | ||
if value == True: | ||
write_value = "True" | ||
elif value == False: | ||
write_value = "False" | ||
else: | ||
write_value = f"\"{value}\"" | ||
if "#" in line: | ||
conf.write(f"{key}: {write_value} # {line.split('#')[1]}\n") | ||
else: | ||
conf.write(f"{key}: {write_value}\n") | ||
else: | ||
conf.write(line) | ||
|
||
with open("config.yaml", "w") as file: | ||
file.writelines(lines) | ||
return True | ||
|
||
logger(f"edit_config: {key} -> {value}") | ||
load_config() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters