File size: 2,222 Bytes
0a3b0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from langchain.llms import OpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
import os

# Set up Streamlit interface
st.title('Weather Q&A using Langchain')
# Adding the markdown message
st.markdown("""
I'm genuinely impressed. Leveraging prompt engineering, I was able to craft this program in just 5 minutes, and it's fully functional! All I did was instruct ChatGPT to integrate langchain and streamlit, set up inputs for the API keys, pose a weather-related question, and use the details from the [LangChain OpenWeatherMap link](https://python.langchain.com/docs/integrations/tools/openweathermap) as a coding and output guide. Now, envisioning a solution is all it takes. It's auto-magical! I may have been a terrible programmer, but I\'m an amazing prompt engineer, bless the Lord!
""")

st.sidebar.header('API Configuration')

# Input for OpenAI API key and OpenWeather API key in the Streamlit sidebar
os.environ["OPENAI_API_KEY"] = st.sidebar.text_input('OpenAI API Key:', value='', type='password')
os.environ["OPENWEATHERMAP_API_KEY"] = st.sidebar.text_input('OpenWeather API Key:', value='', type='password')

# Input for question about the weather
question = st.text_input('Ask a question about the weather (e.g., "What\'s the weather like in London?"):')

# Initialize Langchain's OpenAI and agent_chain only once API keys are provided
if os.environ["OPENAI_API_KEY"] and os.environ["OPENWEATHERMAP_API_KEY"]:
    try:
        llm = OpenAI(temperature=0)
        tools = load_tools(["openweathermap-api"], llm)
        agent_chain = initialize_agent(
            tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
        )

        # If a question is provided, proceed to get an answer
        if question:
            response = agent_chain.run(question)
            st.write(response)
    except Exception as e:
        st.warning("There was an error processing your request.")
        st.write(f"Details: {e}")
        st.write("Please provide more specific information. For example, you may need to provide the country sucn as Florence Kentucky US.")
else:
    st.warning("Please provide your API keys in the left sidebar!")