-
Notifications
You must be signed in to change notification settings - Fork 1
/
openai-rag-arxiv.py
54 lines (37 loc) · 1.44 KB
/
openai-rag-arxiv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from langchain import hub
from langchain.document_loaders import ArxivLoader
from langchain.retrievers import ArxivRetriever
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema import StrOutputParser
import os
import getpass
os.environ['OPENAI_API_KEY'] = getpass.getpass("Enter your openai api key: ")
# cleanup previous
# vectorstore.delete_collection()
docs = ArxivLoader(query="Antibiotic design using deep learning", load_max_docs=10).load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
retriever = vectorstore.as_retriever()
prompt = hub.pull("rlm/rag-prompt")
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
resp = rag_chain.invoke(
"""
Can you provide a summary of the current state of applying
deep learning to the discovery of new antibiotics?
"""
)
print(f'{resp.format()}')
# vectorstore.delete_collection()