Spaces:
Runtime error
Runtime error
File size: 3,725 Bytes
9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e 7bef441 9572c0e |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import streamlit as st
try:
import dotenv
dotenv.load_dotenv()
except ImportError:
pass
import openai
import os
import streamlit.components.v1 as components
import requests
openai.api_key = os.getenv("OPENAI_API_KEY")
embedbase_api_key = os.getenv("EMBEDBASE_API_KEY")
URL = "https://api.embedbase.xyz"
local_history = []
def add_to_dataset(dataset_id: str, data: str):
response = requests.post(
f"{URL}/v1/{dataset_id}",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer " + embedbase_api_key,
},
json={
"documents": [
{
"data": data,
},
],
},
)
response.raise_for_status()
return response.json()
def search_dataset(dataset_id: str, query: str, limit: int = 3):
response = requests.post(
f"{URL}/v1/{dataset_id}/search",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer " + embedbase_api_key,
},
json={
"query": query,
"top_k": limit,
},
)
response.raise_for_status()
return response.json()
def chat(user_input: str, conversation_name: str) -> str:
local_history.append(user_input)
history = search_dataset(
f"infinite-pt-{conversation_name}",
# searching using last 4 messages from local history
"\n\n---\n\n".join(local_history[-4:]),
limit=3,
)
print("history", history)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
*[
{
"role": "assistant",
"content": h["data"],
}
for h in history["similarities"][-5:]
],
{"role": "user", "content": user_input},
],
)
message = response.choices[0]["message"]
add_to_dataset(f"infinite-pt-{conversation_name}", message["content"])
local_history.append(message)
return message["content"]
from datetime import datetime
# conversation name is date like ddmmyy_hhmmss
# conversation_name = datetime.now().strftime("%d%m%y_%H%M%S")
conversation_name = st.text_input("Conversation name", "purpose")
# eg not local dev
if not os.getenv("OPENAI_API_KEY"):
embedbase_api_key = st.text_input(
"Your Embedbase key", "get it here <https://app.embedbase.xyz/signup>"
)
openai_key = st.text_input(
"Your OpenAI key", "get it here <https://platform.openai.com/account/api-keys>"
)
openai.api_key = openai_key
user_input = st.text_input("You", "How can I reach maximum happiness this year?")
if st.button("Send"):
infinite_pt_response = chat(user_input, conversation_name)
st.markdown(
f"""
Infinite-PT
"""
)
st.write(infinite_pt_response)
components.html(
"""
<script>
const doc = window.parent.document;
buttons = Array.from(doc.querySelectorAll('button[kind=primary]'));
const send = buttons.find(el => el.innerText === 'Send');
doc.addEventListener('keydown', function(e) {
switch (e.keyCode) {
case 13:
send.click();
break;
}
});
</script>
""",
height=0,
width=0,
)
st.markdown(
"""
[Source code](https://huggingface.co/spaces/louis030195/infinite-memory-chatgpt)
"""
)
st.markdown(
"""
Built with ❤️ by [louis030195](https://louis030195.com).
"""
)
st.markdown(
"""
Powered by [Embedbase](https://embedbase.xyz).
"""
)
|