rkoushikroy2 commited on
Commit
e1895ef
1 Parent(s): e9dc548

Upload 9 files

Browse files
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import gradio as gr
3
+ from helper_functions import *
4
+
5
+ with gr.Blocks() as app:
6
+ gr.Markdown('# Trading Q&A Bot')
7
+ session_data = gr.State([
8
+ [],[]
9
+ ])
10
+ def user(user_message, history):
11
+ return "", history + [[user_message, None]]
12
+
13
+ def bot(history, session_data_fn):
14
+ messages_archived = session_data_fn[0]
15
+ messages_current = session_data_fn[1]
16
+ bot_message, messages_archived, messages_current = get_reply(history[-1][0], messages_archived, messages_current)
17
+ history[-1][1] = bot_message
18
+ session_data_fn[0] = messages_archived
19
+ session_data_fn[1] = messages_current
20
+ return history, session_data_fn
21
+
22
+ def reset_memory(session_data_fn):
23
+ messages_archived = session_data_fn[0]
24
+ # print("Message Archived Len=", len(messages_archived))
25
+ if(len(messages_archived)>=21):
26
+ messages_archived = messages_archived[0:1] + messages_archived[3:]
27
+ session_data_fn[0] = messages_archived
28
+ return session_data_fn
29
+
30
+ def clear_data(session_data_fn):
31
+ messages_archived = [
32
+ {"role": "system", "content": pre_text}
33
+ ]
34
+ messages_current = []
35
+ session_data_fn[0] = messages_archived
36
+ session_data_fn[1] = messages_current
37
+ return None, session_data_fn
38
+
39
+ def get_context_gr(session_data_fn):
40
+ messages_current = session_data_fn[1]
41
+ return str(messages_current)
42
+
43
+ with gr.Tab("Chat"):
44
+ with gr.Row():
45
+ with gr.Column():
46
+ msg = gr.Textbox()
47
+ with gr.Row():
48
+ submit = gr.Button("Submit")
49
+ clear = gr.Button("Clear")
50
+ with gr.Column():
51
+ chatbot = gr.Chatbot()
52
+
53
+ with gr.Tab("Prompt"):
54
+ context = gr.Textbox()
55
+ submit_p = gr.Button("Check Prompt")
56
+ # Tab Chat
57
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
58
+ bot, [chatbot, session_data], [chatbot, session_data]
59
+ ).then(
60
+ fn = reset_memory, inputs = session_data, outputs = session_data
61
+ )
62
+ submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
63
+ bot, [chatbot, session_data], [chatbot, session_data]
64
+ ).then(
65
+ fn = reset_memory, inputs = session_data, outputs = session_data
66
+ )
67
+ clear.click(
68
+ fn = clear_data,
69
+ inputs = session_data,
70
+ outputs = [chatbot, session_data],
71
+ queue = False
72
+ )
73
+ # Tab Prompt
74
+ submit_p.click(get_context_gr, session_data, context, queue=False)
75
+ app.launch(auth=(os.getenv("id"), os.getenv("password")), show_api=False)
helper_functions.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings.openai import OpenAIEmbeddings
2
+ from langchain.vectorstores import Chroma
3
+ import os
4
+ import openai
5
+ import pandas as pd
6
+ import numpy as np
7
+
8
+ # Set up OpenAI API key
9
+ openai.api_key = os.getenv("OPENAI_API_KEY")
10
+
11
+ # Load data
12
+ persist_directory = 'trading_psychology_db'
13
+ embedding = OpenAIEmbeddings()
14
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding)
15
+
16
+ pre_text_trading_psychology = """
17
+ You are a helpful AI assistant that helps people with their trading psychology.
18
+ You can use the provided context as knowledge source.
19
+ Context is generated from a trading psychology podcast called 'Chat with Traders' Hosted by 'Aaron Fifield'.
20
+ You answer the question by using the context and your own knowledge. Try to give definitive answers.
21
+ Do not mention that the answer is based on the context.
22
+ """
23
+
24
+ def search(query):
25
+ similar_docs = vectordb.similarity_search(query=query, k=10)
26
+ similar_texts = [doc.page_content for doc in similar_docs]
27
+ context = "\n\n".join(similar_texts)
28
+ return context
29
+
30
+ def get_context(prompt):
31
+ # Get the context based on the prompt
32
+ context = search(prompt)
33
+
34
+ # Concatenate the prompt and context
35
+ formatted_prompt = f"""
36
+ {pre_text_trading_psychology}
37
+
38
+ User Question: {prompt}
39
+
40
+ Context: ```{context}```
41
+
42
+ Your answer:
43
+ """
44
+ return formatted_prompt
45
+
46
+ def get_reply(message, messages_archived, messages_current):
47
+
48
+ if message:
49
+ messages_current = messages_archived.copy()
50
+ context = get_context(message)
51
+ messages_current.append(
52
+ {"role": "user", "content": context}
53
+ )
54
+ chat = openai.ChatCompletion.create(
55
+ model="gpt-3.5-turbo", messages=messages_current, temperature=0
56
+ )
57
+
58
+ reply = chat.choices[0].message.content
59
+ messages_archived.append({"role": "user", "content": message})
60
+ messages_archived.append({"role": "assistant", "content": reply})
61
+ # If no message is provided, return a string that says "No Message Received"
62
+ else:
63
+ reply = "No Message Received"
64
+
65
+ return reply, messages_archived, messages_current
66
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ pandas
3
+ numpy
4
+ matplotlib
5
+ scikit-learn
6
+ plotly
7
+ chroma
8
+ langchain
trading_psychology_db/chroma-collections.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1ae55c0abbb25ad17d63a6732feacf47806f61807485bd4b7758a31cb5ada35
3
+ size 557
trading_psychology_db/chroma-embeddings.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f08cb6580c659c5e8faaaa404b55f6da24d9e3f67abef14d8cb522f177cccd0a
3
+ size 57649612
trading_psychology_db/index/id_to_uuid_4f94db2f-78d2-4816-ac3d-207a0e7f262d.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e9f435e479fada87fecd19ba43fa21ed476d3ff654dbcad819ceb500dc3fd771
3
+ size 202368
trading_psychology_db/index/index_4f94db2f-78d2-4816-ac3d-207a0e7f262d.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:946a7b24b9a96d54aebaea7bbefd0859972f89c1aaa2b0d288814b60b1424758
3
+ size 39217988
trading_psychology_db/index/index_metadata_4f94db2f-78d2-4816-ac3d-207a0e7f262d.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:de986627cfd874afb64b76640619ee2fc43fb2350d15ef5fc3f65cddf06ab98f
3
+ size 74
trading_psychology_db/index/uuid_to_id_4f94db2f-78d2-4816-ac3d-207a0e7f262d.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a8a23c2548020d2e4b0ecd730e7454f916e22f3e72d45214f1f6f49b91f89078
3
+ size 236615