2025年4月15日火曜日

emacs の copilot-chat で選択した部分をコンテキストに追加して質問する方法

emacs の copilot-chat で選択した部分をコンテキストに追加して質問する方法

概要

前回選択中のバッファをコンテキストに追加する方法を紹介しました
今回は選択した部分をコンテキストとして追加し質問する方法を紹介します

環境

  • macOS 15.4
  • emacs 30.1

copilot-chat-custom-prompt-selection

  • 質問したい部分を選択
  • M-x copilot-chat-custom-prompt-selection
  • ミニバッファで質問する

すると Copilot chat 用のバッファで質問が返ってくるので確認します
ちゃんと選択した部分が質問のコンテキストに追加されていることを確認しましょう

* *[16:28:35]* You                 :you:
Create another Message instance and call the say method.

#+BEGIN_SRC python
from my_libs.util import Message


def main():
    # Create an instance of the Message class
    msg = Message()
    
    # Call the say method with keyword arguments
    try:
        msg.say(text="Hello, World!", sender="user@example.com", recipient="recipient@example.com")
    except ValueError:
        print("An error occurred while executing the say method.")

if __name__ == "__main__":
    main()

#+END_SRC


** *[16:28:39]* Copilot(gpt-4o)                 :copilot:
*** Create another Message instance and call the ~say~ method
To create another instance of the ~Message~ class and call the ~say~ method, you can follow the example below:

#+BEGIN_SRC python
from my_libs.util import Message


def main():
    # Create the first instance of the Message class
    msg1 = Message()
    
    # Call the say method for the first instance
    try:
        msg1.say(text="Hello, World!", sender="user@example.com", recipient="recipient@example.com")
    except ValueError:
        print("An error occurred while executing the say method for msg1.")
    
    # Create another instance of the Message class
    msg2 = Message()
    
    # Call the say method for the second instance
    try:
        msg2.say(text="Goodbye, World!", sender="admin@example.com", recipient="user@example.com")
    except ValueError:
        print("An error occurred while executing the say method for msg2.")

if __name__ == "__main__":
    main()
#+END_SRC

*** Explanation
+ The code creates two separate instances of the ~Message~ class: ~msg1~ and ~msg2~.
+ The ~say~ method is called on each instance with different arguments.
+ Error handling is included using ~try~ and ~except~ blocks to catch any ~ValueError~ exceptions that may occur during the execution of the ~say~ method.


* ╭──── Chat Input ────╮ :copilotChatInput:

最後に

コンテキストをうまく追加して自分のコードを LLM 側に認識させましょう

0 件のコメント:

コメントを投稿