Artem Kobrin commited on
Commit
aec25d4
·
1 Parent(s): 4798573
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.sqlite3 filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Generative AI Chatbot through Document Sources
3
+ """
4
+ import boto3
5
+ import gradio as gr
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.embeddings.openai import OpenAIEmbeddings
8
+ #from langchain.llms import ChatOpenAI
9
+ from langchain.vectorstores import Chroma
10
+ from langchain.chat_models import ChatOpenAI
11
+
12
+ # Get OpenAI API key from SSM Parameter Store
13
+ API_KEY_PARAMETER_PATH = '/openai/api_key'
14
+ ssm_client = boto3.client('ssm', region_name='us-east-1')
15
+ def get_openai_api_key(client, parameter_path):
16
+ """
17
+ Get the OpenAI API key from the SSM Parameter Store
18
+ Args:
19
+ ssm_client: boto3 SSM client
20
+ parameter_path: path to the SSM Parameter Store parameter
21
+ Returns:
22
+ OpenAI API key
23
+ """
24
+ try:
25
+ response = client.get_parameter(
26
+ Name=parameter_path,
27
+ WithDecryption=True,
28
+ )
29
+ return response['Parameter']['Value']
30
+ except client.exceptions.ParameterNotFound:
31
+ raise Exception(f'Parameter {parameter_path} not found in SSM Parameter Store')
32
+
33
+ # Get the API key from the SSM Parameter Store
34
+ openai_api_key = get_openai_api_key(client=ssm_client, parameter_path=API_KEY_PARAMETER_PATH)
35
+
36
+ def OpenAIWithChroma(persist_directory='./chroma.db', model_name='gpt-3.5-turbo-16k', chain_type="stuff"):
37
+ """
38
+ Create a retrieval chatbot with OpenAI LLM and Chroma
39
+ Args:
40
+ persist_directory: directory to save the Chroma database
41
+ model_name: name of the OpenAI LLM
42
+ chain_type: type of chain to use for the retrieval chatbot
43
+ Returns:
44
+ RetrievalQA: retrieval chatbot
45
+ """
46
+
47
+ # connect to local Chroma
48
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
49
+ db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
50
+
51
+ # connect to OpenAI LLM with Chroma
52
+ llm = ChatOpenAI(model_name=model_name, temperature=0, openai_api_key=openai_api_key, max_tokens=5000)
53
+ chain = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=db.as_retriever(), return_source_documents=True)
54
+ return chain
55
+
56
+ def message_construction(result):
57
+ message = "**Bot Answer:** \n"
58
+ message += f"{result['result']}\n"
59
+ source_documents = "**Source Documents:**\n"
60
+ for d in result['source_documents']:
61
+ source_documents += f"* *{d.metadata['source']}* - {d.page_content[0:200].encode('unicode_escape').decode('utf-8')}...\n"
62
+ return message + "\n" + source_documents
63
+
64
+ retrieval_chain = OpenAIWithChroma()
65
+ with gr.Blocks(theme=gr.themes.Default(
66
+ primary_hue="blue",
67
+ secondary_hue="yellow"
68
+ )) as demo:
69
+ gr.Markdown("""
70
+ # Neurons Lab: Generative AI Chatbot through Document Sources
71
+ [![](https://neurons-lab-public-bucket.s3.eu-central-1.amazonaws.com/GenerativeAIHead.png)](https://docsend.com/view/c6dim5yjb35iijqv)'
72
+ ## Document Sources
73
+ 1. [Generative AI in Finance and Banking: The Current State and Future Implications](https://www.leewayhertz.com/generative-ai-in-finance-and-banking/#Variational-Autoencoders-(VAEs))
74
+ 2. [McKinsey & Company: The economic potential of generative AI](https://www.mckinsey.com/~/media/mckinsey/business%20functions/mckinsey%20digital/our%20insights/the%20economic%20potential%20of%20generative%20ai%20the%20next%20productivity%20frontier/the-economic-potential-of-generative-ai-the-next-productivity-frontier-vf.pdf)
75
+ 3. [Deloitte: Generative AI is all the rage](https://www2.deloitte.com/content/dam/Deloitte/us/Documents/deloitte-analytics/us-ai-institute-gen-ai-for-enterprises.pdf)
76
+
77
+ ## Prompt Examples
78
+ - Provide Generative AI use cases for financial services. Print in table view wiht columns: Use Case Name, Description
79
+ - Provide Generative AI models that fit for Financial Services. Print in table view with columns: Model Name, Model Description, Areas of Application in Finance.
80
+ - Provide real world example on how Generative AI change Financial Services sector.
81
+ - What is difference between traditional AI and Generative AI?
82
+ - Summarise the economic potential of generative AI
83
+ - How does Generative AI change a future of work?
84
+ - How Generative AI can personalise customer experience in finance?
85
+
86
+ """)
87
+ chatbot = gr.Chatbot()
88
+ msg = gr.Textbox()
89
+ clear = gr.ClearButton([msg, chatbot])
90
+
91
+
92
+ def respond(message, chat_history):
93
+ result = retrieval_chain({"query": message})
94
+ bot_message = message_construction(result)
95
+ chat_history.append((message, bot_message))
96
+ return "", chat_history
97
+
98
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
99
+
100
+ demo.launch()
chroma.db/01fbad3f-bd27-4643-a55d-0b69caf8390a/data_level0.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f18abd8c514282db82706e52b0a33ed659cd534e925a6f149deb7af9ce34bd8e
3
+ size 6284000
chroma.db/01fbad3f-bd27-4643-a55d-0b69caf8390a/header.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:effaa959ce2b30070fdafc2fe82096fc46e4ee7561b75920dd3ce43d09679b21
3
+ size 100
chroma.db/01fbad3f-bd27-4643-a55d-0b69caf8390a/length.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ab51ac16a607ba3546558ba23783902cd1aeda45d1fcccb4673eb72da148419c
3
+ size 4000
chroma.db/01fbad3f-bd27-4643-a55d-0b69caf8390a/link_lists.bin ADDED
File without changes
chroma.db/chroma.sqlite3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b517a18ed9554b2efc2a27ae29189f51952a48a5fb55a6d3330805c42a3e0dc3
3
+ size 4190208
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ langchain
3
+ Chroma
4
+ chromadb
5
+ openai
6
+ boto3
7
+ unstructured
8
+ tiktoken