├── .gitignore ├── test.py ├── 1.LLMs └── 1_llm_demo.py ├── 2.ChatModels ├── 2_chatmodel_anthropic.py ├── 3_chatmodel_google.py ├── 1_chatmodel_openai.py ├── 4_chatmodel_hf_api.py └── 5_chatmodel_hf_local.py ├── 3.EmbeddingModels ├── 1_embedding_openai_query.py ├── 3_embedding_hf_local.py ├── 2_embedding_openai_docs.py └── 4_document_similarity.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | venv -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import langchain 2 | 3 | print(langchain.__version__) -------------------------------------------------------------------------------- /1.LLMs/1_llm_demo.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import OpenAI 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | llm = OpenAI(model='gpt-3.5-turbo-instruct') 7 | 8 | result = llm.invoke("What is the capital of India") 9 | 10 | print(result) -------------------------------------------------------------------------------- /2.ChatModels/2_chatmodel_anthropic.py: -------------------------------------------------------------------------------- 1 | from langchain_anthropic import ChatAnthropic 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | model = ChatAnthropic(model='claude-3-5-sonnet-20241022') 7 | 8 | result = model.invoke('What is the capital of India') 9 | 10 | print(result.content) -------------------------------------------------------------------------------- /2.ChatModels/3_chatmodel_google.py: -------------------------------------------------------------------------------- 1 | from langchain_google_genai import ChatGoogleGenerativeAI 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | model = ChatGoogleGenerativeAI(model='gemini-1.5-pro') 7 | 8 | result = model.invoke('What is the capital of India') 9 | 10 | print(result.content) -------------------------------------------------------------------------------- /2.ChatModels/1_chatmodel_openai.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | model = ChatOpenAI(model='gpt-4', temperature=1.5, max_completion_tokens=10) 7 | 8 | result = model.invoke("Write a 5 line poem on cricket") 9 | 10 | print(result.content) -------------------------------------------------------------------------------- /3.EmbeddingModels/1_embedding_openai_query.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import OpenAIEmbeddings 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | embedding = OpenAIEmbeddings(model='text-embedding-3-large', dimensions=32) 7 | 8 | result = embedding.embed_query("Delhi is the capital of India") 9 | 10 | print(str(result)) -------------------------------------------------------------------------------- /2.ChatModels/4_chatmodel_hf_api.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | llm = HuggingFaceEndpoint( 7 | repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0", 8 | task="text-generation" 9 | ) 10 | 11 | model = ChatHuggingFace(llm=llm) 12 | 13 | result = model.invoke("What is the capital of India") 14 | 15 | print(result.content) -------------------------------------------------------------------------------- /3.EmbeddingModels/3_embedding_hf_local.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import HuggingFaceEmbeddings 2 | 3 | embedding = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') 4 | 5 | documents = [ 6 | "Delhi is the capital of India", 7 | "Kolkata is the capital of West Bengal", 8 | "Paris is the capital of France" 9 | ] 10 | 11 | vector = embedding.embed_documents(documents) 12 | 13 | print(str(vector)) -------------------------------------------------------------------------------- /3.EmbeddingModels/2_embedding_openai_docs.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import OpenAIEmbeddings 2 | from dotenv import load_dotenv 3 | 4 | load_dotenv() 5 | 6 | embedding = OpenAIEmbeddings(model='text-embedding-3-large', dimensions=32) 7 | 8 | documents = [ 9 | "Delhi is the capital of India", 10 | "Kolkata is the capital of West Bengal", 11 | "Paris is the capital of France" 12 | ] 13 | 14 | result = embedding.embed_documents(documents) 15 | 16 | print(str(result)) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # LangChain Core 2 | langchain 3 | langchain-core 4 | 5 | # OpenAI Integration 6 | langchain-openai 7 | openai 8 | 9 | # Anthropic Integration 10 | langchain-anthropic 11 | 12 | # Google Gemini (PaLM) Integration 13 | langchain-google-genai 14 | google-generativeai 15 | 16 | # Hugging Face Integration 17 | langchain-huggingface 18 | transformers 19 | huggingface-hub 20 | 21 | # Environment Variable Management 22 | python-dotenv 23 | 24 | # Machine Learning Utilities 25 | numpy 26 | scikit-learn 27 | -------------------------------------------------------------------------------- /2.ChatModels/5_chatmodel_hf_local.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline 2 | import os 3 | 4 | os.environ['HF_HOME'] = 'D:/huggingface_cache' 5 | 6 | llm = HuggingFacePipeline.from_model_id( 7 | model_id='TinyLlama/TinyLlama-1.1B-Chat-v1.0', 8 | task='text-generation', 9 | pipeline_kwargs=dict( 10 | temperature=0.5, 11 | max_new_tokens=100 12 | ) 13 | ) 14 | model = ChatHuggingFace(llm=llm) 15 | 16 | result = model.invoke("What is the capital of India") 17 | 18 | print(result.content) -------------------------------------------------------------------------------- /3.EmbeddingModels/4_document_similarity.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import OpenAIEmbeddings 2 | from dotenv import load_dotenv 3 | from sklearn.metrics.pairwise import cosine_similarity 4 | import numpy as np 5 | 6 | load_dotenv() 7 | 8 | embedding = OpenAIEmbeddings(model='text-embedding-3-large', dimensions=300) 9 | 10 | documents = [ 11 | "Virat Kohli is an Indian cricketer known for his aggressive batting and leadership.", 12 | "MS Dhoni is a former Indian captain famous for his calm demeanor and finishing skills.", 13 | "Sachin Tendulkar, also known as the 'God of Cricket', holds many batting records.", 14 | "Rohit Sharma is known for his elegant batting and record-breaking double centuries.", 15 | "Jasprit Bumrah is an Indian fast bowler known for his unorthodox action and yorkers." 16 | ] 17 | 18 | query = 'tell me about bumrah' 19 | 20 | doc_embeddings = embedding.embed_documents(documents) 21 | query_embedding = embedding.embed_query(query) 22 | 23 | scores = cosine_similarity([query_embedding], doc_embeddings)[0] 24 | 25 | index, score = sorted(list(enumerate(scores)),key=lambda x:x[1])[-1] 26 | 27 | print(query) 28 | print(documents[index]) 29 | print("similarity score is:", score) 30 | 31 | 32 | 33 | 34 | --------------------------------------------------------------------------------