1# Language model integration
2
3## Assistant Panel
4
5The 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`).
6
7> **Note**: A default binding can be set to toggle the right dock via the settings.
8
9### Setup
10
111. Create an [OpenAI API key](https://platform.openai.com/account/api-keys)
122. Make sure that your OpenAI account has credits
133. Open the assistant panel, using either the `assistant: toggle focus` or the `workspace: toggle right dock` action in the command palette (`cmd-shift-p`).
144. Make sure the assistant panel is focused:
15
16 
17
185. Open the command palette (`cmd-shift-p`) and use the now-available `assistant: reset key` action to set your OpenAI API key:
19 
20
21The OpenAI API key will be saved in your keychain.
22
23Zed 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`.
24
25### Having a conversation
26
27The 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:
28
29- `You`
30- `Assistant`
31- `System`
32
33To begin, select a model and type a message in a `You` block.
34
35
36
37As you type, the remaining tokens count for the selected model is updated.
38
39Inserting 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.
40
41
42
43To 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 new line.
44
45After submitting a message, the assistant's response will be streamed below, in an `Assistant` message block.
46
47
48
49The stream can be canceled at any point with `escape`. This is useful if you realize early on that the response is not what you were looking for.
50
51If you want to start a new conversation at any time, you can hit `cmd-n` or use the `New Context` menu option in the hamburger menu at the top left of the panel.
52
53Simple 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.
54
55### Editing a conversation
56
57The 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:
58
591. Write text in a `You` block.
602. Submit the message with `cmd-enter`
613. Receive an `Assistant` response that doesn't meet your expectations
624. Cancel the response with `escape`
635. Erase the content of the `Assistant` message block and remove the block entirely
646. Add additional context to your original message
657. Submit the message with `cmd-enter`
66
67Being able to edit previous messages gives you control over how tokens are used. You don't need to start up a new context to correct a mistake or to add additional context and you don't have to waste tokens by submitting follow-up corrections.
68
69Some additional points to keep in mind:
70
71- You are free to change the model type at any point in the conversation.
72- 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.
73
74### Saving and loading conversations
75
76After 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.
77
78
79
80### Using a custom API endpoint for OpenAI
81
82You can use a custom API endpoint for OpenAI, as long as it's compatible with the OpenAI API structure.
83
84To do so, add the following to your Zed `settings.json`:
85
86```json
87{
88 "language_models": {
89 "openai": {
90 "api_url": "http://localhost:11434/v1"
91 }
92 }
93}
94```
95
96The custom URL here is `http://localhost:11434/v1`.
97
98### Using Ollama on macOS
99
100You can use Ollama with the Zed assistant by making Ollama appear as an OpenAPI endpoint.
101
1021. Download, for example, the `mistral` model with Ollama:
103
104 ```
105 ollama pull mistral
106 ```
107
1082. Make sure that the Ollama server is running. You can start it either via running the Ollama app, or launching:
109
110 ```
111 ollama serve
112 ```
113
1143. In the assistant panel, select one of the Ollama models using the model dropdown.
1154. (Optional) If you want to change the default url that is used to access the Ollama server, you can do so by adding the following settings:
116
117```json
118{
119 "language_models": {
120 "ollama": {
121 "api_url": "http://localhost:11434"
122 }
123 }
124}
125```
126
127### Using Claude 3.5 Sonnet
128
129You can use Claude with the Zed assistant by choosing it via the model dropdown in the assistant panel.
130
131You need can obtain an API key [here](https://console.anthropic.com/settings/keys).
132
133Even if you pay for Claude Pro, you will still have to [pay for additional credits](https://console.anthropic.com/settings/plans) to use it via the API.
134
135## Inline generation
136
137You can generate and transform text in any editor by selecting text and pressing `ctrl-enter`.
138You can also perform multiple generation requests in parallel by pressing `ctrl-enter` with multiple cursors, or by pressing `ctrl-enter` with a selection that spans multiple excerpts in a multibuffer.
139To create a custom keybinding that prefills a prompt, you can add the following format in your keymap:
140
141```json
142[
143 {
144 "context": "Editor && mode == full",
145 "bindings": {
146 "ctrl-shift-enter": [
147 "assistant::InlineAssist",
148 { "prompt": "Build a snake game" }
149 ]
150 }
151 }
152]
153```
154
155## Advanced: Overriding prompt templates
156
157Zed allows you to override the default prompts used for various assistant features by placing custom Handlebars (.hbs) templates in your `~/.config/zed/prompts/templates` directory. The following templates can be overridden:
158
1591. `content_prompt.hbs`: Used for generating content in the editor.
160 Format:
161
162 ```handlebars
163 You are an AI programming assistant. Your task is to
164 {{#if is_insert}}insert{{else}}rewrite{{/if}}
165 {{content_type}}{{#if language_name}} in {{language_name}}{{/if}}
166 based on the following context and user request. Context:
167 {{#if is_truncated}}
168 [Content truncated...]
169 {{/if}}
170 {{document_content}}
171 {{#if is_truncated}}
172 [Content truncated...]
173 {{/if}}
174
175 User request:
176 {{user_prompt}}
177
178 {{#if rewrite_section}}
179 Please rewrite the section enclosed in
180 <rewrite_this></rewrite_this>
181 tags.
182 {{else}}
183 Please insert your response at the
184 <insert_here></insert_here>
185 tag.
186 {{/if}}
187
188 Provide only the
189 {{content_type}}
190 content in your response, without any additional explanation.
191 ```
192
1932. `terminal_assistant_prompt.hbs`: Used for the terminal assistant feature.
194 Format:
195
196 ```handlebars
197 You are an AI assistant for a terminal emulator. Provide helpful responses to
198 user queries about terminal commands, file systems, and general computer
199 usage. System information: - Operating System:
200 {{os}}
201 - Architecture:
202 {{arch}}
203 {{#if shell}}
204 - Shell:
205 {{shell}}
206 {{/if}}
207 {{#if working_directory}}
208 - Current Working Directory:
209 {{working_directory}}
210 {{/if}}
211
212 Latest terminal output:
213 {{#each latest_output}}
214 {{this}}
215 {{/each}}
216
217 User query:
218 {{user_prompt}}
219
220 Provide a clear and concise response to the user's query, considering the
221 given system information and latest terminal output if relevant.
222 ```
223
2243. `edit_workflow.hbs`: Used for generating the edit workflow prompt.
225
2264. `step_resolution.hbs`: Used for generating the step resolution prompt.
227
228You can customize these templates to better suit your needs while maintaining the core structure and variables used by Zed. Zed will automatically reload your prompt overrides when they change on disk. Consult Zed's assets/prompts directory for current versions you can play with.
229
230Be sure you want to override these, as you'll miss out on iteration on our built in features. This should be primarily used when developing Zed.