Spaces:
Paused
Paused
Add forgetting long-term history
Browse files- app.py +27 -2
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import openai
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
|
|
@@ -9,10 +10,10 @@ def ask(question, history):
|
|
| 9 |
try:
|
| 10 |
response = openai.ChatCompletion.create(
|
| 11 |
model="gpt-3.5-turbo",
|
| 12 |
-
messages=[
|
| 13 |
{"role":"user" if i%2==0 else "assistant", "content":content}
|
| 14 |
for i,content in enumerate(history)
|
| 15 |
-
]
|
| 16 |
)["choices"][0]["message"]["content"]
|
| 17 |
while response.startswith("\n"):
|
| 18 |
response = response[1:]
|
|
@@ -25,6 +26,30 @@ def ask(question, history):
|
|
| 25 |
return history
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
import gradio as gr
|
| 30 |
|
|
|
|
| 1 |
import openai
|
| 2 |
+
import tiktoken
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 10 |
try:
|
| 11 |
response = openai.ChatCompletion.create(
|
| 12 |
model="gpt-3.5-turbo",
|
| 13 |
+
messages=forget_long_term([
|
| 14 |
{"role":"user" if i%2==0 else "assistant", "content":content}
|
| 15 |
for i,content in enumerate(history)
|
| 16 |
+
])
|
| 17 |
)["choices"][0]["message"]["content"]
|
| 18 |
while response.startswith("\n"):
|
| 19 |
response = response[1:]
|
|
|
|
| 26 |
return history
|
| 27 |
|
| 28 |
|
| 29 |
+
def forget_long_term(messages, max_num_tokens=4000):
|
| 30 |
+
def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
|
| 31 |
+
"""Returns the number of tokens used by a list of messages."""
|
| 32 |
+
try:
|
| 33 |
+
encoding = tiktoken.encoding_for_model(model)
|
| 34 |
+
except KeyError:
|
| 35 |
+
encoding = tiktoken.get_encoding("cl100k_base")
|
| 36 |
+
if model == "gpt-3.5-turbo": # note: future models may deviate from this
|
| 37 |
+
num_tokens = 0
|
| 38 |
+
for message in messages:
|
| 39 |
+
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
|
| 40 |
+
for key, value in message.items():
|
| 41 |
+
num_tokens += len(encoding.encode(value))
|
| 42 |
+
if key == "name": # if there's a name, the role is omitted
|
| 43 |
+
num_tokens += -1 # role is always required and always 1 token
|
| 44 |
+
num_tokens += 2 # every reply is primed with <im_start>assistant
|
| 45 |
+
return num_tokens
|
| 46 |
+
else:
|
| 47 |
+
raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
|
| 48 |
+
See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
|
| 49 |
+
while num_tokens_from_messages(messages)>max_num_tokens:
|
| 50 |
+
messages = messages[1:]
|
| 51 |
+
return messages
|
| 52 |
+
|
| 53 |
|
| 54 |
import gradio as gr
|
| 55 |
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
openai==0.27.0
|
|
|
|
|
|
| 1 |
+
openai==0.27.0
|
| 2 |
+
tiktoken==0.3.0
|