Skip to content

Commit fdcdd8a

Browse files
authored
Merge pull request #55 from blink1073/INTPYTHON-442-lint
INTPYTHON-442 Add linting to repo
2 parents 336e15b + 775656b commit fdcdd8a

File tree

49 files changed

+8341
-8246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+8341
-8246
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
# Ruff version.
1717
rev: v0.8.2
1818
hooks:
19-
# - id: ruff
20-
# args: ["--fix", "--show-fixes"]
2119
- id: ruff-format
2220
exclude: notebooks/techniques/evaluating_information_retrival_techniques_mongondb_langchain.ipynb
21+
- id: ruff
22+
args: ["--fix", "--show-fixes"]

notebooks/agents/MongoDB_Haystack_self_reflecting_Cooking_agent.ipynb

+16-16
Original file line numberDiff line numberDiff line change
@@ -268,20 +268,19 @@
268268
},
269269
"outputs": [],
270270
"source": [
271-
"from haystack import Pipeline, Document\n",
272-
"from haystack.document_stores.types import DuplicatePolicy\n",
273-
"from haystack.components.writers import DocumentWriter\n",
274-
"from haystack.components.generators import OpenAIGenerator\n",
271+
"from bson import json_util\n",
272+
"from haystack import Document, Pipeline\n",
275273
"from haystack.components.builders.prompt_builder import PromptBuilder\n",
276-
"from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder\n",
277-
"from haystack_integrations.document_stores.mongodb_atlas import (\n",
278-
" MongoDBAtlasDocumentStore,\n",
279-
")\n",
274+
"from haystack.components.embedders import OpenAIDocumentEmbedder, OpenAITextEmbedder\n",
275+
"from haystack.components.generators import OpenAIGenerator\n",
276+
"from haystack.components.writers import DocumentWriter\n",
277+
"from haystack.document_stores.types import DuplicatePolicy\n",
280278
"from haystack_integrations.components.retrievers.mongodb_atlas import (\n",
281279
" MongoDBAtlasEmbeddingRetriever,\n",
282280
")\n",
283-
"from datasets import load_dataset\n",
284-
"from bson import json_util\n",
281+
"from haystack_integrations.document_stores.mongodb_atlas import (\n",
282+
" MongoDBAtlasDocumentStore,\n",
283+
")\n",
285284
"\n",
286285
"dataset = {\n",
287286
" \"train\": [\n",
@@ -1156,8 +1155,9 @@
11561155
},
11571156
"outputs": [],
11581157
"source": [
1159-
"from colorama import Fore\n",
11601158
"from typing import List\n",
1159+
"\n",
1160+
"from colorama import Fore\n",
11611161
"from haystack import component\n",
11621162
"\n",
11631163
"\n",
@@ -1167,9 +1167,8 @@
11671167
" def run(self, replies: List[str]):\n",
11681168
" if \"DONE\" in replies[0]:\n",
11691169
" return {\"recipe\": replies[0].replace(\"done\", \"\")}\n",
1170-
" else:\n",
1171-
" print(Fore.RED + \"Not done yet, could make recipe more efficient\")\n",
1172-
" return {\"recipe_to_check\": replies[0]}"
1170+
" print(Fore.RED + \"Not done yet, could make recipe more efficient\")\n",
1171+
" return {\"recipe_to_check\": replies[0]}"
11731172
]
11741173
},
11751174
{
@@ -1555,9 +1554,10 @@
15551554
}
15561555
],
15571556
"source": [
1558-
"from pymongo import MongoClient\n",
1559-
"import json\n",
15601557
"import datetime\n",
1558+
"import json\n",
1559+
"\n",
1560+
"from pymongo import MongoClient\n",
15611561
"\n",
15621562
"query = \"How can I cook a lasagne?\"\n",
15631563
"result = reflecting_rag_pipeline.run(\n",

notebooks/agents/agent_fireworks_ai_langchain_mongodb.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@
452452
},
453453
"outputs": [],
454454
"source": [
455-
"from langchain_openai import OpenAIEmbeddings\n",
456455
"from langchain_mongodb import MongoDBAtlasVectorSearch\n",
456+
"from langchain_openai import OpenAIEmbeddings\n",
457457
"\n",
458458
"embedding_model = OpenAIEmbeddings(model=\"text-embedding-3-small\", dimensions=256)\n",
459459
"\n",
@@ -556,8 +556,8 @@
556556
"outputs": [],
557557
"source": [
558558
"from langchain.agents import tool\n",
559-
"from langchain_community.document_loaders import ArxivLoader\n",
560559
"from langchain.tools.retriever import create_retriever_tool\n",
560+
"from langchain_community.document_loaders import ArxivLoader\n",
561561
"\n",
562562
"\n",
563563
"# Custom Tool Definiton\n",
@@ -727,8 +727,8 @@
727727
},
728728
"outputs": [],
729729
"source": [
730-
"from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory\n",
731730
"from langchain.memory import ConversationBufferMemory\n",
731+
"from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory\n",
732732
"\n",
733733
"\n",
734734
"def get_session_history(session_id: str) -> MongoDBChatMessageHistory:\n",

notebooks/agents/agentchat_RetrieveChat_mongodb.ipynb

-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@
5454
}
5555
],
5656
"source": [
57-
"import json\n",
5857
"import os\n",
5958
"\n",
60-
"import autogen\n",
6159
"from autogen import AssistantAgent\n",
6260
"from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
6361
"\n",

notebooks/agents/agentic_rag_factory_safety_assistant_with_langgraph_langchain_mongodb.ipynb

+35-37
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
},
3636
"outputs": [],
3737
"source": [
38-
"import os\n",
3938
"import getpass\n",
39+
"import os\n",
4040
"\n",
4141
"\n",
4242
"# Function to securely get and set environment variables\n",
@@ -122,6 +122,7 @@
122122
"# Step 1: Data Loading\n",
123123
"import pandas as pd\n",
124124
"from datasets import load_dataset\n",
125+
"\n",
125126
"# Make sure you have an Hugging Face token(HF_TOKEN) in your development environemnt before running the code below\n",
126127
"# How to get a token: https://huggingface.co/docs/hub/en/security-tokens\n",
127128
"\n",
@@ -961,9 +962,9 @@
961962
" if isinstance(value, (pd.Series, np.ndarray, list)):\n",
962963
" # Handle array-like objects\n",
963964
" if len(value) > 0 and not pd.isna(value).all():\n",
964-
" combined.append(f\"{attr.capitalize()}: {str(value)}\")\n",
965+
" combined.append(f\"{attr.capitalize()}: {value!s}\")\n",
965966
" elif not pd.isna(value):\n",
966-
" combined.append(f\"{attr.capitalize()}: {str(value)}\")\n",
967+
" combined.append(f\"{attr.capitalize()}: {value!s}\")\n",
967968
" return \" \".join(combined)\n",
968969
"\n",
969970
" df[\"combined_info\"] = df.apply(combine_row, axis=1)\n",
@@ -1056,8 +1057,8 @@
10561057
"outputs": [],
10571058
"source": [
10581059
"import tiktoken\n",
1059-
"from tqdm import tqdm\n",
10601060
"from langchain_openai import OpenAIEmbeddings\n",
1061+
"from tqdm import tqdm\n",
10611062
"\n",
10621063
"MAX_TOKENS = 8191 # Maximum tokens for text-embedding-3-small\n",
10631064
"OVERLAP = 50\n",
@@ -1116,14 +1117,13 @@
11161117
" if isinstance(input_data, str):\n",
11171118
" # Return list of embeddings for string input\n",
11181119
" return chunk_embeddings[0]\n",
1119-
" else:\n",
1120-
" # Create duplicated rows for each chunk with the respective embedding for row input\n",
1121-
" duplicated_rows = []\n",
1122-
" for embedding in chunk_embeddings:\n",
1123-
" new_row = input_data.copy()\n",
1124-
" new_row[\"embedding\"] = embedding\n",
1125-
" duplicated_rows.append(new_row)\n",
1126-
" return duplicated_rows"
1120+
" # Create duplicated rows for each chunk with the respective embedding for row input\n",
1121+
" duplicated_rows = []\n",
1122+
" for embedding in chunk_embeddings:\n",
1123+
" new_row = input_data.copy()\n",
1124+
" new_row[\"embedding\"] = embedding\n",
1125+
" duplicated_rows.append(new_row)\n",
1126+
" return duplicated_rows"
11271127
]
11281128
},
11291129
{
@@ -2029,8 +2029,7 @@
20292029
" # Connection successful\n",
20302030
" print(\"Connection to MongoDB successful\")\n",
20312031
" return client\n",
2032-
" else:\n",
2033-
" print(\"Connection to MongoDB failed\")\n",
2032+
" print(\"Connection to MongoDB failed\")\n",
20342033
" return None\n",
20352034
"\n",
20362035
"\n",
@@ -2059,7 +2058,6 @@
20592058
"outputs": [],
20602059
"source": [
20612060
"# Programmatically create vector search index for both colelctions\n",
2062-
"import time\n",
20632061
"from pymongo.operations import SearchIndexModel\n",
20642062
"\n",
20652063
"\n",
@@ -2086,7 +2084,7 @@
20862084
" # time.sleep(20) # Sleep for 20 seconds\n",
20872085
" print(f\"New index '{index_name}' created successfully:\", result)\n",
20882086
" except Exception as e:\n",
2089-
" print(f\"Error creating new vector search index '{index_name}': {str(e)}\")"
2087+
" print(f\"Error creating new vector search index '{index_name}': {e!s}\")"
20902088
]
20912089
},
20922090
{
@@ -2193,7 +2191,6 @@
21932191
"outputs": [],
21942192
"source": [
21952193
"import pandas as pd\n",
2196-
"from pymongo import MongoClient\n",
21972194
"from pymongo.errors import BulkWriteError\n",
21982195
"\n",
21992196
"\n",
@@ -2506,7 +2503,6 @@
25062503
"outputs": [],
25072504
"source": [
25082505
"# Programatically create search indexes\n",
2509-
"from pymongo.operations import IndexModel\n",
25102506
"\n",
25112507
"\n",
25122508
"def create_collection_search_index(collection, index_definition, index_name):\n",
@@ -2531,7 +2527,7 @@
25312527
" print(f\"Search index '{index_name}' created successfully\")\n",
25322528
" return result\n",
25332529
" except Exception as e:\n",
2534-
" print(f\"Error creating search index: {str(e)}\")\n",
2530+
" print(f\"Error creating search index: {e!s}\")\n",
25352531
" return None\n",
25362532
"\n",
25372533
"\n",
@@ -2642,9 +2638,9 @@
26422638
},
26432639
"outputs": [],
26442640
"source": [
2645-
"from langchain_openai import OpenAIEmbeddings\n",
26462641
"from langchain_mongodb import MongoDBAtlasVectorSearch\n",
26472642
"from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever\n",
2643+
"from langchain_openai import OpenAIEmbeddings\n",
26482644
"\n",
26492645
"ATLAS_VECTOR_SEARCH_INDEX = \"vector_index_with_filter\"\n",
26502646
"embedding_model = OpenAIEmbeddings(\n",
@@ -2802,13 +2798,13 @@
28022798
"outputs": [],
28032799
"source": [
28042800
"import pickle\n",
2801+
"from collections.abc import AsyncIterator\n",
28052802
"from contextlib import AbstractContextManager\n",
2803+
"from datetime import datetime, timezone\n",
28062804
"from types import TracebackType\n",
2807-
"from typing import Any, Dict, Optional, AsyncIterator, Union, List, Tuple\n",
2805+
"from typing import Any, Dict, List, Optional, Tuple, Union\n",
28082806
"\n",
28092807
"from langchain_core.runnables import RunnableConfig\n",
2810-
"from typing_extensions import Self\n",
2811-
"\n",
28122808
"from langgraph.checkpoint.base import (\n",
28132809
" BaseCheckpointSaver,\n",
28142810
" Checkpoint,\n",
@@ -2818,7 +2814,7 @@
28182814
")\n",
28192815
"from langgraph.checkpoint.serde.jsonplus import JsonPlusSerializer\n",
28202816
"from motor.motor_asyncio import AsyncIOMotorClient\n",
2821-
"from datetime import datetime, timezone\n",
2817+
"from typing_extensions import Self\n",
28222818
"\n",
28232819
"\n",
28242820
"class JsonPlusSerializerCompat(JsonPlusSerializer):\n",
@@ -3015,7 +3011,8 @@
30153011
},
30163012
"outputs": [],
30173013
"source": [
3018-
"from typing import Dict, Any\n",
3014+
"from typing import Any, Dict\n",
3015+
"\n",
30193016
"from langchain.agents import tool\n",
30203017
"\n",
30213018
"\n",
@@ -3099,9 +3096,9 @@
30993096
},
31003097
"outputs": [],
31013098
"source": [
3102-
"from pydantic import BaseModel, Field, constr\n",
31033099
"from typing import List\n",
3104-
"from datetime import datetime\n",
3100+
"\n",
3101+
"from pydantic import BaseModel, Field\n",
31053102
"\n",
31063103
"\n",
31073104
"class Step(BaseModel):\n",
@@ -3144,7 +3141,7 @@
31443141
"\n",
31453142
" return document\n",
31463143
" except Exception as e:\n",
3147-
" raise ValueError(f\"Invalid safety procedure data: {str(e)}\")\n",
3144+
" raise ValueError(f\"Invalid safety procedure data: {e!s}\")\n",
31483145
"\n",
31493146
"\n",
31503147
"# Tool to add new safety procedures\n",
@@ -3323,9 +3320,7 @@
33233320
},
33243321
"outputs": [],
33253322
"source": [
3326-
"from langchain_openai import ChatOpenAI\n",
33273323
"from langchain_anthropic import ChatAnthropic\n",
3328-
"from langchain_groq import ChatGroq\n",
33293324
"\n",
33303325
"# llm = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
33313326
"llm = ChatAnthropic(model=\"claude-3-sonnet-20240229\", temperature=0)\n",
@@ -3356,9 +3351,10 @@
33563351
},
33573352
"outputs": [],
33583353
"source": [
3359-
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
33603354
"from datetime import datetime\n",
33613355
"\n",
3356+
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
3357+
"\n",
33623358
"\n",
33633359
"def create_agent(llm, tools, system_message: str):\n",
33643360
" \"\"\"Create an agent.\"\"\"\n",
@@ -3512,9 +3508,10 @@
35123508
"outputs": [],
35133509
"source": [
35143510
"import operator\n",
3515-
"from langchain_core.messages import BaseMessage\n",
35163511
"from typing import Annotated, TypedDict\n",
35173512
"\n",
3513+
"from langchain_core.messages import BaseMessage\n",
3514+
"\n",
35183515
"\n",
35193516
"class AgentState(TypedDict):\n",
35203517
" messages: Annotated[List[BaseMessage], operator.add]\n",
@@ -3539,6 +3536,7 @@
35393536
"outputs": [],
35403537
"source": [
35413538
"import functools\n",
3539+
"\n",
35423540
"from langchain_core.messages import AIMessage, ToolMessage\n",
35433541
"\n",
35443542
"\n",
@@ -3676,8 +3674,8 @@
36763674
"outputs": [],
36773675
"source": [
36783676
"import asyncio\n",
3679-
"from langchain_core.messages import HumanMessage, AIMessage\n",
3680-
"import time\n",
3677+
"\n",
3678+
"from langchain_core.messages import HumanMessage\n",
36813679
"\n",
36823680
"\n",
36833681
"async def chat_loop():\n",
@@ -3704,7 +3702,7 @@
37043702
" for attempt in range(max_retries):\n",
37053703
" try:\n",
37063704
" async for chunk in graph.astream(state, config, stream_mode=\"values\"):\n",
3707-
" if \"messages\" in chunk and chunk[\"messages\"]:\n",
3705+
" if chunk.get(\"messages\"):\n",
37083706
" last_message = chunk[\"messages\"][-1]\n",
37093707
" if isinstance(last_message, AIMessage):\n",
37103708
" last_message.name = (\n",
@@ -3719,12 +3717,12 @@
37193717
" break\n",
37203718
" except Exception as e:\n",
37213719
" if attempt < max_retries - 1:\n",
3722-
" print(f\"\\nAn unexpected error occurred: {str(e)}\")\n",
3720+
" print(f\"\\nAn unexpected error occurred: {e!s}\")\n",
37233721
" print(f\"\\nRetrying in {retry_delay} seconds...\")\n",
37243722
" await asyncio.sleep(retry_delay)\n",
37253723
" retry_delay *= 2\n",
37263724
" else:\n",
3727-
" print(f\"\\nMax retries reached. OpenAI API error: {str(e)}\")\n",
3725+
" print(f\"\\nMax retries reached. OpenAI API error: {e!s}\")\n",
37283726
" break\n",
37293727
"\n",
37303728
" print(\"\\n\") # New line after the complete response"

0 commit comments

Comments
 (0)