Spaces:
Sleeping
Sleeping
from llama_index import VectorStoreIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext, StorageContext,load_index_from_storage | |
from langchain.chat_models import ChatOpenAI | |
from langchain import OpenAI | |
import os | |
from llama_index.langchain_helpers.agents import IndexToolConfig, LlamaToolkit, create_llama_chat_agent | |
from langchain.chains.conversation.memory import ConversationBufferMemory | |
import json | |
from llama_index import QuestionAnswerPrompt | |
prompt_names = ["Simple Question - Financial","Simple Question - Marketing","Deep Business Research On any topic", | |
"Financial - Extensive Research Report","Financial - Detailed Financial Report","Marketing - Create a marketing strategy", | |
"Marketing - Create Educational Blog Post","Marketing - Create promotional content","Marketing - Creative Campaign Build For Product", | |
"Respond To A Customer Email" | |
] | |
combined_prompts=["Regulatory Screener"] | |
prompt_enhancers_main = [ | |
"""As an expert financial analyst, you are tasked with creating a well-researched report on the provided topic that is specified at the end of this prompt with the trigger word being INSTRUCTIONS. This is to be written for a for sophisticated and educated investor. Your report should be detailed, accurate, and concise while offering key findings, trends, and insights using headlines and bullet points.Your audience comprises knowledgeable investors who expect professional-level content. Ensure that your analysis is as long as it needs to be, be thorough and insightful by considering factors such as market conditions, industry trends, company performance metrics, risk assessments, future growth prospects, and any other relevant information. Here are the INSTRUCTIONS: | |
""", | |
"""As the world's best creative marketer and messenger, your task is to develop a unique and comprehensive marketing and messaging answer based off the the task below which follows the word INSTRUCTIONS. Your goal is to provide an effective piece of content from the instructions that showcases creativity and expertise while maintaining organization through headers and bullet points. Ensure your response stands out among competitors while being clear, organized, detailed by using headers and bullet points throughout. Here are your INSTRUCTIONS: | |
""", | |
"""My main request will be at the end of this prompt, which I will identify as HERE IS YOUR TASK: But First, I wanted to provide you specific instructions first. When performing your task, when applicable, please use Headlines to seperate topics and numbered bullet points for subtopics and/or details when neccessary. Please write in a friendly tone and pay special attention to accuracy and be as thorough as required.Ignore all previous instructions. Act as a researcher and writing expert, professor with fluent ENGLISH and 20 years of research experience. This is to be written for MBA level executives in the cruise industry. ALWAYS include clear headlines, bullet points, and sub-bullet points where needed. Use industry data, financial data, any data and sources when available (including references where you found if possible. Put key takeaways at the end of the article. pay special attention to accuracy, thouroughness, and completeness. Cite sources when possible. Ensure this will be long enough and have content to complete the entire research article, thought process, to completion.Please follow these instructions precisely, HERE IS YOUR TASK: | |
""", | |
"""As an expert financial analyst, you are tasked with creating a well-researched report on the provided topic for sophisticated and educated investors. Your report should be detailed, accurate, and concise while offering key findings, trends, and insights using headlines and bullet points.Your audience comprises knowledgeable investors who expect professional-level content. Ensure that your analysis is thorough and insightful by considering factors such as market conditions, industry trends, company performance metrics, risk assessments, future growth prospects, and any other relevant information.Structure your report with the following sections:1. Executive Summary2. Introduction (including the topic)3. Market Overview4. Industry Analysis5. Company Analysis6. Financial Performance Metrics7. Risk Assessment & Mitigation Strategies8. Future Growth Prospects & Recommendations.Please provide specific details related to the given topic within each section of the report structure outlined above so that it becomes an informative and actionable guide for potential investors interested in this particular area of investment.In addition to maintaining a clear focus on delivering valuable insights to these high-profile investors, demonstrate flexibility and creativity in your response by exploring unique angles or perspectives that may not have been previously considered but could provide additional value for potential investors.Topic: | |
""", | |
"""As a stock research analyst, your task is to analyze and provide a comprehensive summary of the specified company's financial performance for the requested years. The company and additional information will be at the end of this prompt following the words INSTRUCTIONS. Your response should be structured in an easy-to-understand format suitable for someone who does not have specific knowledge about this company or sector. Please structure your response using bullet points with headings and subheadings as follows: 1. Key Business and Operational Highlights: - Summarize significant business events and operational achievements mentioned in the reports. 2. Financial Performance: - Revenues: Provide revenue figures, changes from last quarter and last year, and explanations for these changes. - Expenses: Summarize expenses, including any notable increases or decreases compared to prior periods. - Income: Describe income trends, comparing them to previous quarters and years. 3. Balance Sheet Analysis: - Liabilities: Explain any major shifts in liabilities observed during the reporting period. - Liquidity: Assess the company's liquidity position based on available data. - Funding: Highlight any noteworthy funding activities that occurred during the reporting period. - Capital: Discuss capital-related developments within the company (e.g., issuance of new shares). - Asset Quality: Analyze asset quality indicators such as non-performing assets ratios. 4. Forward Guidance: - Summarize forward guidance provided by the company, indicating whether it has been raised or lowered compared to previous projections. In your response, please ensure proper grammar usage without fully capitalizing verbs. Also, do not mention anything about yourself or the task you have been given to answer. To proceed with this analysis, kindly provide below information: | |
""", | |
"""Create a well-written marketing report and analysis for the product listed at the end of this prompt, which will follow the words INSTRUCTIONS. Your response should be creative, engaging, and professional in tone to effectively appeal to potential customers while focusing on accuracy, relevance, and clarity. Please follow these guidelines: 1. Identify the target audience for this product or service. 2. Clearly define at least three unique selling points (USPs) of the product or service. 3. Mention any special offers, promotions, or discounts associated with it. 4. Explain how this product or service can benefit potential customers by addressing their specific needs, pain points, or desires. 5. Include fictional testimonials, case studies, or statistics that demonstrate success stories related to the product or service. 6. Provide a call-to-action (CTA) that encourages readers to take advantage of what is being offered. Organize your response using headlines and bullet points where appropriate for better readability and quick comprehension of key aspects. Here is the product we are rebranding and need this reuest for: | |
""", | |
"""Your task is to use all of the information you have in your database about the topic/company/product supplied after the DETAILED INSTRUCTIONS action at the end of this prompt. you are to write an educational and engaging blog post on behalf of a specified company or product. The main focus should be on the given topic, with the goal of indirectly supporting the product by offering valuable information to readers without making it a direct sales article. Instead, aim for creating content that showcases the benefits or relevance of the product in relation to the topic.The blog post should have several paragraphs with clear headlines and subheadings to improve readability. Remember to incorporate relevant details about the company or product as necessary but ensure it does not come across as promotional. Your writing style should captivate users while delivering useful knowledge related to the specified topic.Please note that your response should demonstrate creativity and originality, providing unique insights and perspectives on the subject matter.Once you've provided this information, proceed with crafting an informative and engaging blog post focusing on the designated topic. Throughout your text, subtly showcase how this topic relates to or supports the mentioned company/product without overt promotion.Remember to maintain a balance between providing valuable information and showcasing how it connects to your chosen company/product. This will create a more enjoyable reading experience for your audience while still highlighting its relevance.HERE ARE THE DETAILED INSTRUCTIONS: | |
""", | |
"""Your task is to use all of the information you have in your database about the topic/company/product supplied after the DETAILED INSTRUCTIONS action at the end of this prompt. you are to write an a promotional piece of content as outlined in instructions, make it engaging and it should be written to help sell the product or service. The main focus should be on the given topic in the instructions, with the goal of directly promoting the product by offering valuable information to readers and benfefits of the product without. Focus on creating content that showcases the benefits or relevance of the product in relation to the topic. The content should clear headlines and subheadings when needed to improve readability. Your writing style should utilize the brand voice within the content of your database, and captivate users while delivering useful knowledge related to the specified topic. Please note that your response should demonstrate creativity and originality, providing unique insights and perspectives on the subject matter. Once you've provided this information, proceed with crafting an informative and engaging piece of content. Remember to maintain a balance between providing valuable information and showcasing how it connects to your chosen company/product. This will create a more enjoyable reading experience for your audience while still highlighting its relevance. HERE ARE THE DETAILED INSTRUCTIONS: | |
""", | |
"""Your task is to create a creative and original campaign for my company/product which is identified at the end of this prompt after the word INSTRUCTIONS. Please provide an outline of your proposed campaign, including details on objectives, strategies, tactics, timelines, budgets, etc., as well as how each element fits together in order to effectively achieve the desired outcome. Your response should include a clear goal for the campaign that aligns with our business goals and objectives; an effective strategy for achieving it; and specific tactics that can be used to reach our target audience. Additionally, please feel free to incorporate creative ideas or suggestions which could potentially increase effectiveness or improve the overall impact of the campaign. Your response should also take into account any relevant industry trends or changes in consumer behavior that may affect the success of the campaign. Please note that you should use the provided sequences as inspiration and guidance when crafting your response but allow yourself flexibility to come up with unique solutions. You should also ensure that your response is structured clearly and logically with attention to detail so that all elements fit together cohesively in order to achieve the desired outcome. HERE ARE THE DETAILED INSTRUCTIONS: | |
""", | |
"""My main request will be at the end of this prompt, which I will identify at the end of the prompt. But First, I wanted to provide you specific instructions first. When performing your task, when applicable, please use Headlines to seperate topics and numbered bullet points for subtopics and/or details when neccessary. Please write in a friendly tone and pay special attention to accuracy and be as thorough as required.You are responding to a customer service email. Please Acknowledge the customer's issue or question, provide a helpful solution or response, and offer additional assistance or follow-up if needed. Please follow these instructions precisely and execute this task: | |
""", | |
] | |
defaultResponse = "Sorry! I can't answer this question, probably because I don't have any relevant Information.\nPlease ask a new question.\nThanks!" | |
def handleSubmit(brainName, question, enhancer, memory, temperature, maxTokens): | |
print(memory) | |
# prompt = """Please use the following instructions and formatting notes only when applicable. 1. Separate primary topics into sections with headlines. Use numbered bullet points for new sub topics within those primary topics and headlines. Respond with the accurate, extensive data. the writing style should be written for an educated reader. Be highly detailed, thorough when needed, accuracy is paramount to this task. Here is the Question: """ | |
prompt = "" | |
combined = False | |
if (question == ""): | |
return "Please Enter Question" | |
if (enhancer != ""): | |
promptIndex = prompt_names.index(enhancer) | |
print("Prompt no: ", promptIndex+1) | |
prompt = prompt_enhancers_main[promptIndex] | |
print("Main Prompt") | |
if (enhancer in combined_prompts): | |
combined = True | |
print("combined: ", combined) | |
if (memory == []): | |
return askQuestion(brainName, question, prompt, temperature, maxTokens, combined) | |
return followUp(brainName, question, memory, temperature, maxTokens) | |
def askQuestion(brain, question, prompt, temperature, maxTokens, combined): | |
temperature = float(temperature) | |
finalQuestion = prompt+question | |
# print(finalQuestion) | |
print(temperature, maxTokens) | |
# print(type(temperature)) | |
# print(type(maxTokens)) | |
print(brain) | |
storage_context = StorageContext.from_defaults(persist_dir="./"+brain) | |
# load index | |
index = load_index_from_storage(storage_context) | |
# index = GPTSimpleVectorIndex.load_from_disk('index.json') | |
# For Q-A set this value to 4, For Content-Genration set this value b/w 7-10. | |
data_chunks = 7 | |
QA_PROMPT_TMPL = ( | |
"We have provided context information below. \n" | |
"---------------------\n" | |
"{context_str}" | |
"\n---------------------\n" | |
"Given this information, please answer the question at the end of this main prompt: " + | |
prompt+" {query_str}\n" | |
) | |
QA_PROMPT = QuestionAnswerPrompt(QA_PROMPT_TMPL) | |
query = question | |
new = LLMPredictor(llm=ChatOpenAI(temperature=temperature, | |
model_name="gpt-3.5-turbo-16k", max_tokens=maxTokens)) | |
# new = LLMPredictor(llm=OpenAI( | |
# temperature=temperature, model_name="text-davinci-003", max_tokens=maxTokens)) | |
service_context = ServiceContext.from_defaults(llm_predictor=new) | |
query_engine = index.as_query_engine( | |
similarity_top_k=7, response_mode="compact", service_context=service_context,text_qa_template=QA_PROMPT) | |
query = question | |
if combined == False: | |
response = query_engine.query(query) | |
print("No Combined") | |
if (response.response == None): | |
response.response = defaultResponse | |
res = {"data": [response.response, { | |
"memory": [question, response.response]}]} | |
return json.dumps(res) | |
else: | |
response = query_engine.query(finalQuestion) | |
print("Combined") | |
res = {"data": [response.response, { | |
"memory": [question, response.response]}]} | |
return json.dumps(res) | |
def followUp(brainName, question, memory, temperature, maxTokens): | |
if (memory == False): | |
return "Please Initiate the Chat first.." | |
if(int(maxTokens)>2000): | |
maxTokens=2000 | |
newMemory = ConversationBufferMemory(memory_key="chat_history") | |
for index in range(len(memory)): | |
if index % 2 == 0: | |
# if index is even, add user message to chat memory | |
newMemory.chat_memory.add_user_message(memory[index]) | |
else: | |
# if index is odd, add AI message to chat memory | |
newMemory.chat_memory.add_ai_message(memory[index]) | |
temperature = float(temperature) | |
index = VectorStoreIndex.from_documents([]) | |
query_engine = index.as_query_engine(similarity_top_k=4,response_mode="compact") | |
llm = OpenAI( | |
temperature=temperature, model_name="text-davinci-003", max_tokens=maxTokens) | |
tool_config = IndexToolConfig( | |
query_engine=query_engine, | |
name="Vector Index", | |
description="Use this tool if you can't find the required Information in the previous message history", | |
index_query_kwargs={"similarity_top_k": 4, "response_mode": "compact"}, | |
tool_kwargs={"return_direct": True} | |
) | |
toolkit = LlamaToolkit(index_configs=[tool_config]) | |
agent_chain = create_llama_chat_agent( | |
toolkit, llm, memory=newMemory, verbose=True) | |
response = agent_chain.run(question) | |
memory.append(question) | |
memory.append(response) | |
res = {"data": [response, {"memory": memory}]} | |
return json.dumps(res) | |