Skip to content

Commit

Permalink
update env method
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Apr 4, 2024
1 parent de73f3c commit 9c986dd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
70 changes: 53 additions & 17 deletions askchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@

__author__ = """Rex Wang"""
__email__ = '[email protected]'
__version__ = '1.1.2'
__version__ = '1.1.3'

import asyncio
from pathlib import Path
import click
from dotenv import set_key
import os

# Main environment file
CONFIG_PATH = Path.home() / ".askchat"
CONFIG_FILE = CONFIG_PATH / ".env"
MAIN_ENV_PATH = Path.home() / '.askchat' / '.env'
ENV_PATH = Path.home() / '.askchat' / 'envs'

raw_env_text = f""""# Description: Env file for askchat.
# Current version: {__version__}
# The base url of the API (with suffix /v1)
# This will override OPENAI_API_BASE_URL if both are set.
OPENAI_API_BASE=''
# The base url of the API (without suffix /v1)
OPENAI_API_BASE_URL=''
# Your API key
OPENAI_API_KEY=''
# The default model name
# You can use `askchat --all-valid-models` to see supported models
OPENAI_API_MODEL=''
"""

# Autocompletion
# environment name completion
class EnvNameCompletionType(click.ParamType):
Expand Down Expand Up @@ -42,20 +62,36 @@ async def show_resp(chat, **options):
print() # add a newline if the message doesn't end with one
return msg

def write_config(config_file, api_key, model, base_url, api_base):
"""Write the environment variables to a config file."""
def write_var(f, var, value, desc):
value = value if value else ""
f.write(f"\n\n# {desc}\n")
f.write(f'{var}="{value}"')
def set_keys(config_file, keys):
"""Set multiple keys in the config file."""
for key, value in keys.items():
if value:
set_key(config_file, key, value)

def raw_config(config_file:str):
"""Empty config file."""
if not CONFIG_PATH.exists():
CONFIG_PATH.mkdir(parents=True)

Check warning on line 74 in askchat/__init__.py

View check run for this annotation

Codecov / codecov/patch

askchat/__init__.py#L74

Added line #L74 was not covered by tests
with open(config_file, "w") as f:
f.write("#Description: Env file for askchat.\n" +\
"#Current version: " + __version__)
# write the environment table
write_var(f, "OPENAI_API_BASE", api_base, "The base url of the API (with suffix /v1)" +\
"\n# This will override OPENAI_API_BASE_URL if both are set.")
write_var(f, "OPENAI_API_BASE_URL", base_url, "The base url of the API (without suffix /v1)")

write_var(f, "OPENAI_API_KEY", api_key, "Your API key")
write_var(f, "OPENAI_API_MODEL", model, "The model name\n" +\
"# You can use `askchat --all-valid-models` to see supported models")
f.write(raw_env_text)

def init_config(config_file:str):
"""Initialize the config file with the current environment variables."""
raw_config(config_file)
set_keys(config_file, {

Check warning on line 81 in askchat/__init__.py

View check run for this annotation

Codecov / codecov/patch

askchat/__init__.py#L80-L81

Added lines #L80 - L81 were not covered by tests
"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY"),
"OPENAI_API_MODEL": os.getenv("OPENAI_API_MODEL"),
"OPENAI_API_BASE_URL": os.getenv("OPENAI_API_BASE_URL"),
"OPENAI_API_BASE": os.getenv("OPENAI_API_BASE"),
})

def write_config(config_file, api_key, model, base_url, api_base, overwrite=False):
"""Write the environment variables to a config file."""
if overwrite or not config_file.exists():
raw_config(config_file)
set_keys(config_file, {
"OPENAI_API_KEY": api_key,
"OPENAI_API_MODEL": model,
"OPENAI_API_BASE_URL": base_url,
"OPENAI_API_BASE": api_base,
})
14 changes: 4 additions & 10 deletions askchat/askenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def new(name, api_key, base_url, api_base, model):
click.confirm("Do you want to continue?", abort=True)
else:
click.echo(f"Environment '{name}' created.")
write_config(config_path, api_key, model, base_url, api_base)
write_config(config_path, api_key, model, base_url, api_base, overwrite=True)

@cli.command()
@click.argument('name', required=False, type=EnvNameCompletionType())
Expand Down Expand Up @@ -120,16 +120,10 @@ def config(name, api_key, base_url, api_base, model):
return
config_path = ENV_PATH / f'{name}.env' if name else MAIN_ENV_PATH
if not config_path.exists():
click.echo(f"Environment '{config_path}' not found.")
click.echo(f"Environment '{config_path}' not found." +\

Check warning on line 123 in askchat/askenv.py

View check run for this annotation

Codecov / codecov/patch

askchat/askenv.py#L123

Added line #L123 was not covered by tests
"Use `askenv new` to create a new environment." )
return
if api_key:
set_key(config_path, "OPENAI_API_KEY", api_key)
if base_url:
set_key(config_path, "OPENAI_API_BASE_URL", base_url)
if api_base:
set_key(config_path, "OPENAI_API_BASE", api_base)
if model:
set_key(config_path, "OPENAI_API_MODEL", model)
write_config(config_path, api_key, model, base_url, api_base)

Check warning on line 126 in askchat/askenv.py

View check run for this annotation

Codecov / codecov/patch

askchat/askenv.py#L126

Added line #L126 was not covered by tests
click.echo(f"Environment {config_path} updated.")

if __name__ == '__main__':
Expand Down
7 changes: 3 additions & 4 deletions askchat/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from chattool import Chat, debug_log
from pathlib import Path
from askchat import (
show_resp, write_config
show_resp, write_config, init_config
, ENV_PATH, MAIN_ENV_PATH
, CONFIG_PATH, CONFIG_FILE
, EnvNameCompletionType, ChatFileCompletionType
Expand Down Expand Up @@ -37,12 +37,11 @@ def setup():
def generate_config_callback(ctx, param, value):
"""Generate a configuration file by environment table."""
if not value: return
api_key, model = os.getenv("OPENAI_API_KEY"), os.getenv("OPENAI_API_MODEL")
base_url, api_base = os.getenv("OPENAI_API_BASE_URL"), os.getenv("OPENAI_API_BASE")
os.makedirs(CONFIG_PATH, exist_ok=True)

Check warning on line 40 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L40

Added line #L40 was not covered by tests
# save the config file
if os.path.exists(CONFIG_FILE):
click.confirm(f"Overwrite the existing configuration file {CONFIG_FILE}?", abort=True)
write_config(CONFIG_FILE, api_key, model, base_url, api_base)
init_config(CONFIG_FILE)

Check warning on line 44 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L44

Added line #L44 was not covered by tests
print("Created config file at", CONFIG_FILE)
ctx.exit()

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from setuptools import setup, find_packages

VERSION = '1.1.2'
VERSION = '1.1.3'

with open('README.md') as readme_file:
readme = readme_file.read()

requirements = ['chattool>=3.1.3', "python-dotenv>=0.17.0", 'Click>=8.0']
requirements = ['chattool>=3.1.4', "python-dotenv>=0.17.0", 'Click>=8.0']

test_requirements = ['pytest>=3']

Expand Down
2 changes: 1 addition & 1 deletion tests/test_askenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_overwrite_environment_confirm(runner, setup_env):
assert "Do you want to continue?" in result.output
# Verify the environment was overwritten by checking if the new API key is in the file
with open(config_path) as f:
assert 'OPENAI_API_KEY="456"' in f.read()
assert "OPENAI_API_KEY='456'" in f.read()

def test_list_initially_empty(runner, setup_env):
"""Ensure no environments are listed when none have been created."""
Expand Down

0 comments on commit 9c986dd

Please sign in to comment.