CoolFace
Apppublic

mohamedamgad2002/Simple-RAG

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
sample_ingestion.py120 linesDownload Raw Back to src
1# import basics
2import os
3import time
4from dotenv import load_dotenv
5
6# import pinecone
7from pinecone import Pinecone, ServerlessSpec
8
9# import langchain
10from langchain_pinecone import PineconeVectorStore
11from langchain_google_genai import GoogleGenerativeAIEmbeddings
12from langchain_core.documents import Document
13
14load_dotenv() 
15
16pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
17
18# initialize pinecone database
19index_name = "sample-index"  # change if desired
20
21# check whether index exists, and create if not
22existing_indexes = [index_info["name"] for index_info in pc.list_indexes()]
23
24if index_name not in existing_indexes:
25    pc.create_index(
26        name=index_name,
27        dimension=3072,
28        metric="cosine",
29        spec=ServerlessSpec(cloud="aws", region="us-east-1"),
30    )
31    while not pc.describe_index(index_name).status["ready"]:
32        time.sleep(1)
33
34index = pc.Index(index_name)
35
36# initialize embeddings model + vector store
37embeddings = GoogleGenerativeAIEmbeddings(model="models/gemini-embedding-001")
38
39vector_store = PineconeVectorStore(index=index, embedding=embeddings)
40
41
42# adding the documents
43
44document_1 = Document(
45    page_content="I had chocalate chip pancakes and scrambled eggs for breakfast this morning.",
46    metadata={"source": "tweet"},
47)
48
49document_2 = Document(
50    page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.",
51    metadata={"source": "news"},
52)
53
54document_3 = Document(
55    page_content="Building an exciting new project with LangChain - come check it out!",
56    metadata={"source": "tweet"},
57)
58
59document_4 = Document(
60    page_content="Robbers broke into the city bank and stole $1 million in cash.",
61    metadata={"source": "news"},
62)
63
64document_5 = Document(
65    page_content="Wow! That was an amazing movie. I can't wait to see it again.",
66    metadata={"source": "tweet"},
67)
68
69document_6 = Document(
70    page_content="Is the new iPhone worth the price? Read this review to find out.",
71    metadata={"source": "website"},
72)
73
74document_7 = Document(
75    page_content="The top 10 soccer players in the world right now.",
76    metadata={"source": "website"},
77)
78
79document_8 = Document(
80    page_content="LangGraph is the best framework for building stateful, agentic applications!",
81    metadata={"source": "tweet"},
82)
83
84document_9 = Document(
85    page_content="The stock market is down 500 points today due to fears of a recession.",
86    metadata={"source": "news"},
87)
88
89document_10 = Document(
90    page_content="I have a bad feeling I am going to get deleted :(",
91    metadata={"source": "tweet"},
92)
93
94documents = [
95    document_1,
96    document_2,
97    document_3,
98    document_4,
99    document_5,
100    document_6,
101    document_7,
102    document_8,
103    document_9,
104    document_10,
105]
106
107# generate unique id's
108
109i = 0
110uuids = []
111
112while i < len(documents):
113
114    i += 1
115
116    uuids.append(f"id{i}")
117
118# add to database
119
120vector_store.add_documents(documents=documents, ids=uuids)