-
Notifications
You must be signed in to change notification settings - Fork 698
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
chat: flake fixes #1301
Merged
Merged
chat: flake fixes #1301
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -7,31 +7,35 @@ | |||||
|
||||||
OpenAI Assistant API: https://platform.openai.com/docs/api-reference/assistants | ||||||
OpenAI Assistant Playground: https://platform.openai.com/playground | ||||||
|
||||||
AP_FLAKE8_CLEAN | ||||||
''' | ||||||
|
||||||
import datetime, os | ||||||
import datetime | ||||||
import os | ||||||
|
||||||
try: | ||||||
from openai import OpenAI | ||||||
except: | ||||||
except Exception: | ||||||
print("chat: failed to import openai. See https://ardupilot.org/mavproxy/docs/modules/chat.html") | ||||||
exit() | ||||||
|
||||||
|
||||||
# main function | ||||||
def main(openai_api_key=None, delete_unused=False): | ||||||
|
||||||
print("Starting Assistant File checker") | ||||||
|
||||||
# create connection object | ||||||
try: | ||||||
# if api key is provided, use it to create connection object | ||||||
if openai_api_key is not None: | ||||||
client = OpenAI(api_key=openai_api_key) | ||||||
else: | ||||||
# if no api key is provided, attempt to create connection object without it | ||||||
# user may have set the OPENAI_API_KEY environment variable | ||||||
client = OpenAI() | ||||||
except: | ||||||
# if no api key is provided, attempt to create connection object without it | ||||||
# user may have set the OPENAI_API_KEY environment variable | ||||||
client = OpenAI() | ||||||
except Exception: | ||||||
# if connection object creation fails, exit with error message | ||||||
print("assistant_file_checker: failed to connect to OpenAI. Perhaps the API key was incorrect?") | ||||||
exit() | ||||||
|
@@ -41,7 +45,7 @@ def main(openai_api_key=None, delete_unused=False): | |||||
print("File list:") | ||||||
file_found = False | ||||||
for file in existing_files.data: | ||||||
print(" id:" + file.id + " name:" + file.filename + " size:" + str(file.bytes) + " date:" + str(datetime.datetime.fromtimestamp(file.created_at))) | ||||||
print(" id:" + file.id + " name:" + file.filename + " size:" + str(file.bytes) + " date:" + str(datetime.datetime.fromtimestamp(file.created_at))) # noqa | ||||||
file_found = True | ||||||
if not file_found: | ||||||
print(" no files found") | ||||||
|
@@ -64,20 +68,20 @@ def main(openai_api_key=None, delete_unused=False): | |||||
for assistant_file in client.beta.assistants.files.list(assistant_id=assistant.id): | ||||||
file_assistant_count[assistant_file.id] = file_assistant_count.get(assistant_file.id, 0) + 1 | ||||||
filename_size_date = get_filename_size_date_from_id(existing_files, assistant_file.id) | ||||||
print(" id:" + assistant_file.id + " name:" + filename_size_date[0] + " size:" + str(filename_size_date[1]) + " date:" + filename_size_date[2]) | ||||||
print(" id:" + assistant_file.id + " name:" + filename_size_date[0] + " size:" + str(filename_size_date[1]) + " date:" + filename_size_date[2]) # noqa | ||||||
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. f-strings can make your code a little more readable:
Suggested change
|
||||||
|
||||||
# display the list of files which are not used by any Assistants | ||||||
print_unused_files_header = True | ||||||
for existing_file in existing_files: | ||||||
if not existing_file.id in file_assistant_count.keys(): | ||||||
if existing_file.id not in file_assistant_count.keys(): | ||||||
# print header if this is the first unused file | ||||||
if print_unused_files_header: | ||||||
print("Files not used by any Assistants:") | ||||||
print_unused_files_header = False | ||||||
|
||||||
# print file attributes | ||||||
filename_size_date = get_filename_size_date_from_id(existing_files, existing_file.id) | ||||||
print(" id:" + existing_file.id + " name:" + filename_size_date[0] + " size:" + str(filename_size_date[1]) + " date:" + filename_size_date[2]) | ||||||
print(" id:" + existing_file.id + " name:" + filename_size_date[0] + " size:" + str(filename_size_date[1]) + " date:" + filename_size_date[2]) # noqa | ||||||
|
||||||
# delete the unused file if specified by the user | ||||||
if delete_unused: | ||||||
|
@@ -87,6 +91,7 @@ def main(openai_api_key=None, delete_unused=False): | |||||
# print completion message | ||||||
print("Assistant File check complete") | ||||||
|
||||||
|
||||||
# searches the files_list for the specified file id and retuns the filename, size and creation date (as a string) if found | ||||||
# returns None if not found | ||||||
def get_filename_size_date_from_id(files_list, file_id): | ||||||
|
@@ -95,17 +100,18 @@ def get_filename_size_date_from_id(files_list, file_id): | |||||
return file.filename, file.bytes, str(datetime.datetime.fromtimestamp(file.created_at)) | ||||||
return "not found", "unknown", "unknown" | ||||||
|
||||||
|
||||||
# call main function if this is run as standalone script | ||||||
if __name__ == "__main__": | ||||||
|
||||||
# parse command line arguments | ||||||
from argparse import ArgumentParser | ||||||
parser = ArgumentParser(description="MAVProxy Chat Module Assistant File Checker") | ||||||
parser.add_argument("--api-key", default=None, help="OpenAI API Key") | ||||||
parser.add_argument("--delete-unused", action='store_true', help="delete unused files") | ||||||
try: | ||||||
args = parser.parse_args() | ||||||
except AttributeError as err: | ||||||
except AttributeError: | ||||||
parser.print_help() | ||||||
os.sys.exit(0) | ||||||
main(openai_api_key=args.api_key, delete_unused=args.delete_unused) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can pull the result apart directly here instead of indexing
filename_size_date
: