Skip to content

Commit f7ec733

Browse files
authored
Add prompts guide for Cody (#828)
This guide will help you write effective prompts to maximize your experience with Cody. It's divided into three parts: 1. **Preparation**: How you'll prepare your code for Cody 2. **Prompting**: How you will create effective prompts for Cody 3. **Example prompts**: Examples of prompts for different use cases
1 parent 3f92f37 commit f7ec733

File tree

2 files changed

+305
-0
lines changed

2 files changed

+305
-0
lines changed

docs/cody/prompts-guide.mdx

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
# Cody Prompting Guide
2+
3+
<p className="subtitle">To get the best results from Cody, whether you're exploring a codebase, summarizing a pull request, or generating code, clear and effective prompts are key. This guide will help you write effective prompts to maximize your experience with Cody.</p>
4+
5+
## Why do prompts matter?
6+
7+
Prompts are the foundation of how AI coding assistants like Cody interact with large language models (LLMs). They're not just chat inputs—they guide Cody to give precise, contextual, and actionable code suggestions.
8+
9+
While Cody handles a lot of prompt engineering under the hood, the quality of your prompts still plays a key role in shaping the results.
10+
11+
So, what defines a great prompt?
12+
13+
## Anatomy of a prompt
14+
15+
A well-crafted prompt has all the key elements to guide Cody in delivering accurate and relevant responses. You don’t need to include every element in every prompt, but understanding them can help you write more effectively.
16+
17+
Let's split these docs into three parts:
18+
19+
1. **Preparation**: How you'll prepare your code for Cody
20+
2. **Prompting**: How you will create effective prompts for Cody
21+
3. **Example prompts**: Examples of prompts for different use cases
22+
23+
## 1. Preparation
24+
25+
Before you start writing prompts, preparing your codebase for Cody is essential. Here are some key preparation steps:
26+
27+
## Treat Cody like a new team member
28+
29+
When using Cody, it's helpful to treat it like a new team member unfamiliar with your codebase. This approach ensures you provide Cody with the necessary context and information to generate accurate and contextually aware answers.
30+
31+
You should focus on providing Cody with all the necessary information, such as the codebase structure, function names, and any relevant docs. The more context and details you provide, the better Cody can assist you in generating relevant and accurate code. For example,
32+
33+
❌ Instead of a vague prompt like:
34+
35+
```
36+
How can I filter products in JavaScript?
37+
```
38+
39+
✅ Provide a more specific prompt with details:
40+
41+
```
42+
I have an array of product objects in JavaScript, each with the following properties: id, name, category, and price. How can I write a function to filter the products by category?
43+
```
44+
45+
## Define a persona or role
46+
47+
Specify a persona or role in your prompt to provide an extra layer of context to guide Cody. For example, asking Cody to act as a **beginner Python developer** can result in simpler, more beginner-friendly code snippets.
48+
49+
## Choose descriptive variable names
50+
51+
Using clear and descriptive names for variables, functions, and classes is essential for making your code readable and understandable for you and Cody. Avoid abbreviations or ambiguous names that may confuse your AI assistant.
52+
53+
✅ Good example:
54+
55+
```php
56+
function calculateTotalPrice($items, $taxRate) {
57+
// ...
58+
}
59+
```
60+
61+
❌ Bad example:
62+
63+
```php
64+
function calc($i, $t) {
65+
// ...
66+
}
67+
```
68+
69+
## Write clear code comments and docstrings
70+
71+
In addition to variable names, comments, and docstrings are crucial in guiding Cody's understanding of your code. Treat them as a way to communicate with Cody, just like you would with a new engineer. Explain complex logic, algorithms, or project-specific concepts to give Cody context.
72+
73+
✅ Good example:
74+
75+
```javascript
76+
/**
77+
* Calculate the shipping cost based on the order total.
78+
* - For orders under $50, the shipping cost is $5.
79+
* - For orders between $50 and $100, the shipping cost is $10.
80+
* - For orders above $100, shipping is free.
81+
*
82+
* @param {number} orderTotal - The total amount of the order.
83+
* @returns {number} The shipping cost, determined by the following rules:
84+
*/
85+
function calculateShippingCost(orderTotal) {
86+
// Cody will autocomplete here
87+
}
88+
```
89+
90+
A bonus here is that Cody can generate these docstrings for you, so you don't have to write them manually. You can use the **document-code** prompt to do this.
91+
92+
## @-mention context
93+
94+
Cody leverages the `@-mention` syntax to source context via files, symbols, web URLs, and more. By default, Cody will automatically detect context from the codebase you're working in via pre-filled context chips in the chat input field. Make sure that when you are working with any codebase, Cody picks up the default context. An empty context chip means Cody will search based on generally available context.
95+
96+
You can learn more about context [here](/cody/core-concepts/context).
97+
98+
## Selecting the right LLM
99+
100+
Cody offers a variety of LLMs for both chat and in-line edits by all the leading LLM providers. Each LLM has its strengths and weaknesses, so it is important to select the right one for your use case. For example, Claude 3.5 Sonnet and GPT-4o are powerful for code generation and provide accurate results. However, Gemini 1.5 Flash is a decent choice for cost-effective searches. So, you can always optimize your choice of LLM based on your use case.
101+
102+
Learn more about all the supported LLMs [here](/cody/capabilities/supported-models).
103+
104+
## 2. Prompting
105+
106+
Now that your code is well-prepared, let's focus on writing effective prompts for Cody via the following best practices:
107+
108+
## Provide specific information
109+
110+
When using Cody chat, provide as much detail as possible. Include information about the problem, expected behavior, constraints, and project-specific requirements.
111+
112+
Be sure to include comprehensive details about the problem, what you expect to happen, any limitations, and specific requirements related to your project.
113+
114+
❌ Bad example:
115+
116+
```
117+
How do I calculate discounts based on loyalty points in Laravel?
118+
```
119+
120+
✅ Good example:
121+
122+
```
123+
I need to calculate discounts based on customer loyalty points.
124+
125+
- If the customer has loyalty points above 500, apply a 10% discount.
126+
- If the customer has loyalty points between 200 and 500, apply a 5% discount.
127+
- If the customer has loyalty points below 200, no discount is applied.
128+
129+
Create a function that takes the total amount and loyalty points as input and returns an object with the discount percentage and discount amount.
130+
```
131+
132+
## Provide specific context
133+
134+
While preparing your codebase for Cody, you learned about the importance of context chips. In addition to this default context, you can provide additional and more specific context to help Cody better understand your codebase.
135+
136+
You can continue to `@-mention` files, symbols, and other context sources (as supported by your Cody tier) to make your search more specific and granular. You should approach this as if explaining the situation to a new team member. You should:
137+
138+
- Reference important files and symbols
139+
- Provide examples from other similar functions
140+
141+
## Provide examples and test cases
142+
143+
You should include examples and test cases when applicable to clarify your expectations. Demonstrate edge cases or handling of special conditions to guide Cody in generating robust code.
144+
145+
❌ Bad example:
146+
147+
```
148+
I need to validate email addresses in JavaScript
149+
```
150+
151+
✅ Good example:
152+
153+
```
154+
Create a function to validate email addresses in JavaScript. Return true for valid email addresses and false for invalid ones. Here are some example test cases:
155+
156+
Valid:
157+
158+
159+
160+
Invalid:
161+
- "john@example"
162+
- "jane.doe@example."
163+
- "invalid.email"
164+
165+
Please write the function
166+
```
167+
168+
## Iterate and refine
169+
170+
Start with a general prompt and incrementally add more details based on Cody's responses. Take advantage of the fact that you can chat with Cody. Bring the back-and-forth conversation, especially if you didn't like Cody's first response. Review the generated code or suggestions, provide feedback, and refine your prompts to get the desired results.
171+
172+
Initial prompt:
173+
174+
```
175+
I need to calculate the total price of an order. Help me write the function
176+
```
177+
178+
Refined prompt:
179+
180+
```
181+
Thanks. I forgot to mention:
182+
183+
- The function should take an array of order items as input
184+
- We need to apply a 10% discount if the total price exceeds $100
185+
- Final total should be rounded to two decimal places
186+
187+
Please update the function
188+
```
189+
190+
## Leverage Cody's capabilities
191+
192+
You can utilize many other [Cody's capabilities](/cody/capabilities) for generating boilerplate code, common patterns, and repetitive tasks. Prompt it to assist with unit tests, docs, and error handling to save time and ensure code quality.
193+
194+
✅ Good example:
195+
196+
```
197+
Help me write tests for the `calculateAverageRating` function. Here are the test cases I have in mind:
198+
199+
- Empty ratings array should return 0
200+
- Array with one rating should return that rating
201+
- Array with multiple ratings should calculate the average correctly
202+
203+
Make sure the tests cover any edge cases or potential issues.
204+
```
205+
206+
You can also use the **generate-unit-tests** prompt to generate tests for your code.
207+
208+
## Miscellaneous information
209+
210+
Try adding any supplementary details regarding comments, debugging guidelines, error management, required dependencies, or coding styles. For instance, when directing Cody to implement a database query in SQL, specify the need for parameterized queries to prevent SQL injection vulnerabilities and provide suggestions for optimizing query performance.
211+
212+
## Prompts Library
213+
214+
To accelerate and automate your work, you can leverage Cody's Prompt Library, which helps you build customizable building blocks (prompts), share your best prompts with your teammates, and enable site administrators to promote the best prompts to the rest of the organization.
215+
216+
The Prompt Library is a system for creating and sharing customizable prompts. It is explicitly designed for scalability, repeatability, and flexibility.
217+
218+
Learn more about the [Prompts and the Prompt Library here](/cody/capabilities/commands).
219+
220+
## Example Prompts
221+
222+
Let's examine some examples of good and reusable prompts that you can create via the Prompt Library.
223+
224+
<Accordion title=" 1. Prompt that specifies the language/framework version you are using">
225+
226+
This prompt can help if Cody needs to generate code in the correct language/framework version. For example,
227+
228+
>Cody generates code snippets using a newer [Spring](https://spring.io/) version that's incompatible with your legacy Java application, which runs Spring 3.x. This mismatch leads to examples utilizing features from Spring Boot 2.x that your project doesn't support.
229+
230+
The prompt to tackle this issue looks like this:
231+
232+
```
233+
Please generate Java code that is compatible with the following specifications:
234+
235+
Java version: 1.6
236+
Spring framework version: 3.x
237+
Testing framework: JUnit 4"
238+
239+
```
240+
241+
</Accordion>
242+
243+
<Accordion title="2. Prompt that instructs Cody to follow your organization's style guide">
244+
245+
This prompt can help if Cody needs to follow your organization's coding standards or style guide. For example,
246+
247+
>Your organization follows a specific Python coding style guide that mandates using snake_case for variable names and requires docstrings for all functions. However, Cody might generate code that doesn't adhere to these conventions, leading to inconsistency in your codebase.
248+
249+
The prompt to resolve this inconsistency will be:
250+
251+
```
252+
Generate Python code that adheres to our organization's style guide, which includes the following requirements:
253+
254+
Use snake_case for all variable and function names.
255+
Include docstrings for all functions, describing their purpose and parameters.
256+
Follow PEP 8 guidelines for code formatting, including indentation and line length.
257+
```
258+
259+
</Accordion>
260+
261+
<Accordion title="3. Prompt with enhanced generate-unit-tests capabilities">
262+
263+
This prompt can help you if you want to generate unit tests for your code based on a specific format. For example,
264+
265+
>You're working with Golang and need Cody to generate unit tests that comply with your team’s conventions. Your organization follows a specific structure for tests, including table-driven tests and `require` from the `testify` package for assertions. However, Cody might provide a simple test that does not align with your standards.
266+
267+
While you can use the default `generate-unit-tests` prompt but to get the desired output, you can customize e the prompt to include the following:
268+
269+
```
270+
Please generate Golang unit tests that adhere to the following conventions:
271+
272+
Use table-driven tests for different input cases.
273+
Use the require package from testify for assertions.
274+
Follow idiomatic Go test naming conventions and structure.
275+
```
276+
277+
</Accordion>
278+
279+
<Accordion title="4. An onboarding prompt for new team members">
280+
281+
This prompt can help you if you want to onboard new team members by providing them with a set of instructions or guidelines to get started with a project. For example,
282+
283+
```
284+
@repo tell me about this project and how to set it up.
285+
```
286+
287+
</Accordion>
288+
289+
<Accordion title="5. Customized code documentation prompt">
290+
291+
To encourage detailed and comprehensive docs of your codebase, you can create a prompt instructing Cody to generate documentation based on your style guide. For example,
292+
293+
```
294+
Please generate a docstring for the given Python function that adheres to our team's documentation standards. The docstring should include:
295+
1. A brief summary of the function's purpose.
296+
2. A description of each parameter, including name, type, and purpose.
297+
3. The return value, including its type and description.
298+
4. Any raised exceptions, if applicable.
299+
5. Example usage of the function, where relevant.
300+
```
301+
302+
</Accordion>
303+
304+
You can share all these prompt examples with your team members to help them get started with Cody. If you want others to make the most of these, you can also promote the best prompts.

src/data/navigation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const navigation: NavigationItem[] = [
6969
subsections: [
7070
{ title: "Context", href: "/cody/core-concepts/context", },
7171
{ title: "Token Limits", href: "/cody/core-concepts/token-limits", },
72+
{ title: "Prompts Guide", href: "/cody/prompts-guide", },
7273
// { title: "Embeddings", href: "/cody/core-concepts/embeddings", },
7374
// { title: "Keyword Search", href: "/cody/core-concepts/keyword-search", },
7475
// { title: "Code Graph", href: "/cody/core-concepts/code-graph", },

0 commit comments

Comments
 (0)