File size: 9,281 Bytes
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c873676
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651eba0
 
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
 
c873676
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e28d4b
cb9e56f
 
 
5e28d4b
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c873676
cb9e56f
 
 
c873676
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e28d4b
cb9e56f
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import streamlit as st
import pinecone
import openai
from openai.embeddings_utils import get_embedding
import json

OPENAI_KEY = st.secrets["OPENAI_KEY"]
PINECONE_KEY = st.secrets["PINECONE_KEY"]
INDEX = 'openai-ml-qa'
instructions = {
    "conservative q&a": "Answer the question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\nContext:\n{0}\n\n---\n\nQuestion: {1}\nAnswer:",
    "paragraph about a question":"Write a paragraph, addressing the question, and use the text below to obtain relevant information\"\n\nContext:\n{0}\n\n---\n\nQuestion: {1}\nParagraph long Answer:",
    "bullet points": "Write a bullet point list of possible answers, addressing the question, and use the text below to obtain relevant information\"\n\nContext:\n{0}\n\n---\n\nQuestion: {1}\nBullet point Answer:",
    "summarize problems given a topic": "Write a summary of the problems addressed by the questions below\"\n\n{0}\n\n---\n\n",
    "extract key libraries and tools": "Write a list of libraries and tools present in the context below\"\n\nContext:\n{0}\n\n---\n\n",
    "simple instructions": "{1} given the common questions and answers below \n\n{0}\n\n---\n\n",
    "summarize": "Write an elaborate, paragraph long summary about \"{1}\" given the questions and answers from a public forum on this topic\n\n{0}\n\n---\n\nSummary:",
}

@st.experimental_singleton(show_spinner=False)
def init_openai():
    # initialize connection to OpenAI
    openai.api_key = OPENAI_KEY

@st.experimental_singleton(show_spinner=False)
def init_pinecone(index_name):
    # initialize connection to Pinecone vector DB (app.pinecone.io for API key)
    pinecone.init(
        api_key=PINECONE_KEY,
        environment='us-west1-gcp'
    )
    index = pinecone.Index(index_name)
    stats = index.describe_index_stats()
    dims = stats['dimension']
    count = stats['namespaces']['']['vector_count']
    return index, dims, count

def create_context(question, index, lib_meta, max_len=3750, top_k=5):
    """
    Find most relevant context for a question via Pinecone search
    """
    q_embed = get_embedding(question, engine=f'text-embedding-ada-002')
    res = index.query(
        q_embed, top_k=top_k,
        include_metadata=True, filter={
            'docs': {'$in': lib_meta}
        })
    

    cur_len = 0
    contexts = []
    sources = []

    for row in res['matches']:
        meta = row['metadata']
        text = (
            f"Topic: {meta['thread']}\n"+
            f"Answer: {meta['context']}"
        )
        cur_len += len(text)
        if cur_len < max_len:
            contexts.append(text)
            sources.append(row['metadata'])
        else:
            cur_len -= len(text) + 4
            if max_len - cur_len < 200:
                break
    return "\n\n###\n\n".join(contexts), sources

def answer_question(
    index,
    fine_tuned_qa_model="text-davinci-003",
    question="Am I allowed to publish model outputs to Twitter, without a human review?",
    instruction="Answer the question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\nContext:\n{0}\n\n---\n\nQuestion: {1}\nAnswer:",
    max_len=3550,
    size="curie",
    top_k=5,
    debug=False,
    max_tokens=400,
    stop_sequence=None,
    domains=["huggingface", "tensorflow", "streamlit", "pytorch"],
):
    """
    Answer a question based on the most similar context from the dataframe texts
    """
    context, sources = create_context(
        question,
        index,
        lib_meta=domains,
        max_len=max_len,
        top_k=top_k
    )
    if debug:
        print("Context:\n" + context)
        print("\n\n")
    try:
        # fine-tuned models requires model parameter, whereas other models require engine parameter
        model_param = (
            {"model": fine_tuned_qa_model}
            if ":" in fine_tuned_qa_model
            and fine_tuned_qa_model.split(":")[1].startswith("ft")
            else {"engine": fine_tuned_qa_model}
        )
        #print(instruction.format(context, question))
        response = openai.Completion.create(
            prompt=instruction.format(context, question),
            temperature=0,
            max_tokens=max_tokens,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0,
            stop=stop_sequence,
            **model_param,
        )
        return response["choices"][0]["text"].strip(), sources
    except Exception as e:
        print(e)
        return ""

def search(index, query, style, top_k, lib_filters):
    if query != "":
        with st.spinner("Retrieving, please wait..."):
            answer, sources = answer_question(
                index,
                question=query,
                instruction=instructions[style],
                top_k=top_k
            )
            # lowercase relevant lib filters
            lib_meta = [lib.lower() for lib in lib_filters.keys() if lib_filters[lib]]
            lower_libs = [lib.lower() for lib in libraries]
        # display the answer
        st.write(answer)
        with st.expander("Sources"):
            for source in sources:
                st.write(f"""
                {source['docs']} > {source['category']} > [{source['thread']}]({source['href']})
                """)

st.markdown("""
<link
  rel="stylesheet"
  href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
""", unsafe_allow_html=True)

#model_name = 'mpnet-discourse'

libraries = [
    "Streamlit",
    "HuggingFace",
    "PyTorch",
    "TensorFlow"
]

with st.spinner("Connecting to OpenAI..."):
    retriever = init_openai()

with st.spinner("Connecting to Pinecone..."):
    index, dims, count = init_pinecone(INDEX)

st.write("# ML Q&A")
search = st.container()
query = search.text_input('Ask a framework-specific question!', "")

with search.expander("Search Options"):
    style = st.radio(label='Style', options=[
        'Paragraph about a question', 'Conservative Q&A',
        'Bullet points', 'Summarize problems given a topic',
        'Extract key libraries and tools', 'Simple instructions',
        'Summarize'
    ])
    # add section for filters
    st.write("""
    #### Metadata Filters

    **Libraries**
    """)
    # create two cols
    cols = st.columns(2)
    # add filtering based on library
    lib_filters = {}
    for lib in libraries:
        i = len(lib_filters.keys()) % 2
        with cols[i]:
            lib_filters[lib] = st.checkbox(lib, value=True)
    st.write("---")
    top_k = st.slider(
        "top_k",
        min_value=1,
        max_value=20,
        value=5
    )

st.sidebar.write(f"""
### Info

**Pinecone index name**: {INDEX}

**Pinecone index size**: {count}

**OpenAI embedding model**: *text-embedding-ada-002*

**Vector dimensionality**: {dims}

**OpenAI generation model**: *text-davinci-003*

---

### How it Works

The Q&A tool takes discussions and docs from some of the best Python ML
libraries and collates their content into a natural language search and Q&A tool.

Ask questions like **"How do I use the gradient tape in tensorflow?"** or **"What is the difference
between Tensorflow and PyTorch?"**, choose a answer style, and return relevant results!

The app is powered using OpenAI's embedding service with Pinecone's vector database. The whole process consists
of *three* steps:

**1**. Questions are fed into OpenAI's embeddings service to generate a {dims}-dimensional query vector.

**2**. We use Pinecone to identify similar context vectors (previously encoded from Q&A pages).

**3**. Relevant pages are passed in a new question to OpenAI's generative model, returning our answer.

**How do I make something like this?**

It's easy! Check out the [source code](https://github.com/pinecone-io/examples/tree/master/integrations/openai/beyond_search_webinar) and learn how to [integrate OpenAI and Pinecone in the docs](https://www.pinecone.io/docs/integrations/openai/)!

---

### Usage

If you'd like to restrict your search to a specific library (such as PyTorch or
Streamlit) you can with the *Advanced Options* dropdown. The source of information
can be switched between official docs and forum discussions too!

If you'd like OpenAI to consider more or less pages, try changing the `top_k` slider.

Want to see the original sources that GPT-3 is using to generate the answer? No problem, just click on the **Sources** box.
""")

#if style.lower() == 'conservative q&a':
#    search.info("*Access search options above.*")

if search.button("Go!") or query != "":
    with st.spinner("Retrieving, please wait..."):
        # lowercase relevant lib filters
        lib_meta = [lib.lower() for lib in lib_filters.keys() if lib_filters[lib]]
        # ask the question
        answer, sources = answer_question(
            index,
            question=query,
            instruction=instructions[style.lower()],
            top_k=top_k,
            domains=lib_meta
        )
    # display the answer
    st.write(answer)
    with st.expander("Sources"):
        for source in sources:
            st.write(f"""
            {source['docs']} > {source['category']} > [{source['thread']}]({source['href']})
            """)