Spaces:
Sleeping
Sleeping
File size: 3,273 Bytes
fde2252 9fcd64a fde2252 |
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 |
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
qna_system_message = """
You are an assistant to an insurance firm who answers user queries on policy documents.
User input will have the context required by you to answer user questions.
This context will begin with the word: ###Context.
The context contains references to specific portions of a document relevant to the user query.
User questions will begin with the word: ###Question.
Please answer user questions only using the context provided in the input.
Do not mention anything about the context in your final answer. Your response should only contain the answer to the question.
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 = """
###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 = 'mlabonne/NeuralHermes-2.5-Mistral-7B'
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(question):
relevant_document_chunks = retriever.invoke(question)
context_list = [d.page_content for d in relevant_document_chunks]
context_for_query = "\n".join(context_list)
prompt = [
{'role':'system', 'content': qna_system_message},
{'role': 'user', 'content': qna_user_message_template.format(
context=context_for_query,
question=question
)
}
]
try:
response = client.chat.completions.create(
model=qna_model,
messages=prompt,
temperature=0
)
prediction = response.choices[0].message.content.strip()
except Exception as e:
prediction = f'Sorry, I encountered the following error: \n {e}'
return prediction
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
demo = gr.Interface(
inputs=textbox, fn=predict, outputs="text",
title="AMA on your insurance policy document",
description="This web API presents an interface to ask questions on contents of your health insurance policy.",
article="Note that questions that are not relevant to the policy will not be answered.",
examples=[["My trip was delayed and I paid 45, how much am I covered for?", ""],
["I just had a baby, is baby food covered?", ""],
["How is the gauze used in my operation covered?", ""]
],
concurrency_limit=16
)
demo.queue()
demo.launch(auth=("demouser", os.getenv('PASSWD'))) |