-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_chatbot_prompt_options.py
81 lines (65 loc) · 2.51 KB
/
weather_chatbot_prompt_options.py
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
# -*- coding: utf-8 -*-
"""Weather Chatbot.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1iOdUPjm4wfk5lVWOKuFjnhKR9sM9zK6q
"""
import streamlit as st
import os
import json
from langchain.agents import Tool
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain import OpenAI
from langchain.utilities import SerpAPIWrapper
from langchain.agents import initialize_agent
# Read from config.json
with open("config.json", "r") as config_file:
config_data = json.load(config_file)
open_api_key = config_data['OPENAI_API_KEY']
serp_api_key = config_data['SERPAPI_API_KEY']
# Function to initialize your chatbot
def initialize_chatbot(open_api_key, serp_api_key):
# Set API keys (already set in your environment)
os.environ["OPENAI_API_KEY"] = open_api_key
os.environ["SERPAPI_API_KEY"] = serp_api_key
file_path = r'C:\Users\307164\Desktop\Weather Chat Bot\weather_agent.json'
# Initialize tools and memory
search = SerpAPIWrapper()
tools = [
Tool(
name="Weather Forecaster",
func=search.run,
description="useful for when you need to answer questions about current and future weather forecast"
),
]
memory = ConversationBufferMemory(memory_key="chat_history")
llm = OpenAI(temperature=0)
agent_chain = initialize_agent(tools, llm=llm, verbose=True,
agent_path=file_path,
memory=memory)
return agent_chain
# Streamlit App
def main():
st.title("Weather Forecast Chatbot")
# Initialize chatbot
agent_chain = initialize_chatbot(open_api_key, serp_api_key)
# Suggested questions
suggested_questions = [
"What's the weather like today in Allentown, PA?",
"Generate a 10 day weather forecast for Providence, Rhode Island",
"Is it going to rain later today in Scranton, PA",
# ... add more questions here ...
]
# Display suggested questions as buttons
for question in suggested_questions:
if st.button(question):
st.session_state['selected_question'] = question
# User input
user_input = st.text_input("Or, ask your own question about the weather:",
value=st.session_state.get('selected_question', ''))
if user_input:
# Chatbot logic
response = agent_chain.run(input=user_input)
st.write(response)
if __name__ == "__main__":
main()