-
Notifications
You must be signed in to change notification settings - Fork 95
/
app.py
80 lines (62 loc) · 2.7 KB
/
app.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
import os
import openai
import streamlit as st
from dotenv import load_dotenv
from render import bot_msg_container_html_template, user_msg_container_html_template
from utils import semantic_search
import prompts
import pinecone
# Set up OpenAI API key
openai.api_key = st.secrets["OPENAI_API_KEY"]
pinecone.init(api_key=st.secrets["PINECONE_API_KEY"], environment=st.secrets["PINECONE_ENVIRONMENT"])
index = pinecone.Index(st.secrets["PINECONE_INDEX_NAME"])
st.header("HormoziGPT - By Liam Ottley")
# Define chat history storage
if "history" not in st.session_state:
st.session_state.history = []
# Construct messages from chat history
def construct_messages(history):
messages = [{"role": "system", "content": prompts.system_message}]
for entry in history:
role = "user" if entry["is_user"] else "assistant"
messages.append({"role": role, "content": entry["message"]})
return messages
# Generate response to user prompt
def generate_response():
st.session_state.history.append({
"message": st.session_state.prompt,
"is_user": True
})
print(f"Query: {st.session_state.prompt}")
# Perform semantic search and format results
search_results = semantic_search(st.session_state.prompt, index, top_k=3)
print(f"Results: {search_results}")
context = ""
for i, (title, transcript) in enumerate(search_results):
context += f"Snippet from: {title}\n {transcript}\n\n"
# Generate human prompt template and convert to API message format
query_with_context = prompts.human_template.format(query=st.session_state.prompt, context=context)
# Convert chat history to a list of messages
messages = construct_messages(st.session_state.history)
messages.append({"role": "user", "content": query_with_context})
# Run the LLMChain
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
print(messages)
# Parse response
bot_response = response["choices"][0]["message"]["content"]
st.session_state.history.append({
"message": bot_response,
"is_user": False
})
# User input prompt
user_prompt = st.text_input("Enter your prompt:",
key="prompt",
placeholder="e.g. 'Write me a business plan to scale my coaching business'",
on_change=generate_response
)
# Display chat history
for message in st.session_state.history:
if message["is_user"]:
st.write(user_msg_container_html_template.replace("$MSG", message["message"]), unsafe_allow_html=True)
else:
st.write(bot_msg_container_html_template.replace("$MSG", message["message"]), unsafe_allow_html=True)