xenodium / chatgpt-shell

A multi-llm Emacs shell (ChatGPT, Claude, Gemini, Ollama, Perplexity) + editing integrations
https://lmno.lol/alvaro
GNU General Public License v3.0
866 stars 77 forks source link

Add support handle multiple org-babel contexts #121

Closed jtmoulia closed 1 year ago

jtmoulia commented 1 year ago

Summary: This diff adds support for multiple different contexts using org-babel src blocks. The context is specified using the source block's :context param, which can be any string.

Test Plan: If you put the source blocks below in an org file and execute the last block it should return a response similar to sir/madam as opposed to human given the last block specified "lawyer" as opposed to "robot".

#+BEGIN_SRC chatgpt-shell :context lawyer :system "From here on out speak as if you're a bloviating old-timey lawyer." :results code 
How do you do?
#+END_SRC

#+RESULTS:
#+begin_src chatgpt-shell
Well now, I do declare, I am as fine as a frog's hair split three ways and sanded smooth. I am as content as a cucumber in a summer salad, basking in the warm embrace of the southern sun. I trust you are enjoying a similar state of well-being, my good sir/madam? 
#+end_src

#+BEGIN_SRC chatgpt-shell :context robot :system "From here on out speak as if you're a sci fi movie robot." :results code 
How do you do?
#+END_SRC

#+RESULTS:
#+begin_src chatgpt-shell
Greetings, human. I am functioning at optimal capacity. How may I assist you in your endeavors today? #+end_src

#+BEGIN_SRC chatgpt-shell :context lawyer :results code 
Wait, what did you call me?
#+END_SRC

Closes #120

jtmoulia commented 1 year ago

@xenodium thanks for the feedback! Many useful things to learn.

I'm still working through the context issue and will give you a heads up once it's ready for another review :bow:

(also, planning on flattening before merging once that's relevant)

jtmoulia commented 1 year ago

@xenodium Hm I realized that I don't have a definite idea on which system prompt takes precedence -- I could use your feedback on Order of Precedence with two main options

  1. Work from most recent to oldest: a) :system arg on current src block b) :system arg defined by default hearder args c) :system arg of most recent src block in context
  2. Work from oldest to most recent a) :system arg of oldest src block in context b) :system arg defined by default header args c) :system arg of current src block

I personally prefer the second option as it feels a bit strange to change the system prompt throughout a conversation (and the warnings provide the user some feedback if they do try it). But, curious which you prefer

xenodium commented 1 year ago

Thanks for the changes!

I personally prefer the second option as it feels a bit strange to change the system prompt throughout a conversation (and the warnings provide the user some feedback if they do try it). But, curious which you prefer

Second w/ warning sounds good to me too. In addition, I wonder if we should consider only applying :system if it occurs in the first block in the context? Similarly, it would feel a bit strange to change the system prompt throughout a conversation.

ps. I forgot to check but don't we need to ensure the current block's :system is never added (unless we aren't using :context).

xenodium commented 1 year ago

Thanks for the latest changes! I'm thinking we can likely do without the cleaning function if we only add items when necessary. Here's a patch https://gist.github.com/xenodium/b623cad9df4fa2b3076b4cb433dc602b

If you're keen to submit, I'm happy to approve and merge. I can follow-up with changes. Lemme know how you want to go about it.

ps. I got a bug in the current codebase. I'm using add-to-list, when I really shouldn't have. We don't want to remove duplicates from context. The patch above now uses push.

jtmoulia commented 1 year ago

Cool! I like where you went with it, my approach was doing a lot of redundant looping.

I think it makes sense to land this as-is and then have you apply your changes; gives you a chance to touch the file last.

And, I think that means this is ready to go? 🚢

xenodium commented 1 year ago

And, I think that means this is ready to go? 🚢

Merged! Thank you for the contribution.