import re
from langchain.llms import OpenAI
import requests
import json
import os
import phonenumbers
from src.constants import *
def answer_suggestion(query):
llm = OpenAI(openai_api_key = os.environ["OPENAI_API_KEY"],
model_name = 'gpt-4',
temperature=0.5)
response = llm(query)
# response = chain.run(query)
return response
def has_alpha(input_string):
return any(char.isalpha() for char in input_string)
def remove_alpha_chars(input_string):
return re.sub(r'[a-zA-Z]', '', input_string)
def check_string(input_string):
keywords = ["understand your question", "understand your query", "enough information", "don't have enough information", "openai", "open ai", "gpt", "rephrase", "I don't have the details", "there's no information","but I can't provide" "document does not", "I don't have",'not provided in the context','do not have']
for keyword in keywords:
if keyword.lower() in input_string.lower():
return True
return False
def check_string_2_company_string(input_string):
keywords = ["openai", "open ai", "gpt","context","GPT-3.5-turbo","gpt-2","gpt 3","gpt 3.5","gpt-4","gpt-4-0613","gpt-4-32k","gpt-4-32k-0613","gpt-3.5-turbo","gpt-3.5-turbo-16k","gpt-3.5-turbo-0613","text-davinci-002"]
for keyword in keywords:
if keyword in input_string.lower():
return True
return False
def extract_phone_numbers(text):
# Define a regular expression pattern to match potential phone number candidates
pattern = r'\b(?:\+?(?:(?:(?:\d{1,3}[\s.-])?[(]?\d{1,4}[)]?[\s.-]?))?)?(?:(?:\d{1,5}[\s.-])?\d{1,5}[\s.-]?\d{1,9})(?:[^\d\s]+)?\b'
# Use re.findall() to extract all potential phone number candidates
phone_number_candidates = re.findall(pattern, text)
return phone_number_candidates
def is_valid_phone_number(phone_number, default_country_code="US"):
try:
# Try parsing with the default country code assumption
parsed_number = phonenumbers.parse(phone_number, default_country_code)
if phonenumbers.is_valid_number(parsed_number):
return True
# If not valid, try with other common country codes
common_country_codes = ["US", "GB", "IN", "AE", "AU"]
for country_code in common_country_codes:
parsed_number = phonenumbers.parse(phone_number, country_code)
if phonenumbers.is_valid_number(parsed_number):
return True
return False
except phonenumbers.NumberParseException:
return False
def extract_email_and_phone(text):
try:
invalid_phone = []
phones = []
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
emails = re.findall(email_pattern, text)
potential_numbers = extract_phone_numbers(text)
for number in potential_numbers:
if is_valid_phone_number(number) and has_alpha(number)==False:
phones.append(number)
else:
if len(number) >= 7:
invalid_phone.append(number)
return emails, phones, invalid_phone
except:
return [], [],[]
def check_user_intent(input_string):
keywords = ["i wouldn't give","no","sorry", "i can't give my personal details","why should i give","why"]
pattern = r'\b(?:{})\b'.format('|'.join(map(re.escape, keywords)))
match = re.search(pattern, input_string.lower())
return match is not None
def collect_email(text,context_id,mail):
emails,phones,invalid_phone = extract_email_and_phone(text)
if emails !=[] or phones !=[]:
#change status true
#upload contact details
if invalid_phone !=[]:
headers = {'Content-Type': 'application/json'}
payload = {"context_id": context_id,"email": str(emails),"contact": "","contact_status":4}
requests.post(contact_save, headers=headers, data=json.dumps(payload)).json()
resp = "Hello, it appears that the number you entered is incorrect. Could you kindly retype it again?"
else:
headers = {'Content-Type': 'application/json'}
payload = {"context_id": context_id,"email": str(emails),"contact": str(phones),"contact_status":3}
requests.post(contact_save, headers=headers, data=json.dumps(payload)).json()
resp = "Thank you for sharing your contact details. Our team will reach out to you within the next 24 hours to assist with your inquiry. In the meantime, if there's anything else I can help you with, please don't hesitate to ask."
else:
status = check_user_intent(text)
if status == True:
headers = {'Content-Type': 'application/json'}
payload = {"context_id": context_id,"email": "","contact": "","contact_status": 2}
requests.post(contact_save, headers=headers, data=json.dumps(payload)).json()
resp = "I understand your privacy concerns, and I respect your decision. If you have any other questions or concerns, feel free to ask, and I'll do my best to assist you. You can also reach out to our support team directly at "+ mail
else:
headers = {'Content-Type': 'application/json'}
payload = {"context_id": context_id,"email": "","contact": "","contact_status": 1}
requests.post(contact_save, headers=headers, data=json.dumps(payload)).json()
resp = "No"
if invalid_phone !=[]:
headers = {'Content-Type': 'application/json'}
payload = {"context_id": context_id,"email": str(emails),"contact": "","contact_status":4}
requests.post(contact_save, headers=headers, data=json.dumps(payload)).json()
resp = "Hello, it appears that the number you entered is incorrect. Could you kindly retype it again?"
return resp