Action

ChatGPT: Converse

Posted by agiletortoise, Last update 14 days ago

This action allows you to conduct an ongoing conversation with ChatGPT in a single draft. When you run the action, any lines with no prefex are treated as your input prompts, text starting with > (Markdown quotes) will be treated as previous responses from ChatGPT…and any new responses will be appended at the end of the document, leaving you ready to type your next prompt in the conversation.

An example draft might look like:

What are the three best examples of film noir?

> 1. Double Indemnity (1944) - Directed by Billy Wilder, this film is considered a quintessential example of film noir with its complex characters, moral ambiguity, and cynical view of society.
> 
> 2. The Maltese Falcon (1941) - Directed by John Huston and starring Humphrey Bogart, this classic film noir features a hard-boiled detective, a femme fatale, and a mystery surrounding a valuable object.
> 
> 3. Touch of Evil (1958) - Directed by Orson Welles, this film noir is known for its innovative cinematography, atmospheric setting, and morally ambiguous characters. It is considered one of the last great films of the classic film noir era.

Tell me more about the first one.

> "Double Indemnity" is a 1944 film directed by Billy Wilder and based on a novel by James M. Cain. The film follows a smitten insurance agent, Walter Neff (played by Fred MacMurray), who becomes involved in a plot to kill an unhappy housewife's husband in order to collect on a double indemnity insurance policy. 
> 
> Barbara Stanwyck plays the seductive Phyllis Dietrichson, the femme fatale who manipulates Neff into carrying out the murder. As the plan unravels, tension builds as the two characters try to outwit each other and avoid detection by a suspicious claims investigator, Barton Keyes (played by Edward G. Robinson).
> 
> "Double Indemnity" is considered a classic example of film noir for its shadowy cinematography, morally ambiguous characters, and themes of betrayal, greed, and lust. The film was nominated for several Academy Awards and has stood the test of time as a landmark in the film noir genre.

For more information on using this action, visit our OpenAI integration guide

Steps

  • script

    // default inputs
    const model = "gpt-3.5-turbo"
    const system = "You are a helpful assistant"
    const chatResponsePrefix = "> "
    const initialText = editor.getText().trim()
    
    // splits the text into messages for OpenAI's API
    function parseChat(s) {
    	let messages = []
    	for (let line of s.split("\n")) {
    		let ln = line.trim()
    		if (ln.length == 0) { continue }
    		if (ln.startsWith(chatResponsePrefix)) { // assistant text
    			messages.push({
    				"role": "assistant",
    				"content": ln.slice(chatResponsePrefix.length)
    			})
    		}
    		else { // user message
    			messages.push({
    				"role": "user",
    				"content": ln
    			})
    		}
    	}
    	return messages
    }
    
    let f = () => {
    	let messages = parseChat(initialText)
    	if (messages.length == 0) {
    		alert("No input. Please type some text to submit in the editor.")
    		return false
    	}
    
    	let ai = new OpenAI()
    	ai.model = model
    	let response = ai.request({  
            "path": "/chat/completions",  
            "method": "POST",  
            "data": {  
                "model": model,  
                "messages": messages
            }  
        })
    	if (!response.success) {
    		console.log(`ERROR: ${response.statusCode}
    ${response.responseText}`)
    		context.fail()
    		return false
    	}
    
    	let newMessage = response.responseData.choices[0].message.content
    	let newContent = `${initialText}
    	
    ${newMessage.split("\n").map(ln => `${chatResponsePrefix}${ln}`).join("\n")}
    
    `
    	editor.setText(newContent)
    	editor.activate()
    	editor.setSelectedRange(100000, 0) // move cursor to end
    	return true
    }
    
    if (!f()) {
    	context.cancel()
    }
    
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.