-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from langchain_community.document_loaders import WebBaseLoader | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
from langchain_openai import ChatOpenAI,OpenAIEmbeddings | ||
from langchain_community.vectorstores import MongoDBAtlasVectorSearch | ||
from langchain.retrievers import ContextualCompressionRetriever | ||
from langchain.retrievers.document_compressors import LLMChainExtractor | ||
from pymongo import MongoClient | ||
import urllib | ||
|
||
llm=ChatOpenAI(model="gpt-4",temperature=0) | ||
compressor=LLMChainExtractor.from_llm(llm) | ||
embeddings=OpenAIEmbeddings() | ||
|
||
client=MongoClient() | ||
username="ronidas" | ||
pwd="pLy0SzzYX3zclsaq" | ||
|
||
uri = "mongodb+srv://"+urllib.parse.quote_plus(username)+":"+urllib.parse.quote_plus(pwd)+"@cluster0.lymvb.mongodb.net/?retryWrites=true&w=majority" | ||
# Create a new client and connect to the server | ||
client = MongoClient(uri) | ||
# Send a ping to confirm a successful connection | ||
try: | ||
client.admin.command('ping') | ||
collection=client["llm_tutorial"]["langchain"] | ||
vs=MongoDBAtlasVectorSearch(collection,embeddings,index_name="tutorial_35") | ||
docs=vs.max_marginal_relevance_search("what was the impact of covid on the world economy",k=1) | ||
print(docs[0].page_content) | ||
print("========================================================") | ||
except Exception as e: | ||
print(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from langchain_community.document_loaders import WebBaseLoader | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
from langchain_openai import ChatOpenAI,OpenAIEmbeddings | ||
from langchain_community.vectorstores import MongoDBAtlasVectorSearch | ||
from pymongo import MongoClient | ||
import urllib | ||
|
||
url="https://en.wikipedia.org/wiki/COVID-19" | ||
loader=WebBaseLoader([url]) | ||
doc=loader.load() | ||
|
||
splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=0,separators=["\n\n","\n","(?<=\.)"," "],length_function=len) | ||
docs=splitter.split_documents(doc) | ||
embeddings=OpenAIEmbeddings() | ||
|
||
client=MongoClient() | ||
username="ronidas" | ||
pwd="pLy0SzzYX3zclsaq" | ||
|
||
uri = "mongodb+srv://"+urllib.parse.quote_plus(username)+":"+urllib.parse.quote_plus(pwd)+"@cluster0.lymvb.mongodb.net/?retryWrites=true&w=majority" | ||
# Create a new client and connect to the server | ||
client = MongoClient(uri) | ||
# Send a ping to confirm a successful connection | ||
try: | ||
client.admin.command('ping') | ||
collection=client["llm_tutorial"]["langchain"] | ||
docsearch=MongoDBAtlasVectorSearch.from_documents(docs,embeddings,collection=collection,index_name="tutorial_35") | ||
print("documents are stored into mongodb vector store successfully") | ||
|
||
|
||
except Exception as e: | ||
print(e) |
Binary file not shown.