File size: 8,288 Bytes
15add43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fa05a4
15add43
 
 
defdc7d
 
 
 
 
 
 
15add43
 
 
 
defdc7d
de2352e
15add43
defdc7d
15add43
 
79b0643
15add43
 
defdc7d
8c1c633
 
a7175e3
8c1c633
15add43
a140873
 
 
 
 
 
 
 
8c1c633
defdc7d
15add43
defdc7d
 
15add43
defdc7d
15add43
defdc7d
15add43
 
defdc7d
15add43
 
defdc7d
 
 
 
15add43
 
defdc7d
 
 
 
 
 
 
15add43
defdc7d
15add43
defdc7d
 
 
 
 
 
 
 
 
 
15add43
defdc7d
15add43
defdc7d
15add43
defdc7d
 
 
 
15add43
defdc7d
15add43
defdc7d
 
15add43
defdc7d
 
 
 
15add43
defdc7d
 
 
 
 
 
 
15add43
defdc7d
 
15add43
defdc7d
 
15add43
defdc7d
 
15add43
defdc7d
15add43
 
 
defdc7d
 
 
 
 
 
 
 
 
 
 
 
add2b93
defdc7d
 
 
 
add2b93
defdc7d
 
 
 
a7175e3
 
defdc7d
 
 
 
15add43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de2352e
15add43
 
 
 
de2352e
15add43
 
 
cfecdf9
defdc7d
 
cfecdf9
defdc7d
 
 
 
 
 
15add43
 
defdc7d
15add43
de2352e
 
 
 
defdc7d
 
a7175e3
de2352e
 
 
15add43
de2352e
15add43
 
 
defdc7d
15add43
 
 
 
a7175e3
15add43
defdc7d
15add43
 
 
 
79168c4
15add43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import gradio as gr
import numpy as np
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import LLMChain
from langchain import PromptTemplate
import re
import pandas as pd
from langchain.vectorstores import FAISS
import requests
from typing import List
from langchain.schema import (
    SystemMessage,
    HumanMessage,
    AIMessage
)
import os
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chat_models import ChatOpenAI

from fuzzywuzzy import process

from langchain.llms.base import LLM
from typing import Optional, List, Mapping, Any
import anthropic

import ast

CHARACTER_CUT_OFF = 20000
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)

embeddings = HuggingFaceEmbeddings(model_name = "msmarco-distilbert-base-dot-prod-v3")
db = FAISS.load_local('retrieval_db', embeddings)

mp_docs = {}
llm = ChatOpenAI(
            temperature=0,
            model='gpt-3.5-turbo-16k'
        )

# List of product names
products = []
for id in db.index_to_docstore_id.values():
  prod_name = db.docstore.search(id).metadata['Product Name']
  products.append(prod_name)

    
def add_text(history, text):

    print(history)
    history = history + [(text, None)]

    return history, ""
    
# Claude Custom Class
class ClaudeLLM(LLM):

    @property
    def _llm_type(self) -> str:

        return "custom"

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:


        client = anthropic.Client(os.environ['ANTHROPIC_KEY'])


        # How about the formatted prompt?
        prompt_formatted = (
        f"{anthropic.HUMAN_PROMPT}{prompt}\n{anthropic.AI_PROMPT}"
        )


        response = client.completion(
        prompt=prompt_formatted,
        stop_sequences=[anthropic.HUMAN_PROMPT],
        model="claude-instant-v1-100k",
        max_tokens_to_sample=100000,
        temperature=0.3,
         )

        return response["completion"]

        # return Bard().get_answer(prompt)['content']
        # response = requests.post(
        #     "http://127.0.0.1:8000/prompt",
        #     json={
        #         "prompt": prompt,
        #         "temperature": 0,
        #         "max_new_tokens": 256,
        #         "stop": stop + ["Observation:"]
        #     }
        # )

        # response.raise_for_status()

        # return response.json()["response"]

    @property
    def _identifying_params(self) -> Mapping[str, Any]:
        """Get the identifying parameters."""
        return {

        }

def identify_products(query):
  llm = ClaudeLLM()

  prompt = PromptTemplate(
        input_variables=["query",],
        template="""
        Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

        Instruction:
        Extract Product Names from the following query in a list like the following example.
        
        Example:
        Query: What is the difference between A71 and A81 products?
        Response: ['A71', 'A81']
        End of Example.

        Query: {query}
        Response: 

        """,
          )

  chain = LLMChain(llm=llm, prompt=prompt)
  response = chain.run(query=query,)

  return ast.literal_eval(response.strip())



def correct_product_name(user_input, products, threshold=50):
    best_match = process.extractOne(user_input, products)
    
    # If the match percentage is above the threshold, return the match, otherwise return "No suggestions found"
    if best_match[1] >= threshold:
        return best_match[0]
    else:
        return 0
def retrieve_products(query):
  idx_prods = identify_products(query)

  correct_prods = []
  print(idx_prods)
  for prod in idx_prods:
    adj_prod = correct_product_name(prod, products)
    if (adj_prod):
      correct_prods.append(adj_prod)
  print(correct_prods)
  if correct_prods:
    prods_mp = {}
    for prod in correct_prods:
    # Retrieve from db, each product 4-chunks
      docs = db.similarity_search(query = "", filter = {'Product Name': prod}, k = 4, fetch_k = 49190)
      prods_mp[prod] = "\n".join([doc.page_content for doc in docs])

    return prods_mp
  return False
  

# How to access the file? Where is it saved?
def add_file(history, files):
    history = []
    files = files[0]
    docs = []
    for file in files:

      loader = UnstructuredPDFLoader(file.name)
      text = loader.load()
      # pdf_content = pdf2text(file.name)

      docs += text_splitter.split_documents(text)
      # print(docs[0])

    global db
    if(type(db) != str):
      # local_db =

      docs += list(db.docstore._dict.values())

    print(docs)
    history = history + [(f"{len(files)} PDF(s) Uploaded", None),]


    db = FAISS.from_documents(docs, embeddings)
    print(f"History in add file: {history}")
    # print(db.docstore())
    print(type(history), type(history[0]))
    return ([history,],)

def qa_retrieve(chatlog,):

    print(f"Chatlog qa: {chatlog}")
    query = chatlog[-1][0]
    docs = ""

    global db
    print(db)

    global mp_docs
    mp_products = retrieve_products(query)
    if not(mp_products):

        if mp_docs:
            mp_products = mp_docs
    else:
        mp_docs = mp_products
    mp_products = [f"Product: {product}\n Information: {information}" for product, information in mp_products.items()]
    print(f"DOCS RETRIEVED: {mp_docs.values}")
    
    prompt = PromptTemplate(
            input_variables=["query", "products"],
            template="""
            You are a Physics specialist that that can identify main products and their characteristics from the products you have been given.
         You will help the user with questions related to different electronic products, retrieving advanced physics equations, tables and many other valuable information between products.
          Once you have reviewed the documents, you will be able to offer detailed analysis and guidance based on the user's question.

            Answer the following question: {query}
            Use the following documents for each product:
            {products}

            Only use the factual information from the documents to answer the question. You are very careful in the products or theory name and will not invent or imagine names.
            Make sure to always answer the question with the same language the question is in. If it's in chinese make sure to answer in chinese. If it's in english answer in english.
            If you feel like you don't have enough information to answer the question, say "I don't know".

    
            """,
        )
        
    
    # llm = BardLLM()
    chain = LLMChain(llm=llm, prompt = prompt)
    
    response = chain.run(query=query, products="\n".join(mp_products))

    
    chatlog[-1][1] = response
    return chatlog
  
def flush():

  return None

with gr.Blocks() as demo:
    chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)

    with gr.Row():
        with gr.Column(scale=0.65):
            txt = gr.components.Textbox(
             placeholder="Ask me anything",show_label=False
        )

        with gr.Column(scale=0.15, min_width=0):
            btn = gr.UploadButton("📁", file_types=["text"], file_count = 'multiple')

    # with gr.Row():

    #   with gr.Column(scale=0.85):

    #     url = gr.components.Textbox(

    #           label="Website URLs",
    #           placeholder="https://www.example.org/ https://www.example.com/",
    #       )
      
        with gr.Column(scale=0.15, min_width = 0):
            send_btn = gr.Button("📨")

    with gr.Row():
      with gr.Column():
        clear = gr.Button("Clear")
    pdf_content = gr.Textbox("", visible = False)
    txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
        qa_retrieve, [chatbot], chatbot
    ).then(lambda : (None), outputs = [ pdf_content])
    btn.upload(add_file, [chatbot, btn], [chatbot,], batch = True).then(qa_retrieve, [chatbot], chatbot)

    send_btn.click(add_text, [chatbot, txt, ], [chatbot, txt]).then(
        qa_retrieve, [chatbot, ], chatbot).then(lambda :  None, outputs = [ pdf_content])

    clear.click(flush, None, outputs = chatbot, queue=False)

demo.queue(concurrency_count = 4)
demo.launch()