Skip to content

Commit

Permalink
Merge pull request #55 from blink1073/INTPYTHON-442-lint
Browse files Browse the repository at this point in the history
INTPYTHON-442 Add linting to repo
  • Loading branch information
blink1073 authored Dec 11, 2024
2 parents 336e15b + 775656b commit fdcdd8a
Show file tree
Hide file tree
Showing 49 changed files with 8,341 additions and 8,246 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
# Ruff version.
rev: v0.8.2
hooks:
# - id: ruff
# args: ["--fix", "--show-fixes"]
- id: ruff-format
exclude: notebooks/techniques/evaluating_information_retrival_techniques_mongondb_langchain.ipynb
- id: ruff
args: ["--fix", "--show-fixes"]
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,19 @@
},
"outputs": [],
"source": [
"from haystack import Pipeline, Document\n",
"from haystack.document_stores.types import DuplicatePolicy\n",
"from haystack.components.writers import DocumentWriter\n",
"from haystack.components.generators import OpenAIGenerator\n",
"from bson import json_util\n",
"from haystack import Document, Pipeline\n",
"from haystack.components.builders.prompt_builder import PromptBuilder\n",
"from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder\n",
"from haystack_integrations.document_stores.mongodb_atlas import (\n",
" MongoDBAtlasDocumentStore,\n",
")\n",
"from haystack.components.embedders import OpenAIDocumentEmbedder, OpenAITextEmbedder\n",
"from haystack.components.generators import OpenAIGenerator\n",
"from haystack.components.writers import DocumentWriter\n",
"from haystack.document_stores.types import DuplicatePolicy\n",
"from haystack_integrations.components.retrievers.mongodb_atlas import (\n",
" MongoDBAtlasEmbeddingRetriever,\n",
")\n",
"from datasets import load_dataset\n",
"from bson import json_util\n",
"from haystack_integrations.document_stores.mongodb_atlas import (\n",
" MongoDBAtlasDocumentStore,\n",
")\n",
"\n",
"dataset = {\n",
" \"train\": [\n",
Expand Down Expand Up @@ -1156,8 +1155,9 @@
},
"outputs": [],
"source": [
"from colorama import Fore\n",
"from typing import List\n",
"\n",
"from colorama import Fore\n",
"from haystack import component\n",
"\n",
"\n",
Expand All @@ -1167,9 +1167,8 @@
" def run(self, replies: List[str]):\n",
" if \"DONE\" in replies[0]:\n",
" return {\"recipe\": replies[0].replace(\"done\", \"\")}\n",
" else:\n",
" print(Fore.RED + \"Not done yet, could make recipe more efficient\")\n",
" return {\"recipe_to_check\": replies[0]}"
" print(Fore.RED + \"Not done yet, could make recipe more efficient\")\n",
" return {\"recipe_to_check\": replies[0]}"
]
},
{
Expand Down Expand Up @@ -1555,9 +1554,10 @@
}
],
"source": [
"from pymongo import MongoClient\n",
"import json\n",
"import datetime\n",
"import json\n",
"\n",
"from pymongo import MongoClient\n",
"\n",
"query = \"How can I cook a lasagne?\"\n",
"result = reflecting_rag_pipeline.run(\n",
Expand Down
6 changes: 3 additions & 3 deletions notebooks/agents/agent_fireworks_ai_langchain_mongodb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@
},
"outputs": [],
"source": [
"from langchain_openai import OpenAIEmbeddings\n",
"from langchain_mongodb import MongoDBAtlasVectorSearch\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"embedding_model = OpenAIEmbeddings(model=\"text-embedding-3-small\", dimensions=256)\n",
"\n",
Expand Down Expand Up @@ -556,8 +556,8 @@
"outputs": [],
"source": [
"from langchain.agents import tool\n",
"from langchain_community.document_loaders import ArxivLoader\n",
"from langchain.tools.retriever import create_retriever_tool\n",
"from langchain_community.document_loaders import ArxivLoader\n",
"\n",
"\n",
"# Custom Tool Definiton\n",
Expand Down Expand Up @@ -727,8 +727,8 @@
},
"outputs": [],
"source": [
"from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory\n",
"\n",
"\n",
"def get_session_history(session_id: str) -> MongoDBChatMessageHistory:\n",
Expand Down
2 changes: 0 additions & 2 deletions notebooks/agents/agentchat_RetrieveChat_mongodb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@
}
],
"source": [
"import json\n",
"import os\n",
"\n",
"import autogen\n",
"from autogen import AssistantAgent\n",
"from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"import os\n",
"\n",
"\n",
"# Function to securely get and set environment variables\n",
Expand Down Expand Up @@ -122,6 +122,7 @@
"# Step 1: Data Loading\n",
"import pandas as pd\n",
"from datasets import load_dataset\n",
"\n",
"# Make sure you have an Hugging Face token(HF_TOKEN) in your development environemnt before running the code below\n",
"# How to get a token: https://huggingface.co/docs/hub/en/security-tokens\n",
"\n",
Expand Down Expand Up @@ -961,9 +962,9 @@
" if isinstance(value, (pd.Series, np.ndarray, list)):\n",
" # Handle array-like objects\n",
" if len(value) > 0 and not pd.isna(value).all():\n",
" combined.append(f\"{attr.capitalize()}: {str(value)}\")\n",
" combined.append(f\"{attr.capitalize()}: {value!s}\")\n",
" elif not pd.isna(value):\n",
" combined.append(f\"{attr.capitalize()}: {str(value)}\")\n",
" combined.append(f\"{attr.capitalize()}: {value!s}\")\n",
" return \" \".join(combined)\n",
"\n",
" df[\"combined_info\"] = df.apply(combine_row, axis=1)\n",
Expand Down Expand Up @@ -1056,8 +1057,8 @@
"outputs": [],
"source": [
"import tiktoken\n",
"from tqdm import tqdm\n",
"from langchain_openai import OpenAIEmbeddings\n",
"from tqdm import tqdm\n",
"\n",
"MAX_TOKENS = 8191 # Maximum tokens for text-embedding-3-small\n",
"OVERLAP = 50\n",
Expand Down Expand Up @@ -1116,14 +1117,13 @@
" if isinstance(input_data, str):\n",
" # Return list of embeddings for string input\n",
" return chunk_embeddings[0]\n",
" else:\n",
" # Create duplicated rows for each chunk with the respective embedding for row input\n",
" duplicated_rows = []\n",
" for embedding in chunk_embeddings:\n",
" new_row = input_data.copy()\n",
" new_row[\"embedding\"] = embedding\n",
" duplicated_rows.append(new_row)\n",
" return duplicated_rows"
" # Create duplicated rows for each chunk with the respective embedding for row input\n",
" duplicated_rows = []\n",
" for embedding in chunk_embeddings:\n",
" new_row = input_data.copy()\n",
" new_row[\"embedding\"] = embedding\n",
" duplicated_rows.append(new_row)\n",
" return duplicated_rows"
]
},
{
Expand Down Expand Up @@ -2029,8 +2029,7 @@
" # Connection successful\n",
" print(\"Connection to MongoDB successful\")\n",
" return client\n",
" else:\n",
" print(\"Connection to MongoDB failed\")\n",
" print(\"Connection to MongoDB failed\")\n",
" return None\n",
"\n",
"\n",
Expand Down Expand Up @@ -2059,7 +2058,6 @@
"outputs": [],
"source": [
"# Programmatically create vector search index for both colelctions\n",
"import time\n",
"from pymongo.operations import SearchIndexModel\n",
"\n",
"\n",
Expand All @@ -2086,7 +2084,7 @@
" # time.sleep(20) # Sleep for 20 seconds\n",
" print(f\"New index '{index_name}' created successfully:\", result)\n",
" except Exception as e:\n",
" print(f\"Error creating new vector search index '{index_name}': {str(e)}\")"
" print(f\"Error creating new vector search index '{index_name}': {e!s}\")"
]
},
{
Expand Down Expand Up @@ -2193,7 +2191,6 @@
"outputs": [],
"source": [
"import pandas as pd\n",
"from pymongo import MongoClient\n",
"from pymongo.errors import BulkWriteError\n",
"\n",
"\n",
Expand Down Expand Up @@ -2506,7 +2503,6 @@
"outputs": [],
"source": [
"# Programatically create search indexes\n",
"from pymongo.operations import IndexModel\n",
"\n",
"\n",
"def create_collection_search_index(collection, index_definition, index_name):\n",
Expand All @@ -2531,7 +2527,7 @@
" print(f\"Search index '{index_name}' created successfully\")\n",
" return result\n",
" except Exception as e:\n",
" print(f\"Error creating search index: {str(e)}\")\n",
" print(f\"Error creating search index: {e!s}\")\n",
" return None\n",
"\n",
"\n",
Expand Down Expand Up @@ -2642,9 +2638,9 @@
},
"outputs": [],
"source": [
"from langchain_openai import OpenAIEmbeddings\n",
"from langchain_mongodb import MongoDBAtlasVectorSearch\n",
"from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"ATLAS_VECTOR_SEARCH_INDEX = \"vector_index_with_filter\"\n",
"embedding_model = OpenAIEmbeddings(\n",
Expand Down Expand Up @@ -2802,13 +2798,13 @@
"outputs": [],
"source": [
"import pickle\n",
"from collections.abc import AsyncIterator\n",
"from contextlib import AbstractContextManager\n",
"from datetime import datetime, timezone\n",
"from types import TracebackType\n",
"from typing import Any, Dict, Optional, AsyncIterator, Union, List, Tuple\n",
"from typing import Any, Dict, List, Optional, Tuple, Union\n",
"\n",
"from langchain_core.runnables import RunnableConfig\n",
"from typing_extensions import Self\n",
"\n",
"from langgraph.checkpoint.base import (\n",
" BaseCheckpointSaver,\n",
" Checkpoint,\n",
Expand All @@ -2818,7 +2814,7 @@
")\n",
"from langgraph.checkpoint.serde.jsonplus import JsonPlusSerializer\n",
"from motor.motor_asyncio import AsyncIOMotorClient\n",
"from datetime import datetime, timezone\n",
"from typing_extensions import Self\n",
"\n",
"\n",
"class JsonPlusSerializerCompat(JsonPlusSerializer):\n",
Expand Down Expand Up @@ -3015,7 +3011,8 @@
},
"outputs": [],
"source": [
"from typing import Dict, Any\n",
"from typing import Any, Dict\n",
"\n",
"from langchain.agents import tool\n",
"\n",
"\n",
Expand Down Expand Up @@ -3099,9 +3096,9 @@
},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field, constr\n",
"from typing import List\n",
"from datetime import datetime\n",
"\n",
"from pydantic import BaseModel, Field\n",
"\n",
"\n",
"class Step(BaseModel):\n",
Expand Down Expand Up @@ -3144,7 +3141,7 @@
"\n",
" return document\n",
" except Exception as e:\n",
" raise ValueError(f\"Invalid safety procedure data: {str(e)}\")\n",
" raise ValueError(f\"Invalid safety procedure data: {e!s}\")\n",
"\n",
"\n",
"# Tool to add new safety procedures\n",
Expand Down Expand Up @@ -3323,9 +3320,7 @@
},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_anthropic import ChatAnthropic\n",
"from langchain_groq import ChatGroq\n",
"\n",
"# llm = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
"llm = ChatAnthropic(model=\"claude-3-sonnet-20240229\", temperature=0)\n",
Expand Down Expand Up @@ -3356,9 +3351,10 @@
},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"from datetime import datetime\n",
"\n",
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"\n",
"\n",
"def create_agent(llm, tools, system_message: str):\n",
" \"\"\"Create an agent.\"\"\"\n",
Expand Down Expand Up @@ -3512,9 +3508,10 @@
"outputs": [],
"source": [
"import operator\n",
"from langchain_core.messages import BaseMessage\n",
"from typing import Annotated, TypedDict\n",
"\n",
"from langchain_core.messages import BaseMessage\n",
"\n",
"\n",
"class AgentState(TypedDict):\n",
" messages: Annotated[List[BaseMessage], operator.add]\n",
Expand All @@ -3539,6 +3536,7 @@
"outputs": [],
"source": [
"import functools\n",
"\n",
"from langchain_core.messages import AIMessage, ToolMessage\n",
"\n",
"\n",
Expand Down Expand Up @@ -3676,8 +3674,8 @@
"outputs": [],
"source": [
"import asyncio\n",
"from langchain_core.messages import HumanMessage, AIMessage\n",
"import time\n",
"\n",
"from langchain_core.messages import HumanMessage\n",
"\n",
"\n",
"async def chat_loop():\n",
Expand All @@ -3704,7 +3702,7 @@
" for attempt in range(max_retries):\n",
" try:\n",
" async for chunk in graph.astream(state, config, stream_mode=\"values\"):\n",
" if \"messages\" in chunk and chunk[\"messages\"]:\n",
" if chunk.get(\"messages\"):\n",
" last_message = chunk[\"messages\"][-1]\n",
" if isinstance(last_message, AIMessage):\n",
" last_message.name = (\n",
Expand All @@ -3719,12 +3717,12 @@
" break\n",
" except Exception as e:\n",
" if attempt < max_retries - 1:\n",
" print(f\"\\nAn unexpected error occurred: {str(e)}\")\n",
" print(f\"\\nAn unexpected error occurred: {e!s}\")\n",
" print(f\"\\nRetrying in {retry_delay} seconds...\")\n",
" await asyncio.sleep(retry_delay)\n",
" retry_delay *= 2\n",
" else:\n",
" print(f\"\\nMax retries reached. OpenAI API error: {str(e)}\")\n",
" print(f\"\\nMax retries reached. OpenAI API error: {e!s}\")\n",
" break\n",
"\n",
" print(\"\\n\") # New line after the complete response"
Expand Down
Loading

0 comments on commit fdcdd8a

Please sign in to comment.