Conversation
| prompt = mongo.prompts.find_one({"bot_id": self.bot_id}) | ||
| if not prompt: | ||
| if prompt := mongo.prompts.find_one({"bot_id": self.bot_id}): | ||
| return prompt | ||
| else: | ||
| return {SUMMARIZATION_PROMPT: None, SYSTEM_MESSAGE_PROMPT: None} | ||
| return prompt |
There was a problem hiding this comment.
Function PromptsClass._load_prompts refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches)
| if not bot_id: | ||
| return None | ||
| return PromptsClass(bot_id) | ||
| return None if not bot_id else PromptsClass(bot_id) |
There was a problem hiding this comment.
Function load_prompts 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)
| with Session() as session: | ||
| chats = session.query(ChatHistory).limit(limit).offset(offset).all() | ||
| return chats | ||
| return session.query(ChatHistory).limit(limit).offset(offset).all() |
There was a problem hiding this comment.
Function get_all_chat_history refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| else: | ||
| if user_query is not None: | ||
| chat_history.append((user_query, str(entry.message))) | ||
| user_query = None | ||
| elif user_query is not None: | ||
| chat_history.append((user_query, str(entry.message))) | ||
| user_query = None |
There was a problem hiding this comment.
Function get_chat_history_for_retrieval_chain refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
| # Get the first message in each session | ||
| first_message = ( | ||
| if first_message := ( | ||
| session.query(ChatHistory) | ||
| .filter_by(chatbot_id=bot_id, session_id=session_id[0]) | ||
| .order_by(ChatHistory.id.asc()) | ||
| .first() | ||
| ) | ||
|
|
||
| # Convert ChatHistory object to a dictionary | ||
| if first_message: | ||
| ): |
There was a problem hiding this comment.
Function get_unique_sessions_with_first_message_by_bot_id refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Merge append into list declaration (
merge-list-append)
This removes the following comments ( why? ):
# Convert ChatHistory object to a dictionary
# Get the first message in each session
| chat_history_sessions = get_unique_sessions_with_first_message_by_bot_id( | ||
| return get_unique_sessions_with_first_message_by_bot_id( | ||
| bot_id, limit, offset | ||
| ) | ||
|
|
||
| return chat_history_sessions |
There was a problem hiding this comment.
Function get_chat_sessions refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if file: | ||
| try: | ||
| filename = secure_filename(str(uuid.uuid4()) + ".json") | ||
| filename = secure_filename(f"{str(uuid.uuid4())}.json") |
There was a problem hiding this comment.
Function handle_swagger_file refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Replace call to format with f-string (
use-fstring-for-formatting)
| if request.headers.get("Authorization") == f"Bearer {SECRET_KEY}": | ||
| response = reindex_service.reindex_apis() | ||
| return Response(response=response, status=200) | ||
| else: | ||
| if request.headers.get("Authorization") != f"Bearer {SECRET_KEY}": | ||
| return Response(response="Unauthorized", status=401) | ||
| response = reindex_service.reindex_apis() | ||
| return Response(response=response, status=200) |
There was a problem hiding this comment.
Function reindex_apis refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| pdf_sources = [] | ||
|
|
||
| for ds in pdf_datasources: | ||
| pdf_sources.append( | ||
| { | ||
| "id": ds.id, | ||
| "chatbot_id": ds.created_at, | ||
| "source": ds.file_name, | ||
| "status": ds.status, | ||
| "updated_at": ds.updated_at, | ||
| } | ||
| ) | ||
|
|
||
| web_sources = [] | ||
| pdf_sources = [ | ||
| { | ||
| "id": ds.id, | ||
| "chatbot_id": ds.created_at, | ||
| "source": ds.file_name, | ||
| "status": ds.status, | ||
| "updated_at": ds.updated_at, | ||
| } | ||
| for ds in pdf_datasources | ||
| ] | ||
| web_datasources = get_all_website_datasource_by_bot_id(bot_id, limit, offset) | ||
|
|
||
| for wds in web_datasources: | ||
| web_sources.append( | ||
| { | ||
| "id": wds.id, | ||
| "chatbot_id": wds.created_at, | ||
| "source": wds.url, | ||
| "status": wds.status, | ||
| "updated_at": wds.updated_at, | ||
| } | ||
| ) | ||
| web_sources = [ | ||
| { | ||
| "id": wds.id, | ||
| "chatbot_id": wds.created_at, | ||
| "source": wds.url, | ||
| "status": wds.status, | ||
| "updated_at": wds.updated_at, | ||
| } | ||
| for wds in web_datasources | ||
| ] |
There was a problem hiding this comment.
Function get_data_sources refactored with the following changes:
- Convert for loop into list comprehension [×2] (
list-comprehension) - Move assignment closer to its usage within a block (
move-assign-in-block)
| # Recursively process dictionary values | ||
| truncated_dict = {} | ||
| for key, value in json_obj.items(): | ||
| truncated_dict[key] = truncate_json(value, max_elements) | ||
| return truncated_dict | ||
| return { | ||
| key: truncate_json(value, max_elements) | ||
| for key, value in json_obj.items() | ||
| } |
There was a problem hiding this comment.
Function truncate_json refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# Recursively process dictionary values
| return Response(status=404) | ||
|
|
||
| return Response(prompt, status=200) | ||
| return Response(status=404) if prompt is None else Response(prompt, status=200) |
There was a problem hiding this comment.
Function get_prompt 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)
| data = Prompt(**request.get_json()) | ||
| return data | ||
| return Prompt(**request.get_json()) |
There was a problem hiding this comment.
Function get_validated_prompt_data refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| try: | ||
| data = PromptTemplate(**request.get_json()) | ||
| return data | ||
| return PromptTemplate(**request.get_json()) |
There was a problem hiding this comment.
Function get_validated_prompt_template_data refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if request.json: | ||
| chatbot_id = request.json["chatbot_id"] | ||
| file_name = request.json["file_name"] | ||
| celery.send_task( | ||
| "workers.pdf_crawl.retry_failed_pdf_crawl", args=[chatbot_id, file_name] | ||
| ) | ||
|
|
||
| return Response( | ||
| status=415, | ||
| response={ | ||
| "status": "415 Unsupported Media Type", | ||
| "error": "Unsupported Media Type", | ||
| }, | ||
| mimetype="application/json", | ||
| ) | ||
| else: | ||
| if not request.json: | ||
| return Response( | ||
| status=200, | ||
| response={"status": "retrying", "error": None}, | ||
| mimetype="application/json", | ||
| ) | ||
| chatbot_id = request.json["chatbot_id"] | ||
| file_name = request.json["file_name"] | ||
| celery.send_task( | ||
| "workers.pdf_crawl.retry_failed_pdf_crawl", args=[chatbot_id, file_name] | ||
| ) | ||
|
|
||
| return Response( | ||
| status=415, | ||
| response={ | ||
| "status": "415 Unsupported Media Type", | ||
| "error": "Unsupported Media Type", | ||
| }, | ||
| mimetype="application/json", | ||
| ) |
There was a problem hiding this comment.
Function retry_failed_pdf_crawl refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| def get_workflow(workflow_id: str) -> Any: | ||
| workflow = mongo.workflows.find_one({"_id": ObjectId(workflow_id)}) | ||
| if workflow: | ||
| if workflow := mongo.workflows.find_one({"_id": ObjectId(workflow_id)}): |
There was a problem hiding this comment.
Function get_workflow refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| messages: List[BaseMessage] = [] | ||
| messages.append(system_message_classifier) | ||
|
|
||
| if len(prev_conversations) > 0: | ||
| messages: List[BaseMessage] = [system_message_classifier] | ||
| if prev_conversations: | ||
| messages.extend(prev_conversations) | ||
|
|
||
| if context and len(api_summaries) > 0 and len(flows) > 0: | ||
| if context and api_summaries and flows: |
There was a problem hiding this comment.
Function process_conversation_step refactored with the following changes:
- Merge append into list declaration (
merge-list-append) - Simplify sequence length comparison [×3] (
simplify-len-comparison) - Remove redundant pass statement (
remove-redundant-pass) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| website_data_source = WebsiteDataSource.query( | ||
| return WebsiteDataSource.query( | ||
| WebsiteDataSource.id == website_data_source_id | ||
| ).get() | ||
|
|
||
| return website_data_source No newline at end of file | ||
| ).get() No newline at end of file |
There was a problem hiding this comment.
Function get_website_data_source_by_id refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
|
|
||
| # Callbacks support token-wise streaming | ||
| callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) | ||
| llm = LlamaCpp( | ||
| return LlamaCpp( |
There was a problem hiding this comment.
Function get_llama_llm refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if store_type == StoreType.QDRANT.value: | ||
| client = qdrant_client.QdrantClient( | ||
| url=os.environ["QDRANT_URL"], | ||
| prefer_grpc=True, | ||
| api_key=os.getenv("QDRANT_API_KEY", None), | ||
| ) | ||
|
|
||
| vector_store = Qdrant( | ||
| client, collection_name=options.namespace, embeddings=embedding | ||
| ) | ||
|
|
||
| # vector_store = Qdrant.from_documents([], embedding, url='http://localhost:6333', collection=options.namespace) | ||
|
|
||
| else: | ||
| if store_type != StoreType.QDRANT.value: | ||
| raise ValueError("Invalid STORE environment variable value") | ||
|
|
||
| return vector_store No newline at end of file | ||
| client = qdrant_client.QdrantClient( | ||
| url=os.environ["QDRANT_URL"], | ||
| prefer_grpc=True, | ||
| api_key=os.getenv("QDRANT_API_KEY", None), | ||
| ) | ||
|
|
||
| return Qdrant( | ||
| client, collection_name=options.namespace, embeddings=embedding | ||
| ) No newline at end of file |
There was a problem hiding this comment.
Function get_vector_store refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# vector_store = Qdrant.from_documents([], embedding, url='http://localhost:6333', collection=options.namespace)
| characters = string.ascii_letters + string.digits | ||
| token = "".join(secrets.choice(characters) for i in range(length)) | ||
| return token | ||
| return "".join(secrets.choice(characters) for _ in range(length)) |
There was a problem hiding this comment.
Function generate_random_token refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Replace unused for index with underscore (
for-index-underscore)
| return "shared_data/" + filename | ||
| return f"shared_data/{filename}" |
There was a problem hiding this comment.
Function resolve_abs_local_file_path_from refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| validations = { | ||
| 'endpoints_without_operation_id': [endpoint.to_dict() for endpoint in get_endpoints_without_operation_id(endpoints)], | ||
| 'endpoints_without_description': [endpoint.to_dict() for endpoint in get_endpoints_without_description(endpoints)], | ||
| 'endpoints_without_name': [endpoint.to_dict() for endpoint in get_endpoints_without_name(endpoints)], | ||
| 'auth_type': self.get_authorization_type() | ||
| return { | ||
| 'endpoints_without_operation_id': [ | ||
| endpoint.to_dict() | ||
| for endpoint in get_endpoints_without_operation_id(endpoints) | ||
| ], | ||
| 'endpoints_without_description': [ | ||
| endpoint.to_dict() | ||
| for endpoint in get_endpoints_without_description(endpoints) | ||
| ], | ||
| 'endpoints_without_name': [ | ||
| endpoint.to_dict() | ||
| for endpoint in get_endpoints_without_name(endpoints) | ||
| ], | ||
| 'auth_type': self.get_authorization_type(), | ||
| } | ||
| return validations |
There was a problem hiding this comment.
Function SwaggerParser.get_validations refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| page_content=workflow_data["info"]["title"], | ||
| metadata={ | ||
| "workflow_id": str(workflow_id), | ||
| "workflow_id": workflow_id, |
There was a problem hiding this comment.
Function add_workflow_data_to_qdrant refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast)
Branch
mainrefactored 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
mainbranch, then run:Help us improve this pull request!