File size: 5,539 Bytes
4d55d8d
e71c4e6
 
 
 
 
 
 
 
 
 
 
 
e031d5d
 
 
e71c4e6
 
 
 
8a6fff6
e71c4e6
 
 
8a6fff6
 
 
 
e71c4e6
8a6fff6
 
 
e19713c
0c47d68
8a6fff6
e71c4e6
4d55d8d
e71c4e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e19713c
e71c4e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d55d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a6fff6
4d55d8d
 
 
 
 
 
 
 
 
 
 
 
e71c4e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e19713c
e71c4e6
e19713c
e71c4e6
 
 
 
 
e19713c
e71c4e6
 
 
 
 
 
 
 
 
 
 
 
4d55d8d
 
 
e71c4e6
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
import json
import os
from threading import Lock
from typing import Any, Dict, Optional, Tuple

import gradio as gr
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts.chat import (ChatPromptTemplate,
                                    HumanMessagePromptTemplate,
                                    SystemMessagePromptTemplate)

from src.core.chunking import chunk_file
from src.core.embedding import embed_files
from src.core.parsing import read_file

VECTOR_STORE = "faiss"
MODEL = "openai"
EMBEDDING = "openai"
MODEL = "gpt-4"
K = 5
USE_VERBOSE = True
API_KEY = os.environ["OPENAI_API_KEY"]
# , able to have normal interactions as well as answer questions about the 'Croatia', by Insight Guies.
#The context below contains excerpts from the book 'Croatia,' by Insight Guides
# If there is not enough information in the context to formulate a response, you must respond with
# Do not to use prior knowledge when responding, you must only use the information provided in the context.
system_template = """
You are an honest and helpful AI travel assistant. Your customer is talking to you about traveling to Croatia.
Use the context below to respond to your customer.
If the context does not contain enough information to formulate a response, you must respond with:
"I'm sorry, but I can't find the answer to your question in, the book Croatia by Insight Guides."

Context:
{context}

{chat_history}
"""

# Create the chat prompt templates
messages = [
  SystemMessagePromptTemplate.from_template(system_template),
  HumanMessagePromptTemplate.from_template("{question}")
]
qa_prompt = ChatPromptTemplate.from_messages(messages)

class AnswerConversationBufferMemory(ConversationBufferMemory):
  def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
    return super(AnswerConversationBufferMemory, self).save_context(inputs,{'response': outputs['answer']})

def getretriever():
  with open("./resources/RG_Croatia_9ed_FINAL-919-745-7.pdf", 'rb') as uploaded_file:
    try:
      file = read_file(uploaded_file)
    except Exception as e:
      print(e)

  chunked_file = chunk_file(file, chunk_size=512, chunk_overlap=0)
  folder_index = embed_files(
    files=[chunked_file],
    embedding=EMBEDDING,
    vector_store=VECTOR_STORE,
    openai_api_key=API_KEY,
  )
  return folder_index.index.as_retriever(verbose=True, search_type="similarity", search_kwargs={"k": K})

retriever = getretriever()

def predict(message):
  print(message)
  msgJson = json.loads(message)
  print(msgJson)
  messages = [
    SystemMessagePromptTemplate.from_template(system_template),
    HumanMessagePromptTemplate.from_template("{question}")
  ]
  qa_prompt = ChatPromptTemplate.from_messages(messages)

  llm = ChatOpenAI(
        openai_api_key=API_KEY,
        model_name=MODEL,
        verbose=True)
  memory = AnswerConversationBufferMemory(memory_key="chat_history", return_messages=True)
  for msg in msgJson["history"]:
    memory.save_context({"input": msg[0]}, {"answer": msg[1]})

  chain = ConversationalRetrievalChain.from_llm(
    llm,
    retriever=retriever,
    return_source_documents=USE_VERBOSE,
    memory=memory,
    verbose=USE_VERBOSE,
    combine_docs_chain_kwargs={"prompt": qa_prompt})
  chain.rephrase_question = True
  lock = Lock()
  lock.acquire()
  try:
    output = chain({"question": msgJson["question"]})
    output = output["answer"]
  except Exception as e:
    print(e)
    raise e
  finally:
    lock.release()
  return output

def getanswer(chain, question, history):
  if hasattr(chain, "value"):
    chain = chain.value
  if hasattr(history, "value"):
    history = history.value
  if hasattr(question, "value"):
    question = question.value

  history = history or []
  lock = Lock()
  lock.acquire()
  try:
    output = chain({"question": question})
    output = output["answer"]
    history.append((question, output))
  except Exception as e:
    raise e
  finally:
    lock.release()
  return history, history, gr.update(value="")

def load_chain(inputs = None):
  llm = ChatOpenAI(
        openai_api_key=API_KEY,
        model_name=MODEL,
        verbose=True)
  chain = ConversationalRetrievalChain.from_llm(
    llm,
    retriever=retriever,
    return_source_documents=USE_VERBOSE,
    memory=AnswerConversationBufferMemory(memory_key="chat_history", return_messages=True),
    verbose=USE_VERBOSE,
    combine_docs_chain_kwargs={"prompt": qa_prompt})
  return chain

with gr.Blocks() as block:
  with gr.Row():
    with gr.Column(scale=0.75):
      with gr.Row():
        gr.Markdown("<h1>Croatia</h1>")
      with gr.Row():
        gr.Markdown("by Insight Guides")
      chatbot = gr.Chatbot(elem_id="chatbot").style(height=600)

      with gr.Row():
          message = gr.Textbox(
              label="",
              placeholder="Ask Croatia...",
              lines=1,
          )
      with gr.Row():
          submit = gr.Button(value="Send", variant="primary", scale=1)

      state = gr.State()
      chain_state = gr.State(load_chain)

      submit.click(getanswer, inputs=[chain_state, message, state], outputs=[chatbot, state, message])
      message.submit(getanswer, inputs=[chain_state, message, state], outputs=[chatbot, state, message])

    with gr.Column(scale=0.25):
      predictBtn = gr.Button(value="Predict", visible=False)
      predictBtn.click(predict, inputs=[message], outputs=[message])

block.launch(debug=True)