forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatGPT.py
67 lines (51 loc) · 2.21 KB
/
CatGPT.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
from langchain import OpenAI, LLMChain, PromptTemplate
from langchain.chains.conversation.memory import ConversationalBufferWindowMemory
import streamlit as st
from streamlit_chat import message
def open_file(filepath):
with open(filepath, 'r', encoding='utf-8') as infile:
return infile.read()
st.header("CatGPT 🐈")
template = open_file("prompts/persona/catgpt_persona.txt")
prompt = PromptTemplate(
input_variables=["history", "human_input"],
template=template
)
# Enter your API key
OpenAI_api_key = st.text_input("**Step #1**: Enter in Your OpenAI API Key", help="Instructions on how to get an API key at https://elephas.app/blog/how-to-create-openai-api-keys-cl5c4f21d281431po7k8fgyol0")
if OpenAI_api_key != "":
# Set up LLM model with API key
llm = OpenAI(
temperature=0,
model_name='text-davinci-003',
openai_api_key=OpenAI_api_key
)
# Set up LLM model with API key
chatgpt_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=ConversationalBufferWindowMemory(k=2),
)
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
def get_text():
input_text = st.text_input("**Step #2:** Chat","Why don’t cats play poker in the jungle?", key="input")
return input_text
user_input = get_text()
if user_input:
output = chatgpt_chain.predict(human_input=user_input)
st.session_state.past.append(user_input)
st.session_state.generated.append(output)
if st.session_state['generated']:
for i in range(len(st.session_state['generated'])-1, -1, -1):
message(st.session_state["generated"][i], key=str(i), avatar_style="initials",seed="CG")
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
else:
st.info("Please enter in your OpenAI API Key. \
You can create an OpenAI API Key with your \
OpenAI account at https://openai.com/. \
Please see https://elephas.app/blog/how-to-create-openai-api-keys-cl5c4f21d281431po7k8fgyol0 \
for more information. ")