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

Add new environment variable for base URL and update code to use it #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tasks": {
"test": "pip install -r requirements.txt && pytest",
"build": "pip install -r requirements.txt",
"launch": "python main.py"
}
}
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ANTHROPIC_API_KEY="YOUR API KEY"
TAVILY_API_KEY="YOUR API KEY"
ELEVEN_LABS_API_KEY="YOUR API KEY"
BASE_URL="YOUR BASE URL"
5 changes: 5 additions & 0 deletions `pytest.ini` (ADD)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ def setup_virtual_environment() -> Tuple[str, str]:
raise ValueError("TAVILY_API_KEY not found in environment variables")
tavily = TavilyClient(api_key=tavily_api_key)

# Initialize the base URL
base_url = os.getenv("BASE_URL")
if not base_url:
raise ValueError("BASE_URL not found in environment variables")

console = Console()

# Token tracking variables
Expand Down Expand Up @@ -1268,7 +1273,7 @@ def save_chat():
elif message['role'] == 'assistant':
if isinstance(message['content'], str):
formatted_chat += f"## Claude\n\n{message['content']}\n\n"
elif isinstance(message['content'], list):
elif isinstance(message['content'], list]):
for content in message['content']:
if content['type'] == 'tool_use':
formatted_chat += f"### Tool Use: {content['name']}\n\n```json\n{json.dumps(content['input'], indent=2)}\n```\n\n"
Expand Down
44 changes: 44 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from flask import Flask, render_template, request, redirect, url_for
import os
from dotenv import load_dotenv

app = Flask(__name__)

# Load environment variables from .env file
load_dotenv()

@app.route('/settings', methods=['GET', 'POST'])
def settings():
if request.method == 'POST':
# Update environment variables
anthropic_api_key = request.form.get('ANTHROPIC_API_KEY')
tavily_api_key = request.form.get('TAVILY_API_KEY')
eleven_labs_api_key = request.form.get('ELEVEN_LABS_API_KEY')
base_url = request.form.get('BASE_URL')

# Save the updated environment variables to the .env file
with open('.env', 'w') as f:
f.write(f"ANTHROPIC_API_KEY={anthropic_api_key}\n")
f.write(f"TAVILY_API_KEY={tavily_api_key}\n")
f.write(f"ELEVEN_LABS_API_KEY={eleven_labs_api_key}\n")
f.write(f"BASE_URL={base_url}\n")

# Reload the environment variables
load_dotenv()

return redirect(url_for('settings'))

# Load current environment variables
anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')
tavily_api_key = os.getenv('TAVILY_API_KEY')
eleven_labs_api_key = os.getenv('ELEVEN_LABS_API_KEY')
base_url = os.getenv('BASE_URL')

return render_template('settings.html',
anthropic_api_key=anthropic_api_key,
tavily_api_key=tavily_api_key,
eleven_labs_api_key=eleven_labs_api_key,
base_url=base_url)

if __name__ == '__main__':
app.run(debug=True)
72 changes: 72 additions & 0 deletions tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import pytest
from dotenv import load_dotenv
from flask import Flask, render_template, request, redirect, url_for

# Load environment variables from .env file
load_dotenv()

# Initialize the Flask app
app = Flask(__name__)

# Define the settings route
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if request.method == 'POST':
# Update environment variables
anthropic_api_key = request.form.get('ANTHROPIC_API_KEY')
tavily_api_key = request.form.get('TAVILY_API_KEY')
eleven_labs_api_key = request.form.get('ELEVEN_LABS_API_KEY')
base_url = request.form.get('BASE_URL')

# Save the updated environment variables to the .env file
with open('.env', 'w') as f:
f.write(f"ANTHROPIC_API_KEY={anthropic_api_key}\n")
f.write(f"TAVILY_API_KEY={tavily_api_key}\n")
f.write(f"ELEVEN_LABS_API_KEY={eleven_labs_api_key}\n")
f.write(f"BASE_URL={base_url}\n")

# Reload the environment variables
load_dotenv()

return redirect(url_for('settings'))

# Load current environment variables
anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')
tavily_api_key = os.getenv('TAVILY_API_KEY')
eleven_labs_api_key = os.getenv('ELEVEN_LABS_API_KEY')
base_url = os.getenv('BASE_URL')

return render_template('settings.html',
anthropic_api_key=anthropic_api_key,
tavily_api_key=tavily_api_key,
eleven_labs_api_key=eleven_labs_api_key,
base_url=base_url)

# Test functions
def test_settings_page(client):
response = client.get('/settings')
assert response.status_code == 200
assert b'ANTHROPIC_API_KEY' in response.data
assert b'TAVILY_API_KEY' in response.data
assert b'ELEVEN_LABS_API_KEY' in response.data
assert b'BASE_URL' in response.data

def test_update_settings(client):
response = client.post('/settings', data={
'ANTHROPIC_API_KEY': 'new_anthropic_api_key',
'TAVILY_API_KEY': 'new_tavily_api_key',
'ELEVEN_LABS_API_KEY': 'new_eleven_labs_api_key',
'BASE_URL': 'new_base_url'
}, follow_redirects=True)
assert response.status_code == 200
assert b'new_anthropic_api_key' in response.data
assert b'new_tavily_api_key' in response.data
assert b'new_eleven_labs_api_key' in response.data
assert b'new_base_url' in response.data

@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client