-
Notifications
You must be signed in to change notification settings - Fork 0
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
Sourcery refactored main branch #2
base: main
Are you sure you want to change the base?
Conversation
PATH_SEPARATOR = WIN32 and "\\" or "/" | ||
PATH_SEPARATOR = "\\" if WIN32 else "/" |
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.
Lines 27-27
refactored with the following changes:
- Replace boolean ternary with inline if expression (
ternary-to-if-expression
)
# Warn if use_docker was unspecified (or None), and cannot be provided (the default). | ||
# In this case the current behavior is to fall back to run natively, but this behavior | ||
# is subject to change. |
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.
Function execute_code
refactored with the following changes:
- Swap positions of nested conditionals (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements (
hoist-similar-statement-from-if
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
This removes the following comments ( why? ):
# In this case the current behavior is to fall back to run natively, but this behavior
# Warn if use_docker was unspecified (or None), and cannot be provided (the default).
# is subject to change.
if pos == -1: | ||
return response | ||
return response[:pos] | ||
return response if pos == -1 else response[:pos] |
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.
Function _remove_check
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
"success": any(s for s in success_list), | ||
"success": any(success_list), |
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.
Function eval_function_completions
refactored with the following changes:
- Simplify generator expression (
simplify-generator
)
if idx < 0: | ||
return None | ||
if idx < 0: | ||
return None |
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.
Function last_boxed_only_string
refactored with the following changes:
- Hoist conditional out of nested conditional (
hoist-if-from-if
) - Replace if statement with if expression (
assign-if-exp
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if isinstance(message, str): | ||
return {"content": message} | ||
else: | ||
return message | ||
return {"content": message} if isinstance(message, str) else message |
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.
Function ConversableAgent._message_to_dict
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
# When the agent composes and sends the message, the role of the message is "assistant" | ||
# unless it's "function". | ||
valid = self._append_oai_message(message, "assistant", recipient) | ||
if valid: | ||
if valid := self._append_oai_message(message, "assistant", recipient): |
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.
Function ConversableAgent.send
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
This removes the following comments ( why? ):
# unless it's "function".
# When the agent composes and sends the message, the role of the message is "assistant"
# When the agent composes and sends the message, the role of the message is "assistant" | ||
# unless it's "function". | ||
valid = self._append_oai_message(message, "assistant", recipient) | ||
if valid: | ||
if valid := self._append_oai_message(message, "assistant", recipient): |
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.
Function ConversableAgent.a_send
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
This removes the following comments ( why? ):
# unless it's "function".
# When the agent composes and sends the message, the role of the message is "assistant"
else: | ||
if self._consecutive_auto_reply_counter[sender] >= self._max_consecutive_auto_reply_dict[sender]: | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
terminate = self._is_termination_msg(message) | ||
reply = self.get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
if terminate | ||
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply if reply or not terminate else "exit" | ||
elif self._is_termination_msg(message): | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
reply = self.get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply or "exit" | ||
elif self._consecutive_auto_reply_counter[sender] >= self._max_consecutive_auto_reply_dict[sender]: | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
terminate = self._is_termination_msg(message) | ||
reply = self.get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
if terminate | ||
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply if reply or not terminate else "exit" | ||
elif self._is_termination_msg(message): | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
reply = self.get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply or "exit" |
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.
Function ConversableAgent.check_termination_and_human_reply
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Split conditional into multiple branches (
split-or-ifs
) - Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
else: | ||
if self._consecutive_auto_reply_counter[sender] >= self._max_consecutive_auto_reply_dict[sender]: | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
terminate = self._is_termination_msg(message) | ||
reply = await self.a_get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
if terminate | ||
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply if reply or not terminate else "exit" | ||
elif self._is_termination_msg(message): | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
reply = await self.a_get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply or "exit" | ||
elif self._consecutive_auto_reply_counter[sender] >= self._max_consecutive_auto_reply_dict[sender]: | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
terminate = self._is_termination_msg(message) | ||
reply = await self.a_get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
if terminate | ||
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply if reply or not terminate else "exit" | ||
elif self._is_termination_msg(message): | ||
if self.human_input_mode == "NEVER": | ||
reply = "exit" | ||
else: | ||
# self.human_input_mode == "TERMINATE": | ||
reply = await self.a_get_human_input( | ||
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: " | ||
) | ||
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else "" | ||
# if the human input is empty, and the message is a termination message, then we will terminate the conversation | ||
reply = reply or "exit" |
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.
Function ConversableAgent.a_check_termination_and_human_reply
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Split conditional into multiple branches (
split-or-ifs
) - Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
reply = input(prompt) | ||
return reply | ||
return input(prompt) |
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.
Function ConversableAgent.get_human_input
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
reply = input(prompt) | ||
return reply | ||
return input(prompt) |
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.
Function ConversableAgent.a_get_human_input
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
else: | ||
offset = self.agent_names.index(agent.name) + 1 | ||
for i in range(len(self.agents)): | ||
if self.agents[(offset + i) % len(self.agents)] in agents: | ||
return self.agents[(offset + i) % len(self.agents)] | ||
offset = self.agent_names.index(agent.name) + 1 | ||
for i in range(len(self.agents)): | ||
if self.agents[(offset + i) % len(self.agents)] in agents: | ||
return self.agents[(offset + i) % len(self.agents)] |
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.
Function GroupChat.next_agent
refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else
)
contain_code = False | ||
for c in cb: | ||
if c[0] == "python" or c[0] == "wolfram": | ||
contain_code = True | ||
break | ||
contain_code = any(c[0] in ["python", "wolfram"] for c in cb) |
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.
Function _is_termination_msg_mathchat
refactored with the following changes:
- Use any() instead of for loop (
use-any
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
if "=" in last_line: | ||
last_line = "print(" + last_line.split(" = ")[0] + ")" | ||
lines.append(last_line) | ||
else: | ||
lines[-1] = "print(" + last_line + ")" | ||
lines[-1] = f"print({last_line})" |
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.
Function _add_print_to_last_line
refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
if self.verbosity >= 2: | ||
# Use the messaging mechanism so that the analyzer's messages are included in the printed chat. | ||
self.analyzer.reset() # Clear the analyzer's list of messages. | ||
self.send( | ||
recipient=self.analyzer, message=text_to_analyze, request_reply=False | ||
) # Put the message in the analyzer's list. | ||
self.send(recipient=self.analyzer, message=analysis_instructions, request_reply=True) # Request the reply. | ||
return self.last_message(self.analyzer)["content"] | ||
else: | ||
if self.verbosity < 2: | ||
# Use the analyzer's method directly, to leave analyzer message out of the printed chat. | ||
return self.analyzer.analyze_text(text_to_analyze, analysis_instructions) | ||
# Use the messaging mechanism so that the analyzer's messages are included in the printed chat. | ||
self.analyzer.reset() # Clear the analyzer's list of messages. | ||
self.send( | ||
recipient=self.analyzer, message=text_to_analyze, request_reply=False | ||
) # Put the message in the analyzer's list. | ||
self.send(recipient=self.analyzer, message=analysis_instructions, request_reply=True) # Request the reply. | ||
return self.last_message(self.analyzer)["content"] |
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.
Function TeachableAgent.analyze
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
print(colored(" Location = {}".format(self.path_to_dict), "light_green")) | ||
print(colored(f" Location = {self.path_to_dict}", "light_green")) |
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.
Function MemoStore.__init__
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
" ID: {}\n INPUT TEXT: {}\n OUTPUT TEXT: {}".format(uid, input_text, output_text), | ||
f" ID: {uid}\n INPUT TEXT: {input_text}\n OUTPUT TEXT: {output_text}", |
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.
Function MemoStore.list_memos
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
print(colored(" Location = {}".format(self.path_to_dict), "light_green")) | ||
print(colored(f" Location = {self.path_to_dict}", "light_green")) |
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.
Function MemoStore.close
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
"\nINPUT-OUTPUT PAIR ADDED TO VECTOR DATABASE:\n ID\n {}\n INPUT\n {}\n OUTPUT\n {}".format( | ||
self.last_memo_id, input_text, output_text | ||
), | ||
f"\nINPUT-OUTPUT PAIR ADDED TO VECTOR DATABASE:\n ID\n {self.last_memo_id}\n INPUT\n {input_text}\n OUTPUT\n {output_text}", |
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.
Function MemoStore.add_input_output_pair
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
"\nINPUT-OUTPUT PAIR RETRIEVED FROM VECTOR DATABASE:\n INPUT1\n {}\n OUTPUT\n {}\n DISTANCE\n {}".format( | ||
input_text, output_text, distance | ||
), | ||
f"\nINPUT-OUTPUT PAIR RETRIEVED FROM VECTOR DATABASE:\n INPUT1\n {input_text}\n OUTPUT\n {output_text}\n DISTANCE\n {distance}", |
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.
Function MemoStore.get_nearest_memo
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
if n_results > len(self.uid_text_dict): | ||
n_results = len(self.uid_text_dict) | ||
n_results = min(n_results, len(self.uid_text_dict)) |
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.
Function MemoStore.get_related_memos
refactored with the following changes:
- Replace comparison with min/max call (
min-max-identity
) - Replace call to format with f-string (
use-fstring-for-formatting
)
examples = [] | ||
examples.append({"text": "When I say papers I mean research papers, which are typically pdfs.", "label": "yes"}) | ||
examples.append({"text": "Please verify that each paper you listed actually uses langchain.", "label": "no"}) | ||
examples.append({"text": "Tell gpt the output should still be latex code.", "label": "no"}) | ||
examples.append({"text": "Hint: convert pdfs to text and then answer questions based on them.", "label": "yes"}) | ||
examples.append( | ||
{"text": "To create a good PPT, include enough content to make it interesting.", "label": "yes"} | ||
) | ||
examples.append( | ||
examples = [ | ||
{ | ||
"text": "When I say papers I mean research papers, which are typically pdfs.", | ||
"label": "yes", | ||
}, | ||
{ | ||
"text": "Please verify that each paper you listed actually uses langchain.", | ||
"label": "no", | ||
}, | ||
{ | ||
"text": "Tell gpt the output should still be latex code.", | ||
"label": "no", | ||
}, | ||
{ | ||
"text": "Hint: convert pdfs to text and then answer questions based on them.", | ||
"label": "yes", | ||
}, | ||
{ | ||
"text": "To create a good PPT, include enough content to make it interesting.", | ||
"label": "yes", | ||
}, | ||
{ | ||
"text": "No, for this case the columns should be aspects and the rows should be frameworks.", | ||
"label": "no", | ||
} | ||
) | ||
examples.append({"text": "When writing code, remember to include any libraries that are used.", "label": "yes"}) | ||
examples.append({"text": "Please summarize the papers by Eric Horvitz on bounded rationality.", "label": "no"}) | ||
examples.append({"text": "Compare the h-index of Daniel Weld and Oren Etzioni.", "label": "no"}) | ||
examples.append( | ||
}, | ||
{ | ||
"text": "When writing code, remember to include any libraries that are used.", | ||
"label": "yes", | ||
}, | ||
{ | ||
"text": "Please summarize the papers by Eric Horvitz on bounded rationality.", | ||
"label": "no", | ||
}, | ||
{ | ||
"text": "Compare the h-index of Daniel Weld and Oren Etzioni.", | ||
"label": "no", | ||
}, | ||
{ | ||
"text": "Double check to be sure that the columns in a table correspond to what was asked for.", | ||
"label": "yes", | ||
} | ||
) | ||
}, | ||
] |
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.
Function MemoStore.prepopulate
refactored with the following changes:
- Merge append into list declaration [×10] (
merge-list-append
)
output_text = oai.ChatCompletion.extract_text_or_function_call(response)[0] | ||
return output_text | ||
return oai.ChatCompletion.extract_text_or_function_call(response)[0] |
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.
Function TextAnalyzerAgent.analyze_text
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
temperature_or_top_p = params.pop("temperature_or_top_p", None) | ||
if temperature_or_top_p: | ||
if temperature_or_top_p := params.pop("temperature_or_top_p", None): |
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.
Function Completion._get_params_for_create
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
teachable_agent = TeachableAgent( | ||
return TeachableAgent( | ||
name="teachableagent", | ||
llm_config={"config_list": config_list, "request_timeout": 120, "use_cache": use_cache}, | ||
llm_config={ | ||
"config_list": config_list, | ||
"request_timeout": 120, | ||
"use_cache": use_cache, | ||
}, |
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.
Function create_teachable_agent
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
feeds_summary = "\n".join( | ||
return "\n".join( | ||
[ | ||
f"News summary: {f['title']}. {f['summary']} overall_sentiment_score: {f['overall_sentiment_score']}" | ||
for f in feeds | ||
] | ||
) | ||
return feeds_summary |
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.
Function get_market_news
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
teachable_agent = TeachableAgent( | ||
return TeachableAgent( | ||
name="teachableagent", | ||
llm_config={"config_list": config_list, "request_timeout": 120, "use_cache": use_cache}, | ||
llm_config={ | ||
"config_list": config_list, | ||
"request_timeout": 120, | ||
"use_cache": use_cache, | ||
}, |
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.
Function create_teachable_agent
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
for pair in dists.keys(): | ||
for pair in dists: |
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.
Function solve_tsp
refactored with the following changes:
- Remove unnecessary call to keys() (
remove-dict-keys
) - Convert for loop into call to sum() (
sum-comprehension
) - Inline variable that is only used once [×2] (
inline-variable
)
This removes the following comments ( why? ):
# Calculate the cost of the current route
max([len(x["solution"].split()) for x in tune_data]), | ||
max(len(x["solution"].split()) for x in tune_data), |
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.
Function test_math
refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator
) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
Apply Sweep Rules to your PR?
|
Branch
main
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run:Help us improve this pull request!