-
Notifications
You must be signed in to change notification settings - Fork 57
refactor: migrate API key storage from API_KEY.txt to activity_root/data #97
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
c4d5f01
57cb868
77b990d
b734b44
12b80a7
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 |
|---|---|---|
| @@ -1,16 +1,52 @@ | ||
| import os | ||
| import requests | ||
| import json | ||
| import socket | ||
| import logging | ||
|
|
||
| #TODO: Dont hard code these, need to see how sugar as a whole manages API Keys | ||
| from sugar3.activity.activity import get_activity_root | ||
|
|
||
| API_URL = "https://ai.sugarlabs.org/ask-llm-prompted" | ||
| try: | ||
| with open("API_KEY.txt", "r") as f: | ||
| API_KEY = f.read().strip() | ||
| except OSError: | ||
| logging.error("Missing API_KEY.txt file.") | ||
| API_KEY = None | ||
|
|
||
| # Store and read the API key from the activity's persistent data directory | ||
| # instead of a flat file in the bundle. The data/ subdirectory of | ||
| # get_activity_root() survives across activity invocations and is the | ||
| # Sugar-standard location for per-activity configuration. | ||
| _API_KEY_FILENAME = "api_key" | ||
|
|
||
|
|
||
| def _get_api_key_path(): | ||
| """Return the path to the API key file inside activity_root/data/.""" | ||
| data_dir = os.path.join(get_activity_root(), "data") | ||
| os.makedirs(data_dir, exist_ok=True) | ||
| return os.path.join(data_dir, _API_KEY_FILENAME) | ||
|
|
||
|
|
||
| def _read_api_key(): | ||
| """Read the API key from the activity data directory.""" | ||
| path = _get_api_key_path() | ||
| try: | ||
| with open(path, "r") as f: | ||
| key = f.read().strip() | ||
| if key: | ||
| return key | ||
| except OSError: | ||
| pass | ||
| logging.error( | ||
| "Missing API key. Save your key to: %s", path | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
| def save_api_key(key): | ||
| """Persist *key* so it is available on next launch.""" | ||
| path = _get_api_key_path() | ||
| with open(path, "w") as f: | ||
| f.write(key.strip()) | ||
| logging.info("API key saved to %s", path) | ||
|
|
||
|
|
||
| API_KEY = _read_api_key() | ||
|
|
||
| DEFAULT_PROMPT = "You are a friendly teacher named Jane who is 28 years old. You teach 10 year old children. Always give helpful, educational responses in simple words that children can understand. Keep your answers between 20-40 words. Be encouraging and enthusiastic but never use emojis(ever). If you notice spelling mistakes, gently correct them. Stay focused on the topic and give relevant answers." | ||
|
|
||
|
|
@@ -25,7 +61,7 @@ def is_connected(): | |
|
|
||
| def ask_llm_prompted(question, custom_prompt = DEFAULT_PROMPT, timeout=120, max_length=200): | ||
| if API_KEY is None: | ||
| logging.error("Missing API key file: API_KEY.txt") | ||
| logging.error("Missing API key. Use save_api_key() or place it in activity_root/data/api_key") | ||
| return False | ||
|
|
||
| if not is_connected(): | ||
|
|
@@ -88,4 +124,4 @@ def ask_llm_prompted(question, custom_prompt = DEFAULT_PROMPT, timeout=120, max_ | |
| print(f'LLM ANS: {answer}') | ||
|
|
||
| else: | ||
| print("Error, LLM did not respond") | ||
| print("Error, LLM did not respond") | ||
|
Member
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. No newline in EOF. Diff your changes before pushing from next time, you'll be able to catch these.
Author
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. Fixed in 57cb868. Here's the diff confirming the change:
Also noted — will run git diff --staged before every commit going forward.
Fixing.EOF.Line.in.LLM.py.and.Review.Comments.mp4
Member
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. I was asking about a screen recording of you running these changes inside the activity and not just running |
||


Uh oh!
There was an error while loading. Please reload this page.