fpfaqbotdemo / app.py
adnankhd's picture
Update app.py
06ebe09 verified
raw
history blame contribute delete
No virus
3.35 kB
import gradio as gr
#import openai
import faiss
import numpy as np
import json
import os
from openai import OpenAI
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Load the FAQs with embeddings
with open('faqs_with_embeddings.json', 'r') as f:
faqs_with_embeddings = json.load(f)
# Load the FAISS index
index = faiss.read_index('faiss_index.index')
def get_relevant_faqs(query, index, faqs_with_embeddings, client):
query_embedding_response = client.embeddings.create(
input=query, model="text-embedding-3-large"
)
query_embedding = query_embedding_response.data[0].embedding
query_embedding = np.array(query_embedding).astype('float32').reshape(1, -1)
distances, indices = index.search(query_embedding, k=15) # Retrieve top 7 matches
results = []
for i in indices[0]:
results.append(faqs_with_embeddings[i])
return results
def create_prompt(user_query, relevant_faqs):
system_prompt = (
"You are a customer service agent for a bank named HBL Microfinance Bank that has a mobile wallet app named FirstPay. You are required to answer the customer query given below by looking at the information from FAQs list given after the customer query. If the information in the FAQs list is not sufficient to answer the customer query then inform the customer to call the helpline. Where needed you can also augment the answers using your general knowledge, but make sure that information about FirstPay Wallet and HBL Microfinance is coming fom the FAQs list provided and do not make anything yourself about those. Customers are from Pakistan and the customer query may also be regional Pakistani languages such as Urdu, Pashto, Sindhi, Saraiki etc and the fonts may be either roman fonts or some other regional languages fonts, if the customer asks question in one of the regional languages then answer them in the same regional language used by the customer but use roman english fonts or alphabets only."
#"You are a helpful assistant. Use the provided FAQs to answer the user's query. "
#"If the FAQs do not contain enough information, use your general knowledge to help."
)
faqs_context = "\n\n".join(
[f"FAQ {i+1}:\nQ: {faq['question']}\nA: {faq['answer']}" for i, faq in enumerate(relevant_faqs)]
)
user_prompt = f"User query: {user_query}\n\nFAQs:\n{faqs_context}"
return system_prompt, user_prompt
def chatbot_response(query):
relevant_faqs = get_relevant_faqs(query, index, faqs_with_embeddings, client)
system_prompt, user_prompt = create_prompt(query, relevant_faqs)
response = client.chat.completions.create(
model="gpt-4o", # You can use "gpt-4" if you have access
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.0
)
#answer = response['choices'][0]['message']['content']
answer = response.choices[0].message.content
return answer
# Create the Gradio interface
iface = gr.Interface(
fn=chatbot_response,
inputs="text",
outputs="text",
title="FirstPay Demo FAQ Chatbot",
description="I am your (Demo) AI Assistant for FirstPay, how can I help you."
)
# Launch the interface
iface.launch(share=True)