gptel icon indicating copy to clipboard operation
gptel copied to clipboard

conversion from markdown to org-mode can distort the response especially with `

Open armindarvish opened this issue 1 year ago • 9 comments

Hi,

Following some discussion on Reddit, I am opening this issue to collect the examples of problems with org-mode conversion. Unfortunately, it is hard to recreate issues because chatGPT response is not fixed. But below is an example I found from previous chats. As you can see it converted ``` to === in the begining of the first code block and from there the code blocks are all screwed up.

I don't know the date and time of this specific chat so cannot tell you if this specific one is still a problem or not. Also, my guess is (and that is just a guess) that this is hapenning because of a stream response that can be tricky to deal with (Did I just recieve a single ` in some inline code or is it ``` that is coming in a stream for a code block). I haven't digged deeper in the code to see how you are dealing with stream conversions but so far it seems that it happens especially when the source does not include the language and with backquotes in elisp.

I'll try to see if I create some more examples, and may be others can add their examples if they come across any.

Here is the example:

Prompt: Can you tell me how to make an emacs-lisp code to use chatgpt 3.5 model API?

Response (converted to org-mode):

Certainly! Here are the steps to follow to make an emacs-lisp code to use chatgpt 3.5 model API:

  1. First, you need to obtain an API key from OpenAI for chatgpt 3.5 model. This requires creating an account on their website and following their instructions for obtaining an API key.

  2. Once you have the API key, you can use the built-in URL and HTTP libraries in Emacs to create and send POST requests to the OpenAI API. Here is an example code to get you started:

=== (require 'url) (require 'json)

(defun chatgpt (text) "Send a text message to the chatgpt 3.5 model API and return the response." (let* ((url-request-method "POST") (url-request-extra-headers =(("Authorization" . ,(concat "Bearer " "")) ("Content-Type" . "application/json"))) (url-request-data (json-encode =(("model" . "davinci") ("prompt" . ,text))))) (with-current-buffer (url-retrieve-synchronously "https://api.openai.com/v1/engines/davinci-codex/completions") (goto-char (point-min)) (re-search-forward "\n\n") (json-read-object)))) #+end_src

  1. This code defines a function =chatgpt= that takes a =text= argument, sends a JSON request to the OpenAI API, and returns the response as a JSON object. To test the function, you can call it with a sample text message like this:

#+end_src (chatgpt "Hello, how are you?") #+end_src

  1. This should return a JSON object containing the response from the chatgpt 3.5 model API. You can then analyze and use the response in your Emacs Lisp code as needed.

I hope this helps! Let me know if you have any other questions.

armindarvish avatar Jun 23 '23 09:06 armindarvish