Spaces:
Build error
Build error
from openai.embeddings_utils import get_embedding, cosine_similarity | |
import os | |
import openai | |
import pandas as pd | |
import numpy as np | |
import time | |
# Set up OpenAI API key | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
# Load data | |
df = pd.read_csv('data_with_ada_embedding.csv') | |
df["token"] = df.combined_summarised.map(len)//4 | |
df['ada_embedding'] = df.ada_embedding.apply(eval).apply(np.array) | |
pre_text = """FundedNext funds promising traders from all over the world. This is how it works: traders come to the platform, and sign up for different challenges. If they are able to reach the trading targets without breaching any of the rules and pass the challenge, then they can get funded by your company FundedNext. | |
Fundednext has two account models. Users can go for Either Express Model or Evaluation Model, To get a real funded account. Each model has challenge phase and real phase. After sucessfully completing the challenge phase without violating any rules, users are eligible for their real trading account. | |
Express model has two phases. Express Demo and Express Real. Express Demo is the challenge phase. Express users need to pass only one challenge phase to get to Express Real phase. | |
While traders in the Evaluation model need to pass two challenge phases called Phase 1 and Phase 2. The final phase in Evaluation model is Evaluation Real. | |
You are supposed to help the users of FundedNext with their questions and provide them with helpful answers. | |
Your Approach to the conversation will be as follows : | |
As an AI customer service agent, your main job is to help customers with their questions and concerns. This requires you to be empathetic and understanding of their situation. You should always begin the conversation by asking how you can help and listening carefully to their response. | |
It's important to make the customer feel welcome and comfortable by using pleasant greetings and respectful language throughout the interaction. You should always remain professional and respectful, even if the customer is upset or frustrated. | |
Remember to ask clarifying questions if necessary to fully understand the customer's issue. Once you have identified the problem, work to find a solution that meets their needs. End the conversation on a positive note by thanking the customer for their time and offering any additional assistance they may need. | |
Overall, your goal is to provide top-notch customer service that leaves a positive impression on every customer. By following these guidelines, you can ensure that you are doing just that. | |
To help you with the necessary information needed to properly serve the client, relevant context and information are shared with you up next. Use the context to ask follow up questions to the user and respond to the queries. You should only answer the question if you are sure of the answer based on the provided context.""" | |
def search(df, query, max_n, max_token): | |
query_embedding = get_embedding( | |
query, | |
engine="text-embedding-ada-002" | |
) | |
df["similarity"] = df.ada_embedding.apply(lambda x: cosine_similarity(x, query_embedding)) | |
df = df.sort_values("similarity", ascending=False).head(max_n) | |
df = df[df['similarity'] >= .77] | |
df["cumulative_sum"] = df.token.cumsum() | |
return '\n\n'.join(df[(df['cumulative_sum'] < max_token)]["combined_summarised"]) | |
def get_context(query): | |
results = search(df, query, max_n = 10, max_token = 500) | |
return f"""I will ask you a question. You will answer with the help of knowlwedge base. | |
Sometimes the knowledge base will be empty. It means that you will have to answer the question yourself. | |
--Start of Knowledge Base-- | |
{results} | |
--End of Knowledge Base-- | |
My question is: "{query}" | |
""" | |
def get_reply(message, messages_archived, messages_current): | |
if message: | |
messages_current = messages_archived.copy() | |
context = get_context(message) | |
messages_current.append( | |
{"role": "user", "content": context} | |
) | |
chat = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", messages=messages_current, temperature=0 | |
) | |
reply = chat.choices[0].message.content | |
messages_archived.append({"role": "user", "content": message}) | |
messages_archived.append({"role": "assistant", "content": reply}) | |
# If no message is provided, return a string that says "No Message Received" | |
else: | |
reply = "No Message Received" | |
return reply, messages_archived, messages_current | |
def user(user_message, history): | |
return "", history + [[user_message, None]] | |
def bot(history, session_data_fn): | |
messages_archived = session_data_fn[0] | |
messages_current = session_data_fn[1] | |
bot_message, messages_archived, messages_current = get_reply(history[-1][0], messages_archived, messages_current) | |
history[-1][1] = bot_message | |
session_data_fn[0] = messages_archived | |
session_data_fn[1] = messages_current | |
session_data_fn[2].append(history[-1][0]) | |
session_data_fn[3].append(history[-1][1]) | |
return history, session_data_fn | |
def reset_memory(session_data_fn): | |
messages_archived = session_data_fn[0] | |
# print("Message Archived Len=", len(messages_archived)) | |
if(len(messages_archived)>=21): | |
messages_archived = messages_archived[0:1] + messages_archived[3:] | |
session_data_fn[0] = messages_archived | |
return session_data_fn | |
def clear_data(session_data_fn): | |
messages_archived = [ | |
{"role": "system", "content": pre_text} | |
] | |
messages_current = [] | |
session_data_fn[0] = messages_archived | |
session_data_fn[1] = messages_current | |
session_data_fn[2] = [] | |
session_data_fn[3] = [] | |
return None, session_data_fn | |
def get_context_gr(session_data_fn): | |
messages_current = session_data_fn[1] | |
return str(messages_current) | |
def download_file(session_data_fn): | |
df = pd.DataFrame(list(zip(session_data_fn[2], session_data_fn[3])), | |
columns =['user_message', 'bot_reply']) | |
current_date_time = time.strftime("Date %Y_%m_%d Time %H_%M_%S", time.localtime()) | |
file_name = "files/Chat History " + current_date_time + ".csv" | |
df.to_csv(file_name, index=False) | |
return file_name | |