File size: 1,550 Bytes
0809507
 
 
0e8a2bc
 
0809507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1230ae3
 
0809507
 
 
 
 
 
1230ae3
0809507
1230ae3
 
 
 
 
 
 
0809507
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

import requests
import streamlit as st

from models import bloom

st.title("Welcome to RegBotBeta")
st.header("Powered by `LlamaIndex🦙` and `OpenAI API`")

def validate(token: str):
    api_endpoint = "https://api.openai.com/v1/chat/completions"
    api_key = token

    headers = {
        "Content-Type" : "application/json",
        "Authorization": f"Bearer {api_key}"
    }

    messages = [
        {"role": "user", "content": "Say this is a test!"}
    ]

    data = {
        "model": "gpt-3.5-turbo",
        "messages": messages
    }

    response = requests.post(api_endpoint, json=data, headers=headers)
    return response

def create_index():
    index = bloom.initialize_index("bloomLlama")
    return index

def get_response(vector_index, query_str):
    query_engine = vector_index.as_query_engine()
    response = query_engine.query(query_str)
    return response


api_key = st.text_input("Enter your OpenAI API key here:", type="password")
if api_key:
    resp = validate(api_key)
    if ("error" in resp.json()):
        st.info("Your API Token is incorrect! Try again.")
    else:
        os.environ["OPENAI_API_KEY"] = api_key
        index = create_index()

st.write("---")
input_text = st.text_area("Ask your question")

if input_text is not None:
    if st.button("Ask"):
        st.info("Your query: \n" + input_text)
        with st.spinner("Processing your query..."):
            response = get_response(index, input_text)
            print(response)

        st.success(response)

        st.write("---")