You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks a lot for this package. I think it would be great to add commands into this REPL mode.
I would suggest using the commands I am using in the following Python script:
#!/usr/bin/env python3importopenaiimportpyperclipimportosimportplatform# Function to return a string in bolddefbold(string):
returnf"\033[1m{string}\033[0m"# Function to return a string in italicsdefitalic(string):
returnf"\033[3m{string}\033[0m"# Function to centre the text in the terminal windowdefcentre(string):
# Get the width of the terminal windowwidth=os.get_terminal_size().columns# If the string is longer than the width of the terminal window, return the string as isiflen(string) >=width:
returnstring# Calculate the number of spaces to add to the left and right of the stringpadding= (width-len(string)) //2# Return the string with the calculated padding on either sidereturn" "*padding+stringuser_prompt=bold('You:') +'\n'ai_prompt=bold('AI assistant:') +'\n'# Set up OpenAI API keywithopen("/home/diego/.llave") asfh:
api_key=fh.readline().strip()
openai.api_key=api_key# Function to send a message to the OpenAI chatbot model and return its responsedefsend_message(message_log):
# Use OpenAI's ChatCompletion API to get the chatbot's responseresponse=openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301", # The name of the OpenAI chatbot model to usemessages=message_log, # The conversation history up to this point, as a list of dictionariesstop=None, # The stopping sequence for the generated response, if any (not used here)temperature=0.7, # The "creativity" of the generated response (higher temperature = more creative)
)
# Find the first response from the chatbot that has text in it (some responses may not have text)forchoiceinresponse.choices:
if"text"inchoice:
returnchoice.text# If no response with text is found, return the first response's content (which may be empty)returnresponse.choices[0].message.content# If the user types "quit", end the loop and print a goodbye messagedefquit_check(user_input):
ifuser_inputin ["q", "quit"]:
print(f"{bold('Goodbye!')}\n\n")
returnTruereturnFalse# Create a markdown string from the conversation historydefcreate_markdown(message_log):
markdown=""formessageinmessage_log:
ifmessage["role"] !="system":
ifmessage["role"] =="user":
markdown+=f"You\n---\n\n"elifmessage["role"] =="assistant":
markdown+=f"ChatGPT\n-------\n\n"markdown+=f"{message['content']}\n\n"returnmarkdowndefinteraction(message_log, memory):
user_input=multi_input(f"{user_prompt} ")
ifnotuser_input:
returnTrueifquit_check(user_input):
returnFalseifuser_inputin ["m", "memory"]:
memory=notmemoryifmemory:
print(italic("( memory enabled )\n"))
else:
print(italic("( memory disabled )\n"))
returnTrueifnotmemoryoruser_inputin ["d", "delete"]:
message_log.clear()
message_log.extend(initialize_conversation())
returnTrueifuser_inputin ["c", "copy"]:
markdown=create_markdown(message_log)
pyperclip.copy(markdown)
print(italic("( markdown copied to clipboard )\n"))
returnTrueifuser_inputin ["l", "last", "copy last"]:
markdown= [message_log[-1]]
pyperclip.copy(markdown)
print(italic("( last response copied to clipboard )\n"))
returnTrueifuser_input.startswith("save ") oruser_input.startswith("s "):
ifuser_input.startswith("save "):
user_input=user_input.replace("save ", "s ")
filename=user_input[2:].strip()
ifnotfilename.endswith(".md"):
filename+=".md"basename=filename.replace(".md", "")
markdown=f"{basename}\n{'='*len(basename)}\n\n"markdown+=create_markdown(message_log)
filename=filename.replace(" ", "_")
withopen(filename, 'w') asfh:
fh.write(markdown)
print(italic(f"( markdown saved to {filename} )\n"))
returnTrueifuser_inputin ["r", "regenerate"]:
# delete the last message from the logmessage_log.pop()
else:
message_log.append({"role": "user", "content": user_input})
print(ai_prompt)
# Send the conversation history to the chatbot and get its responseresponse=send_message(message_log)
# Add the chatbot's response to the conversation history and print it to the consolemessage_log.append({"role": "assistant", "content": response})
print(f"{response}\n")
returnTrue# Function to replace input that accept multiple lines (ending with two blank# lines instead of enter)defmulti_input(prompt, n_lines=1):
lines= []
continue_prompt=Trueempty_lines=0print(prompt)
whilecontinue_prompt:
line=input('')
ifline:
lines.append(line)
else:
empty_lines+=1ifempty_lines==n_linesandlines:
continue_prompt=Falsereturn''.join(lines)
defshow_header():
print()
print(centre(bold('ChatGPT')))
print()
print(f'Type {bold("m")} or {bold("memory")} to toggle memory (enabled by default).')
print(f'Type {bold("r")} or {bold("regenerate")} to regenerate the last response.')
print(f'Type {bold("d")} or {bold("delete")} to delete the history.')
print(f'Type {bold("c")} or {bold("copy")} to copy the conversation history.')
print(f'Type {bold("l")} or {bold("last")} or {bold("copy last")} to copy the last response.')
print(f'Type {bold("s")} or {bold("save")}{italic("<filename.md>")} to save the conversation.')
print(f'Type {bold("q")} or {bold("quit")} to exit.')
print(f'Type enter two times to send the message.')
print()
definitialize_conversation():
return [{
"role": "system",
"content": "You are a helpful assistant."
}]
# Main function that runs the chatbotdefmain():
show_header()
# Initialize the conversation history with a message from the chatbotmessage_log=initialize_conversation()
memory=Truekeep_running=True# Start a loop that runs until the user types "quit" or "q"whilekeep_running:
keep_running=interaction(message_log, memory)
# Call the main function if this file is executed directly (not imported as a module)if__name__=="__main__":
main()
The text was updated successfully, but these errors were encountered:
I think having these commands would be very useful indeed!
Term.jl could be used to print out a nicely styled header message when the REPL mode is activated outlining the instructions 😁
Hi!
Thanks a lot for this package. I think it would be great to add commands into this REPL mode.
I would suggest using the commands I am using in the following Python script:
The text was updated successfully, but these errors were encountered: