### Install Pinecone Notebook Utilities Source: https://github.com/pinecone-io/examples/blob/master/learn/assistant/pinecone-assistant-getting-started.ipynb Installs the pinecone-notebooks package version 0.1.1, which includes utilities for notebook environments, such as the Pinecone Connect widget for authentication. ```python # Install Pinecone notebook utilities including the Pinecone Connect widget !pip install pinecone-notebooks==0.1.1 ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/search/image/image-retrieval-ebook/vision-transformers/vit.ipynb Installs necessary libraries for the project: datasets, transformers, and torch. This is the initial setup step. ```python !pip install datasets transformers torch ``` -------------------------------- ### Install LangChain Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/02-langchain-chains.ipynb Installs the necessary LangChain libraries and dependencies for the example, including specific versions for langchain, langchain-community, langchain-openai, and numexpr. ```python !pip install -qU \ langchain==0.3.25 \ langchain-community==0.3.25 \ langchain-openai==0.3.22 \ numexpr==2.11.0 ``` -------------------------------- ### Install Pinecone Client and Assistants Plugin Source: https://github.com/pinecone-io/examples/blob/master/learn/assistant/pinecone-assistant-getting-started.ipynb Installs the latest version of the Pinecone client library and the assistants plugin, which are essential for interacting with Pinecone's assistant features. ```python # Install pinecone client and assistants plugin !pip install --upgrade pinecone-client pinecone-plugin-assistant ``` -------------------------------- ### Install LangChain and OpenAI Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/xx-quick-agents-intro.ipynb Installs the necessary libraries for LangChain and OpenAI integration. This is a prerequisite for running the agent examples. ```python !pip install -qU langchain openai ``` -------------------------------- ### Query Pinecone Index (Example 1) Source: https://github.com/pinecone-io/examples/blob/master/integrations/hugging-face/endpoints/00-getting-started.ipynb Queries the Pinecone index with an example passage and prints the top 5 most similar results, including their metadata. ```python query = snli['premise'][0] print(f"Query: {query}") # encode with HF endpoints res = requests.post(endpoint, headers=headers, json={"inputs": query}) xq = res.json()['embeddings'] # query and return top 5 xc = index.query(vector=xq, top_k=5, include_metadata=True) # iterate through results and print text print("Answers:") for match in xc['matches']: print(match['metadata']['text']) ``` -------------------------------- ### Install Prerequisites Source: https://github.com/pinecone-io/examples/blob/master/integrations/groq/groq-llama-3-rag.ipynb Installs necessary Python libraries for the RAG example, including datasets, groq, semantic-router, and pinecone-client. ```Python !pip install -qU \ datasets==2.14.5 \ groq==0.8.0 \ "semantic-router[local]==0.0.45" \ pinecone-client==4.1.0 ``` -------------------------------- ### Query Pinecone Index (Example 2) Source: https://github.com/pinecone-io/examples/blob/master/integrations/hugging-face/endpoints/00-getting-started.ipynb Queries the Pinecone index with a second example passage and prints the top 5 most similar results, including their metadata. ```python query = snli['premise'][100] print(f"Query: {query}") # encode with HF endpoints res = requests.post(endpoint, headers=headers, json={"inputs": query}) xq = res.json()['embeddings'] # query and return top 5 xc = index.query(vector=xq, top_k=5, include_metadata=True) # iterate through results and print text print("Answers:") for match in xc['matches']: print(match['metadata']['text']) ``` -------------------------------- ### Install Prerequisites Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/better-rag/00-rerankers.ipynb Installs necessary Python libraries including datasets, openai, pinecone-client, and cohere for the example. ```python !pip install -qU \ datasets==2.14.5 \ openai==1.6.1 \ pinecone-client==3.1.0 \ cohere==4.27 ``` -------------------------------- ### Query Pinecone Index (Example 3) Source: https://github.com/pinecone-io/examples/blob/master/integrations/hugging-face/endpoints/00-getting-started.ipynb Queries the Pinecone index with a third example passage and prints the top 5 most similar results, including their metadata. ```python query = snli['premise'][200] print(f"Query: {query}") # encode with HF endpoints res = requests.post(endpoint, headers=headers, json={"inputs": query}) xq = res.json()['embeddings'] # query and return top 5 xc = index.query(vector=xq, top_k=5, include_metadata=True) # iterate through results and print text print("Answers:") for match in xc['matches']: print(match['metadata']['text']) ``` -------------------------------- ### OpenAI API Key Setup Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/02-langchain-chains.ipynb Sets up the OpenAI API key by retrieving it from environment variables or prompting the user for input. This is crucial for initializing and using OpenAI's language models within LangChain. ```python import os from getpass import getpass os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") \ or getpass("Enter your OpenAI API key: ") ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/search/semantic-search/openai-embed-v3/openai-embed-v3.ipynb Installs necessary Python packages for the project, including datasets, openai, and pinecone-client. ```python !pip install -qU \ datasets==2.14.6 \ openai==1.2.2 \ pinecone-client==3.1.0 ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/search/semantic-search/2311-embedding-examples/retrieval-perf-bge-flag-fp16.ipynb Installs necessary Python packages for the examples, including datasets and FlagEmbedding. ```bash !pip install -qU \ datasets==2.14.6 \ FlagEmbedding==1.1.5 ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/search/semantic-search/2311-embedding-examples/retrieval-perf-bge-transformers.ipynb Installs the necessary Python libraries: datasets, transformers, torch, tqdm, and numpy. This is a prerequisite for running the subsequent code examples. ```shell !pip install -qU \ datasets \ transformers ``` -------------------------------- ### Install Pinecone Client Source: https://github.com/pinecone-io/examples/blob/master/integrations/haystack/nhs-search/src/requirements.txt Installs the official Pinecone client library for Python. ```bash pip install pinecone-client ``` -------------------------------- ### Install BERTopic Source: https://github.com/pinecone-io/examples/blob/master/learn/experimental/algos-and-libraries/bertopic/01_topic_modeling.ipynb Installs the BERTopic library using pip. This is the first step before using any BERTopic functionalities. ```python !pip install bertopic ``` -------------------------------- ### Install Pinecone Libraries Source: https://github.com/pinecone-io/examples/blob/master/learn/assistant/yorkshire-assistant.ipynb Installs the necessary Pinecone client and assistant plugin libraries using pip. ```bash !pip install -qU \ pinecone==5.4.2 \ pinecone-plugin-assistant==1.1.0 ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/search/semantic-search/2311-embedding-examples/retrieval-perf-cohere.ipynb Installs the necessary Python libraries: datasets and cohere. The '-qU' flags ensure quiet installation and upgrade. ```shell !pip install -qU \ datasets==2.14.6 \ cohere==4.34 ``` -------------------------------- ### Install Prerequisites Source: https://github.com/pinecone-io/examples/blob/master/learn/search/semantic-search/spotify-podcast-search/spotify-podcast-search.ipynb Installs necessary Python libraries including kaggle, sentence-transformers, pinecone-client, and tqdm for data handling, embeddings, and progress tracking. ```python import kaggle ``` -------------------------------- ### Install LangChain and Integrations Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/00-langchain-intro.ipynb Installs the necessary LangChain libraries, including specific versions for Hugging Face and OpenAI integrations. This is a prerequisite for using the examples. ```python !pip install -qU \ langchain==0.3.25 \ langchain-huggingface==0.3.0 \ langchain-openai==0.3.22 ``` -------------------------------- ### Install Pinecone SDK and Assistant Plugin Source: https://github.com/pinecone-io/examples/blob/master/docs/assistant-quickstart.ipynb Installs the necessary Pinecone Python SDK and the assistant plugin using pip. ```python !pip install pinecone pinecone-plugin-assistant ``` -------------------------------- ### Pinecone Client Initialization and Basic Operations Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/agentic-rag/agentic-rag-with-claude.ipynb Demonstrates how to initialize the Pinecone client, create an index, upsert vectors, and query the index. This is a fundamental example for getting started with Pinecone. ```Python from pinecone import Pinecone, ServerlessSpec # Initialize Pinecone api_key = "YOUR_API_KEY" cloud = "aws" region = "us-east-1" pc = Pinecone(api_key=api_key) # Create an index (if it doesn't exist) index_name = "my-index" if index_name not in pc.list_indexes().names: pc.create_index( index_name, dimension=8, # Replace with your vector dimension metric="cosine", # Replace with your metric spec=ServerlessSpec(cloud=cloud, region=region) ) # Connect to the index index = pc.Index(index_name) # Upsert vectors vectors_to_upsert = [ ("vec1", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]), ("vec2", [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]) ] index.upsert(vectors=vectors_to_upsert) # Query the index query_vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] results = index.query(vector=query_vector, top_k=2, include_values=True) print(results) ``` -------------------------------- ### Pinecone Initialization and Basic Operations Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/agentic-rag/agentic-rag-with-claude.ipynb Demonstrates how to initialize the Pinecone client, create an index, upsert data, and perform a basic query. This is a fundamental example for getting started with Pinecone. ```Python from pinecone import Pinecone, ServerlessSpec # Initialize Pinecone api_key = "YOUR_API_KEY" # For serverless # pc = Pinecone(api_key=api_key, environment="YOUR_ENVIRONMENT") # For pod-based pc = Pinecone(api_key=api_key) index_name = "my-index" # Create an index if it doesn't exist if index_name not in pc.list_indexes().names: pc.create_index( index_name, dimension=8, # Replace with your model's dimension metric="cosine", # Or "euclidean", "dotproduct" spec=ServerlessSpec(cloud="aws", region="us-east-1") # Or PodSpec for pod-based ) index = pc.Index(index_name) # Upsert data index.upsert( vectors=[ ("vec1", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]), ("vec2", [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]) ] ) # Query the index query_results = index.query( vector=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], top_k=2, include_values=True ) print(query_results) ``` -------------------------------- ### Initialize Calculator Tool with LLMMathChain Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/xx-quick-agents-intro.ipynb Creates a 'Calculator' tool using LangChain's LLMMathChain. This tool allows the agent to perform mathematical calculations by leveraging the LLM. ```python from langchain.chains import LLMMathChain from langchain.agents import Tool llm_math = LLMMathChain(llm=llm) # initialize the math tool math_tool = Tool( name='Calculator', func=llm_math.run, description='Useful for when you need to answer questions about math.' ) # when giving tools to LLM, we must pass as list of tools tools = [math_tool] ``` -------------------------------- ### Pinecone Client Initialization and Basic Operations Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/agentic-rag/agentic-rag-with-claude.ipynb Demonstrates how to initialize the Pinecone client, create an index, upsert vectors, and query the index. This is a fundamental example for getting started with Pinecone. ```python from pinecone import Pinecone, ServerlessSpec import os # Initialize Pinecone api_key = os.environ.get("PINECONE_API_KEY") if not api_key: raise ValueError("PINECONE_API_KEY environment variable not set.") pc = Pinecone(api_key=api_key) # Define index name and dimension index_name = "my-index" vector_dimension = 8 # Check if index exists, create if not if index_name not in pc.list_indexes().names: pc.create_index( index_name, dimension=vector_dimension, metric="cosine", # or "euclidean", "dotproduct" spec=ServerlessSpec(cloud="aws", region="us-west-2") ) # Connect to the index index = pc.Index(index_name) # Upsert vectors vectors_to_upsert = [ ("vec1", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]), ("vec2", [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]) ] index.upsert(vectors=vectors_to_upsert) # Query the index query_vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] query_results = index.query(vector=query_vector, top_k=1, include_values=True) print(query_results) # Delete the index (optional) # pc.delete_index(index_name) ``` -------------------------------- ### Initialize General Knowledge Tool Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/xx-quick-agents-intro.ipynb Creates a 'Language Model' tool for general purpose queries. This tool uses a simple LLMChain with a prompt template to pass queries directly to the LLM. ```python from langchain.prompts import PromptTemplate from langchain.chains import LLMChain prompt = PromptTemplate( input_variables=["query"], template="{query}" ) llm_chain = LLMChain(llm=llm, prompt=prompt) # initialize the LLM tool llm_tool = Tool( name='Language Model', func=llm_chain.run, description='use this tool for general purpose queries and logic' ) ``` -------------------------------- ### Pinecone Client Initialization and Basic Operations Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/agentic-rag/agentic-rag-with-claude.ipynb Demonstrates how to initialize the Pinecone client, create an index, upsert vectors, and query the index. This is a fundamental example for getting started with Pinecone. ```python from pinecone import Pinecone, ServerlessSpec import os # Initialize connection to Pinecone api_key = os.environ.get("PINECONE_API_KEY") if api_key is None: raise ValueError("PINECONE_API_KEY environment variable not set.") pc = Pinecone(api_key=api_key) # Define index name and configuration index_name = "quickstart" # Check if index exists, if not create it if index_name not in pc.list_indexes().names: pc.create_index( name=index_name, dimension=8, # Example dimension metric="cosine", # Example metric spec=ServerlessSpec(cloud="aws", region="us-west-2") # Example spec ) # Connect to the index index = pc.Index(index_name) # Upsert data index.upsert( vectors=[ ("vec1", [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]), ("vec2", [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]), ("vec3", [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]), ("vec4", [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4]), ] ) # Query the index results = index.query( vector=[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], top_k=2, include_values=True ) print(results) # Delete the index (optional) # pc.delete_index(index_name) ``` -------------------------------- ### Install Pinecone Libraries Source: https://github.com/pinecone-io/examples/blob/master/docs/assets/how-to-create-pinecone-datasets.ipynb Installs the pinecone-client and pinecone-datasets libraries, essential for interacting with Pinecone and managing datasets. ```python !pip install -qU \ pinecone-client==2.2.2 \ pinecone-datasets==0.6.0 ``` -------------------------------- ### Pinecone Client Initialization and Basic Operations (Python) Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/agentic-rag/agentic-rag-with-claude.ipynb Demonstrates how to initialize the Pinecone client, create an index, upsert data, and perform a basic query. This is a fundamental example for getting started with Pinecone. ```Python from pinecone import Pinecone, ServerlessSpec import time # Initialize Pinecone # Replace with your actual API key and environment api_key = "YOUR_API_KEY" # Initialize Pinecone connection # For serverless, specify cloud and region # For pod-based, specify environment pc = Pinecone(api_key=api_key) # Define index name index_name = "my-example-index" # Check if index exists, create if not if index_name not in pc.list_indexes().names: print(f"Creating index: {index_name}") pc.create_index( index_name, dimension=8, # Replace with your vector dimension metric="cosine", # Or "euclidean", "dotproduct" spec=ServerlessSpec(cloud="aws", region="us-east-1") # Or PodSpec for pod-based ) # Wait for index to be ready while not pc.describe_index(index_name).status['ready']: time.sleep(1) # Connect to the index index = pc.Index(index_name) # Upsert data # Vectors should be a list of tuples: (id, vector_values) vectors_to_upsert = [ ("vec1", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]), ("vec2", [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]) ] index.upsert(vectors=vectors_to_upsert) print("Upserted vectors.") # Wait for upsert to complete time.sleep(5) # Query the index query_vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] query_results = index.query(vector=query_vector, top_k=1, include_values=True) print("Query results:") print(query_results) # Delete the index (optional) # pc.delete_index(index_name) # print(f"Deleted index: {index_name}") ``` -------------------------------- ### Extract Answer Text using Indices Source: https://github.com/pinecone-io/examples/blob/master/integrations/haystack/nhs-search/notebooks/01_test_pipeline.ipynb Uses the previously obtained start and end indices to slice the context and get the exact answer string. ```python prediction['answers'][0].context[start:end] ``` -------------------------------- ### Load Calculator Tool using load_tools Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/xx-quick-agents-intro.ipynb An alternative method to initialize the 'llm-math' tool using LangChain's `load_tools` function. This simplifies the tool initialization process. ```python from langchain.agents import load_tools tools = load_tools( ['llm-math'], llm=llm ) ``` -------------------------------- ### Pinecone Initialization and Basic Operations Source: https://github.com/pinecone-io/examples/blob/master/docs/pinecone-reranker.ipynb Demonstrates how to initialize the Pinecone client, create an index, upsert vectors, and perform a similarity search. This is a fundamental example for getting started with Pinecone. ```python from pinecone import Pinecone, ServerlessSpec # Initialize Pinecone api_key = "YOUR_API_KEY" cloud = "aws" region = "us-east-1" pc = Pinecone(api_key=api_key) # Create an index (if it doesn't exist) index_name = "my-index" if index_name not in pc.list_indexes().names: pc.create_index( index_name, dimension=8, # Replace with your vector dimension metric="cosine", # Or "euclidean", "dotproduct" spec=ServerlessSpec(cloud=cloud, region=region) ) # Connect to the index index = pc.Index(index_name) # Upsert vectors vectors_to_upsert = [ ("vec1", [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]), ("vec2", [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]) ] index.upsert(vectors=vectors_to_upsert) # Perform a similarity search query_vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] results = index.query(vector=query_vector, top_k=1, include_values=True) print(results) ``` -------------------------------- ### Create Pinecone Assistant Source: https://github.com/pinecone-io/examples/blob/master/learn/assistant/pinecone-assistant-getting-started.ipynb Initializes the Pinecone client using an API key (either from environment or userdata) and then creates a new assistant with a specified name and metadata. Includes a timeout for the creation process. ```python from google.colab import userdata from pinecone import Pinecone pc = Pinecone(api_key=os.environ.get('PINECONE_API_KEY') or userdata.get('PINECONE_API_KEY')) assistant_name = 'HelloPineconeAssistant' metadata = {"author": "Jane Doe", "version": "1.0"} assistant = pc.assistant.create_assistant( assistant_name=assistant_name, metadata=metadata, timeout=30 # Wait 30 seconds for assistant creation to complete. ) ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/llama-index/llama-index-research-agent.ipynb Installs necessary Python packages for the project, including datasets, llama-index, OpenAI integration, semantic-router with Pinecone support, and search utilities. ```bash !pip install -qU \ datasets==2.19.1 \ llama-index-core==0.11.9 \ llama-index-llms-openai \ llama-index-utils-workflow==0.2.1 \ serpapi==0.1.5 \ google-search-results==2.4.2 \ semantic-router[pinecone]==0.0.65 ``` -------------------------------- ### Install Pinecone SDK and Notebooks Source: https://github.com/pinecone-io/examples/blob/master/docs/pinecone-quickstart.ipynb Installs the Pinecone Python SDK (version 7.0.1) and the pinecone-notebooks library, which is used for authentication in Colab environments. ```python !pip install -qU \ pinecone==7.0.1 \ pinecone-notebooks ``` -------------------------------- ### Authenticate with Pinecone Source: https://github.com/pinecone-io/examples/blob/master/docs/assistant-quickstart.ipynb Installs the pinecone-notebooks package and uses the Authenticate utility to set up API key access for Pinecone. ```python # install dependencies !pip install -qU pinecone-notebooks from pinecone_notebooks.colab import Authenticate Authenticate() ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/03-langchain-conversational-memory.ipynb Installs necessary LangChain and OpenAI libraries for the example. Ensures the correct versions are used for compatibility. ```bash !pip install -qU \ langchain==0.3.25 \ langchain-community==0.3.25 \ langchain-openai==0.3.22 \ tiktoken==0.9.0 ``` -------------------------------- ### Install Prerequisites Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/06-langchain-agents.ipynb Installs necessary Python libraries for LangChain, OpenAI integration, experimental features, search, Wikipedia, and SQLAlchemy. ```python !pip install -qU \ langchain==0.3.25 \ langchain-openai==0.3.22 \ langchain-experimental==0.3.4 \ numexpr==2.11.0 \ google-search-results==2.4.2 \ wikipedia==1.4.0 \ sqlalchemy==2.0.41 ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/chatbots/nemo-guardrails/02a-actions-weather-api.ipynb Installs the necessary Guardrails and OpenAI libraries using pip. This is a prerequisite for running the example. ```python !pip install -qU \ nemoguardrails==0.4.0 \ openai==0.27.8 ``` -------------------------------- ### Install Dependencies Source: https://github.com/pinecone-io/examples/blob/master/learn/generation/chatbots/conversational-agents/langchain-lex-agent.ipynb Installs necessary Python libraries including datasets, pod-gpt, pinecone-client, langchain, openai, and tqdm for the project. ```Python !pip install -qU datasets pod-gpt pinecone-client[grpc] langchain openai tqdm ``` -------------------------------- ### Install Pinecone Client and Datasets Source: https://github.com/pinecone-io/examples/blob/master/docs/it-threat-detection.ipynb Installs the necessary Pinecone client libraries and dataset utilities for the project. Ensures the correct versions are used for compatibility. ```python !pip install -qU \ pinecone-client==3.1.0 \ pinecone-datasets==0.7.0 \ pinecone-notebooks==0.1.1 ```