I'm working on a chatbot, that will later be used and tested in a psychological experiment. This is my first real programming project. I'm using the OpenAI API to provide the language model and Gradio for the UI. I want to add functionality, that would allow me export the conversations of each user, so that me and my team can them analyze later. I have some rough idea how this could be done, but need help with the specific implementation. This is the code I have so far, without the extra functionality:
from openai import OpenAI
import gradio as gr
client = OpenAI(
api_key="OUR API KEY"
)
instructions = "OUR SYSTEM PROMPT"
def chat(system_prompt, user_prompt, model='gpt-4', temperature=0.0):
response = client.chat.completions.create(
temperature=temperature,
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
])
res = response.choices[0].message.content
return res
def format_chat_prompt(message, chat_history, max_convo_length):
prompt = ""
for turn in chat_history[-max_convo_length:]:
user_message, bot_message = turn
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
prompt = f"{prompt}\nUser: {message}\nAssistant:"
return prompt
def respond(message, chat_history, max_convo_length=1000000):
formatted_prompt = format_chat_prompt(message, chat_history, max_convo_length)
bot_message = chat(system_prompt=f'''{instructions}''',
user_prompt=formatted_prompt,
temperature=0.7,
)
chat_history.append((message, bot_message))
return "", chat_history
with gr.Blocks() as demo:
chatbot = gr.Chatbot(height=300)
msg = gr.Textbox(label="Prompt")
btn = gr.Button("Submit")
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
gr.close_all()
demo.launch(share=True)
I have tried the following, which does create a txt file in the project directory:
from openai import OpenAI
import gradio as gr
client = OpenAI(
api_key="OUR API KEY"
)
instructions = "OUR SYSTEM PROMPT"
def chat(system_prompt, user_prompt, model='gpt-4', temperature=0.0):
response = client.chat.completions.create(
temperature=temperature,
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
])
res = response.choices[0].message.content
return res
def format_chat_prompt(message, chat_history, max_convo_length):
prompt = ""
for turn in chat_history[-max_convo_length:]:
user_message, bot_message = turn
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
prompt = f"{prompt}\nUser: {message}\nAssistant:"
return prompt
def respond(message, chat_history, max_convo_length=1000000):
formatted_prompt = format_chat_prompt(message, chat_history, max_convo_length)
bot_message = chat(system_prompt=f"{instructions}", user_prompt=formatted_prompt, temperature=0.7)
chat_history.append((message, bot_message))
export_conversation(chat_history)
return "", chat_history
def export_conversation(chat_history, filename="conversation.txt"):
with open(filename, "w") as file:
for turn in chat_history:
user_message, bot_message = turn
file.write(f"User: {user_message}\nAssistant: {bot_message}\n")
with gr.Blocks() as demo:
chatbot = gr.Chatbot(height=300)
msg = gr.Textbox(label="Prompt")
btn = gr.Button("Submit")
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #press enter to submit
gr.close_all()
demo.launch(share=True)
The problem is that the txt file only contains the single most recent conversation of the user with the most recent input. It seems clear that I need some way to identify each user. Perhaps I could then create a dictionary with IDs as keys and individual conversations as values. What would be a good, easy-to-implement, way of going about the identification? And what other parts of the code would I need to adjust for it to work?