File size: 4,365 Bytes
8c3e214
ed62434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c3e214
 
 
 
ed62434
 
 
 
 
8c3e214
ed62434
 
 
8c3e214
 
 
 
 
ed62434
 
 
 
 
 
 
 
8c3e214
ed62434
8c3e214
ed62434
 
8c3e214
ed62434
 
 
8c3e214
ed62434
 
 
 
8c3e214
ed62434
c29786a
 
8c3e214
 
ed62434
 
 
7ee54e3
ed62434
1449087
 
 
ed62434
 
 
 
 
 
8c3e214
 
 
 
8af8d67
8c3e214
 
 
 
 
 
 
 
 
 
 
ed62434
 
1449087
ed62434
 
 
 
 
 
 
 
 
8c3e214
 
 
ed62434
8c3e214
 
 
 
ed62434
 
8c3e214
ed62434
 
 
 
 
 
8c3e214
ed62434
 
 
 
8c3e214
 
8282916
 
 
ea540b6
8282916
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from fastapi import FastAPI
import os


import phoenix as px
from phoenix.trace.langchain import OpenInferenceTracer, LangChainInstrumentor


from langchain.embeddings import HuggingFaceEmbeddings #for using HugginFace models
from langchain.chains.question_answering import load_qa_chain
from langchain import HuggingFaceHub

from langchain.chains import RetrievalQA
from langchain.callbacks import StdOutCallbackHandler

#from langchain.retrievers import KNNRetriever
from langchain.storage import LocalFileStore
from langchain.embeddings import CacheBackedEmbeddings
from langchain.vectorstores import FAISS


from langchain.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

# from langchain import HuggingFaceHub
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from txtai.embeddings import Embeddings
# from txtai.pipeline import Extractor

# import pandas as pd
# import sqlite3
# import os

# NOTE - we configure docs_url to serve the interactive Docs at the root path
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
app = FastAPI(docs_url="/")

#phoenix setup
session = px.launch_app()
# If no exporter is specified, the tracer will export to the locally running Phoenix server
tracer = OpenInferenceTracer()
# If no tracer is specified, a tracer is constructed for you
LangChainInstrumentor(tracer).instrument()
print(session.url)


os.environ["HUGGINGFACEHUB_API_TOKEN"] = "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"

# embedding cache
store = LocalFileStore("./cache/")

# define embedder
core_embeddings_model  = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
embedder = CacheBackedEmbeddings.from_bytes_store(core_embeddings_model, store)

# define llm
llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1, "max_length":1000000})
#llm=HuggingFaceHub(repo_id="gpt2", model_kwargs={"temperature":1, "max_length":1000000})
handler =  StdOutCallbackHandler()

# set global variable
vectorstore = None
retriever = None


def initialize_vectorstore():

    webpage_loader = WebBaseLoader("https://www.tredence.com/case-studies/tredence-helped-a-global-retailer-providing-holistic-campaign-analytics-by-using-the-power-of-gcp").load()
    webpage_chunks = _text_splitter(webpage_loader)

    global vectorstore
    global retriever

    # store embeddings in vector store
    vectorstore = FAISS.from_documents(webpage_chunks, embedder)
    print("vector store initialized with sample doc")

    # instantiate a retriever
    retriever = vectorstore.as_retriever()


def _text_splitter(doc):
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=50,
        length_function=len,
    )
    return text_splitter.transform_documents(doc)

def _load_docs(path: str):
    load_doc = WebBaseLoader(path).load()
    doc = _text_splitter(load_doc)
    return doc


@app.get("/index/")
def get_domain_file_path(file_path: str):
    print("file_path  " ,file_path)
     
    webpage_loader = _load_docs(file_path)

    webpage_chunks = _text_splitter(webpage_loader)

    # store embeddings in vector store
    vectorstore.add_documents(webpage_chunks)

    return "document loaded to vector store successfully!!"


def _prompt(question):
    return f"""Answer following question using only the context below. Say 'Could not find answer with provided context' when question can't be answered.
            Question: {question}
            Context: """


@app.get("/rag")
def rag( question: str):   

    chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    callbacks=[handler],
    return_source_documents=True
    )

    #response = chain("how tredence brought good insight?")
    response = chain(_prompt(question))
 
    return {"question": question, "answer": response['result']}


initialize_vectorstore()



#import getpass
from pyngrok import ngrok, conf
#print("Enter your authtoken, which can be copied from https://dashboard.ngrok.com/auth")
conf.get_default().auth_token = "2WJNWULs5bCOyJnV24WQYJEKod3_YQUbM5EGCp8sgE4aQvzi"
port = 37689
# Open a ngrok tunnel to the HTTP server
public_url = ngrok.connect(port).public_url
print(" * ngrok tunnel \"{}\" -> \"http://127.0.0.1:{}\"".format(public_url, port))