Reputation: 63
I wanted to make a smart assistant capable of storing all historical conversations that I have with it.
My idea is to be able to have long discussions that can be stored and retrieved over time as I expand some topics that I want to research.
I have a directory with the following files:
This is the code I am using to recurrently place the memory at the beginning of the prompt, prior to entering it to the model:
import openai
import os
from PIL import Image
import requests
import os
openAIKey='APIKEY'
prompt="\n Hi Duncan. You don't seem to be talking. Can you try saying something else?"
def talk(prompt,n=1,flex=0.5,flex_type='temp',memory=False,record_memory=False,memory_address=r'G:\My Drive\0- Personal\07- Duncan\memory',verbose=True):
openai.api_key=openAIKey
model_engine = "text-davinci-003"
# Retrieve memory
os.chdir(memory_address)
memory_txt=''
if memory:
filelist = os.listdir()
for filename in filelist:
with open(filename) as f:
file = open(filename, "r", encoding='utf-8')
contents = file.read()
memory_txt += contents
'''
elif os.path.isfile(os.path.join(memory_address, 'memory.txt')):
with open(os.path.join(memory_address, 'memory.txt')) as f:
memory_txt = f.read()
'''
if memory:
prompt = memory_txt + prompt
if flex_type=='temp':
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1012,
n=n,
stop=None,
temperature=flex)
elif flex_type=='top_n':
print('top_n Pending...')
response_array=[]
for i in range(n):
response = completion.choices[i].text
response_array.append(response)
if verbose==True:
print()
print(response, end='\n----------------------------------------------------------------')
if record_memory:
with open(os.path.join(memory_address, 'memory.txt'), 'a') as f:
f.write(response + '\n')
return response_array
if __name__=='__main__':
memory=True
record_memory=True
flex_type='temp'
n=1
flex=0.5
verbose=True
memory_address=r'G:\My Drive\0- Personal\07- Duncan\memory'
prompt='Welcome Duncan. I am so happy to meet you. \nHi Duncan! I am testing your talking function. What memories can you read?'
ra=talk(prompt=prompt,n=n,flex=flex,flex_type=flex_type,memory=memory,memory_address=memory_address,verbose=verbose)
However it does not seem to be very responsive, and after some interactions it stops giving any answers.
Any idea how this could be improve?
Upvotes: 0
Views: 1225
Reputation: 2194
RAG is your answer. There are many ways to implement RAG.
if you are using OpenAI, use the storage feature that comes along with it. If you have an OpenAI account, search here
If you are using OpenAI from Azure, you can create an "AI Search" service, upload your content there, and configure Azure OpenAI service to point to the Azure search index to refer your content. Link
If you are trying it out locally, use the semantic kernel Text embeddings and Memory APIs to create a local version of embeddings using your data and ask LLM to answer from them. Link
There are also other options to use CosmosDB, MongoDB etc.,
Upvotes: 0
Reputation: 11
I believe openai.ChatCompletion.create
is what you are seeking for: ChatCompletion introduction
Upvotes: 0