insurance-policy-qna / chat_interface.py
pgurazada1's picture
Upload chat_interface.py
3afac77 verified
raw
history blame
3.71 kB
import os
import chromadb
import gradio as gr
from dotenv import load_dotenv
from openai import OpenAI
from langchain_community.embeddings import AnyscaleEmbeddings
from langchain_community.vectorstores import Chroma
from typing import List
qna_system_message = """
You are an assistant to an insurance firm who answers customer queries based on their insurance policy documents.
User input will have the context required by you to answer customer questions.
This context will begin with the word: ###Context.
The context contains references to specific portions of a document relevant to the customer query.
Customer questions will begin with the word: ###Question.
Information about the customer will begin with the word: ###Customer Information
Please answer user questions ONLY using the context provided in the input and the customer information.
DO NOT mention anything about the context in your final answer.
Your response should only contain the answer to the question AND NOTHING ELSE.
If the answer is not found in the context, respond "Sorry, I cannot answer your question. Please contact our representative on the hotline 1-800-AWESOMEINSURER".
"""
qna_user_message_template = """
###Customer Information
Policy Number: NBHTGBP22011V012223#
Premium Amount: $15000
Number of premium installments: 5
Number of installments paid: 3
Last Premium Paid: Yes
Last Premium Date: 2024-05-12
###Context
Here are some documents that are relevant to the question mentioned below.
{context}
###Question
{question}
"""
load_dotenv()
anyscale_api_key = os.environ['ANYSCALE_API_KEY']
client = OpenAI(
base_url="https://api.endpoints.anyscale.com/v1",
api_key=anyscale_api_key
)
qna_model = 'meta-llama/Meta-Llama-3-8B-Instruct'
embedding_model = AnyscaleEmbeddings(
client=client,
model='thenlper/gte-large'
)
chromadb_client = chromadb.PersistentClient(path='./policy_db')
vectorstore_persisted = Chroma(
client=chromadb_client,
collection_name="policy-text",
embedding_function=embedding_model
)
retriever = vectorstore_persisted.as_retriever(
search_type='similarity',
search_kwargs={'k': 5}
)
def predict(input: str, history):
"""
Predict the response of the chatbot and complete a running list of chat history.
"""
relevant_document_chunks = retriever.invoke(input)
context_list = [d.page_content for d in relevant_document_chunks]
context_for_query = "\n".join(context_list)
user_message = [{
'role': 'user',
'content': qna_user_message_template.format(
context=context_for_query,
question=input
)
}]
prompt = [{'role':'system', 'content': qna_system_message}]
for entry in history:
prompt += (
[{'role': 'user', 'content': entry[0]}] +
[{'role': 'assistant', 'content': entry[1]}]
)
final_prompt = prompt + user_message
try:
response = client.chat.completions.create(
model=qna_model,
messages=final_prompt,
temperature=0
)
prediction = response.choices[0].message.content.strip()
except Exception as e:
prediction = f'Sorry, please contact our hotline: 1-800-AWESOMEINSURER'
return prediction
demo = gr.ChatInterface(
fn=predict,
examples=["Summarize key feaures of my policy",
"Did I pay my last installment? How many have I paid so far?",
"Are hospitalization charges included in my policy?"
],
cache_examples=False,
title="Chat about information in your insurance policy",
concurrency_limit=8,
show_progress="full"
)
demo.launch()