NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


import openai
import faiss
import numpy as np
import pickle
from tqdm import tqdm
import argparse
import os

def create_embeddings(input):
"""Create embeddings for the provided input."""
result = []
# limit about 1000 tokens per request
lens = [len(text) for text in input]
query_len = 0
start_index = 0
tokens = 0

def get_embedding(input_slice):
embedding = openai.Embedding.create(model="text-embedding-ada-002", input=input_slice)
return [(text, data.embedding) for text, data in zip(input_slice, embedding.data)], embedding.usage.total_tokens

for index, l in tqdm(enumerate(lens)):
query_len += l
if query_len > 4096:
ebd, tk = get_embedding(input[start_index:index + 1])
query_len = 0
start_index = index + 1
tokens += tk
result.extend(ebd)

if query_len > 0:
ebd, tk = get_embedding(input[start_index:])
tokens += tk
result.extend(ebd)
return result, tokens

def create_embedding(text):
"""Create an embedding for the provided text."""
embedding = openai.Embedding.create(model="text-embedding-ada-002", input=text)
return text, embedding.data[0].embedding

class QA():
def __init__(self,data_embe) -> None:
d = 1536
index = faiss.IndexFlatL2(d)
embe = np.array([emm[1] for emm in data_embe])
data = [emm[0] for emm in data_embe]
index.add(embe)
self.index = index
self.data = data
def __call__(self, query):
embedding = create_embedding(query)
context = self.get_texts(embedding[1], limit)
answer = self.completion(query,context)
return answer,context
def get_texts(self,embeding,limit):
_,text_index = self.index.search(np.array([embeding]),limit)
context = []
for i in list(text_index[0]):
context.extend(self.data[i:i+5])
# context = [self.data[i] for i in list(text_index[0])]
return context

def completion(self,query, context):
"""Create a completion."""
lens = [len(text) for text in context]

maximum = 3000
for index, l in enumerate(lens):
maximum -= l
if maximum < 0:
context = context[:index + 1]
print("H2H", index + 1, "h2h")
break

text = "n".join(f"{index}. {text}" for index, text in enumerate(context))
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{'role': 'system',
'content': f"""In your role as an H2hMovers AI Chatbot, your primary responsibility is to assist customers with their moving-related queries by engaging in conversations based on a provided document.
Start by thoroughly understanding the content of the document, which should contain information about moving services, pricing, packing guidelines, contact details, and other relevant details.
When interacting with customers, ask clear and concise open-ended questions that help them clarify doubts and gather additional information.
Personalize the conversation by using the customer's name and any relevant information obtained during the interaction.
Seek clarification when the customer's query is unclear or if the document doesn't provide a direct answer, and be sure to offer follow-up questions as needed.
Maintain a polite and professional tone throughout the conversation to ensure a positive customer service experience.
Address any special scenarios or unique circumstances related to moving by asking appropriate questions.
Actively listen to the customer's responses and adapt your questions accordingly.
Conclude the conversation by offering assistance, contact information, or any additional relevant details if required, always adhering to ethical guidelines and avoiding requests for sensitive personal information.
Continuously learn from each interaction to enhance your customer service skills and become more effective in assisting customers with their moving-related queries.
If a question goes beyond moving and packing requirements, you will give the following response: "I am Ai Chatbot for H2HMovers, I have no knowledge apart from it".
Also ask the customer on behalf of company about the service.
following are the Task's you will mainly assisting user with which you have to decide from the user conversation.

1. Task : greetings
This is general greeting with user. If there are any previously incomplete leads, ask about those.
Always greet user with, 'Welcome to H2HMovers Bot! How may I assist you today?' [STRICTLY FOLLOW THIS]
Example,
User : hi
Bot : Welcome to H2HMovers Bot! How may I assist you today?:nn{text}"""},
{'role': 'user', 'content': query},
],
)
print("tokens:", response.usage.total_tokens)
return response.choices[0].message.content
import glob
# from pdf_embeddings_function import create_embeddings
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Document QA")
parser.add_argument("--input_file", default="/home/admin1/Documents/H2HMOVERS/h2h_docs/General INformation.pdf", dest="input_file", type=str,help="file_path")
parser.add_argument("--file_embeding", default="input_embed.pkl", dest="file_embeding", type=str,help="File embedding")
parser.add_argument("--print_context", action='store_true',help="print")


args = parser.parse_args()
pdf_files = glob.glob(os.path.join(args.input_file, "*.pdf"))

if os.path.isfile(args.file_embeding):
data_embe = pickle.load(open(args.file_embeding,'rb'))
else:
with open(args.input_file,'r',encoding='utf-8') as f:
texts = f.readlines()
texts = [text.strip() for text in texts if text.strip()]
data_embe,tokens = create_embeddings(texts)
pickle.dump(data_embe,open(args.file_embeding,'wb'))
print(" {} tokens".format(tokens))

qa =QA(data_embe)

limit = 10
while True:
query = input("Please enter a query):")
if query == "quit":
break
elif query.startswith("limit"):
try:
limit = int(query.split(" ")[1])
print("已设置limit为", limit)
except Exception as e:
print("设置limit失败", e)
continue
elif query == "help":
print("输入limit [数字]设置limit")
print("输入quit退出")
continue
answer,context = qa(query)
if args.print_context:
print("已找到相关片段:")
for text in context:
print('t', text)
print("=====================================")
print("回答如下nn")
print(answer.strip())
print("=====================================")
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.