-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoffee_rag_openai.py
45 lines (34 loc) · 1.29 KB
/
coffee_rag_openai.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
import os
from langchain import hub
from langchain.schema import StrOutputParser
from langchain_openai import ChatOpenAI
from langchain_community.vectorstores import faiss
from langchain_core.runnables import RunnablePassthrough
from credential_utilties.environment import setEnvironmentVariables
from embeddings.embeddings_wrapper import HuggingFaceEmbeddings
vectorstore = None
prompt = None
def init():
setEnvironmentVariables()
global vectorstore
global prompt
embeddings = HuggingFaceEmbeddings(model_id = "multi-qa-mpnet-base-dot-v1")
model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR", default=""),"coffee_embeddings")
vectorstore = faiss.FAISS.load_local(model_path, embeddings, allow_dangerous_deserialization=True)
prompt = hub.pull("rlm/rag-prompt")
def run(query):
retriever = vectorstore.as_retriever()
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()
)
ragtext = rag_chain.invoke(query)
return [ragtext]
if __name__ == '__main__':
init()
print(run('what is latte'))