assistant-panel.md

 1# Assistant Panel
 2
 3The assistant panel provides you with a way to interact with OpenAI's large language models. The assistant is good for various tasks, such as generating code, asking questions about existing code, and even writing plaintext, such as emails and documentation. To open the assistant panel, toggle the right dock by using the `workspace: toggle right dock` action in the command palette (`cmd-shift-p`).
 4
 5_Note: A default binding can be set to toggle the right dock via the settings._
 6
 7## Setup
 8
 91. Create an [OpenAI API key](https://platform.openai.com/account/api-keys)
102. Make sure that your OpenAI account has credits
113. Open the assistant panel, using either the `assistant: toggle focus` or the `workspace: toggle right dock` action in the command palette (`cmd-shift-p`).
124. Make sure the assistant panel is focused:
13
14   ![The focused assistant panel](https://zed.dev/img/assistant/assistant-focused.png)
15
165. Open the command palette (`cmd-shift-p`) and use the now-available `assistant: reset key` action to set your OpenAI API key:
17   ![Enter your OpenAI API key into the field on the right and hit return](https://zed.dev/img/assistant/assistant-reset-key.png)
18
19The OpenAI API key will be saved in your keychain.
20
21Zed will also use the `OPENAI_API_KEY` environment variable if it's defined. If you need to reset your OpenAI API key, focus on the assistant panel and run the command palette action `assistant: reset key`.
22
23## Having a conversation
24
25The assistant editor in Zed functions similarly to any other editor. You can use custom key bindings and work with multiple cursors, allowing for seamless transitions between coding and engaging in discussions with the language models. However, the assistant editor differs with the inclusion of message blocks. These blocks serve as containers for text that correspond to different roles within the conversation. These roles include:
26
27- `You`
28- `Assistant`
29- `System`
30
31To begin, select a model and type a message in a `You` block.
32
33![Asking a question](https://zed.dev/img/assistant/ask-a-question.png)
34
35As you type, the remaining tokens count for the selected model is updated.
36
37Inserting text from an editor is as simple as highlighting the text and running `cmd->` (`assistant: quote selection`); Zed will wrap it in a fenced code block if it is code.
38
39![Quoting a selection](https://zed.dev/img/assistant/quoting-a-selection.png)
40
41To submit a message, use `cmd-enter` (`assistant: assist`). Unlike typical chat applications where pressing `enter` would submit the message, in the assistant editor, our goal was to make it feel as close to a regular editor as possible. So, pressing `enter` simply inserts a newline.
42
43After submitting a message, the assistant's response will be streamed below, in an `Assistant` message block.
44
45![Receiving an answer](https://zed.dev/img/assistant/receiving-an-answer.png)
46
47The stream can be cancelled at any point with `escape`. This is useful if you realize early on that the response is not what you were looking for.
48
49If you want to start a new conversation at any time, you can use the `New Conversation` button located at the top-right corner of the assistant panel.
50
51Simple back-and-forth conversations work well with the assistant. However, there may come a time when you want to modify the previous text in the conversation and steer it in a different direction.
52
53## Editing a conversation
54
55The assistant gives you the flexibility to have control over the conversation. You can freely edit any previous text, including the responses from the assistant. If you want to remove a message block entirely, simply place your cursor at the beginning of the block and use the `delete` key. A typical workflow might involve making edits and adjustments throughout the conversation to refine your inquiry or provide additional context. Here's an example:
56
571. Write text in a `You` block.
582. Submit the message with `cmd-enter`
593. Receive an `Assistant` response that doesn't meet your expectations
604. Cancel the response with `escape`
615. Erase the content of the `Assistant` message block and remove the block entirely
626. Add additional context to your original message
637. Submit the message with `cmd-enter`
64
65Being able to edit previous messages gives you control over how tokens are used. You don't need to start up a new conversation to correct a mistake or to add additional context and you don't have to waste tokens by submitting follow-up corrections.
66
67Some additional points to keep in mind:
68
69- You are free to change the model type at any point in the conversation.
70- You can cycle the role of a message block, by clicking on the role, which is useful when you receive a response in an `Assistant` block that you want to edit and send back up as a `You` block.
71
72## Saving and loading conversations
73
74After you submit your first message, a name for your conversation is generated by the language model, and the conversation is automatically saved to your file system in `~/.config/zed/conversations`. You can access and load previous messages by clicking on the hamburger button in the top-left corner of the assistant panel.
75
76![Viewing assistant history](https://zed.dev/img/assistant/assistant-history.png)
77
78## Multiple cursor demo
79
80The assistant is capable of sending multiple requests, and receiving multiple responses, in parallel. [Here's a demo](https://zed.dev/img/post/assistant/demo.webm).