File size: 1,095 Bytes
05cbd63
 
1aaf699
 
05cbd63
1aaf699
 
05cbd63
1e26e8a
05cbd63
f0ef76e
05cbd63
 
 
 
 
 
 
 
1e26e8a
05cbd63
18be5f3
05cbd63
 
 
 
 
 
 
 
 
 
 
 
 
f0ef76e
05cbd63
 
f0ef76e
05cbd63
 
 
 
 
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
import os
import time

import streamlit as st
from langchain.llms import openai

def get_openai_response(question):
    try:
        api_key = os.environ["OPENAI_API_KEY"]  
        llm = openai.OpenAI(
            model_name="text-davinci-003", temperature=0.5, openai_api_key=api_key
        )
        with st.spinner("Generating response..."):
            response = llm(question)
        return response
    except Exception as e:
        st.error(f"Error: {e}")
        return None


st.set_page_config(page_title="Enhanced Q&A Demo")
st.header("Xoxo-Langchain App")

input_text = st.text_input("Ask me anything:", key="input")
submit_button = st.button("Get Answer")

response_history = st.empty()

if submit_button:
    with st.spinner("Processing..."):
        response = get_openai_response(input_text)

    if response:
        st.subheader("The answer is:")
        st.write(response)

        # Add response to history
        response_history.code(f"""
{input_text}
**Answer:** {response}
---
        """)
    else:
        st.error("Something went wrong. Please try again later.")