Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 262ab79

Browse files
saritaihubwritersubatoitspascoalGeekTrainer
authored
[DO NOT MERGE] Official megabranch of "Copilot Chat Cookbook" with new map topic structure (#53046)
Co-authored-by: hubwriter <[email protected]> Co-authored-by: Ben Ahmady <[email protected]> Co-authored-by: Tiago Pascoal <[email protected]> Co-authored-by: Christopher Harrison <[email protected]> Co-authored-by: Sophie <[email protected]> Co-authored-by: Sunbrye Ly <[email protected]> Co-authored-by: sunbrye <[email protected]> Co-authored-by: Hector Alfaro <[email protected]> Co-authored-by: Siara <[email protected]>
1 parent 286327d commit 262ab79

34 files changed

Lines changed: 1999 additions & 22 deletions
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Debugging invalid JSON
3+
shortTitle: Debug invalid JSON
4+
intro: '{% data variables.product.prodname_copilot_chat_short %} can identify and resolve syntax errors or structural issues in JSON data.'
5+
versions:
6+
feature: copilot
7+
category:
8+
- 'Debugging code'
9+
complexity:
10+
- Intermediate
11+
octicon: bug
12+
topics:
13+
- Copilot
14+
---
15+
16+
When working with JSON data, you may encounter issues such as trailing commas, mismatched braces, or incorrect data types that make the JSON invalid. {% data variables.product.prodname_copilot_chat %} can help you debug and fix these errors by suggesting corrections to fix invalid JSON.
17+
18+
## Example scenario
19+
20+
Consider a scenario where an application consumes JSON data from an API, but the response fails to parse due to invalid formatting. You receive the error message:
21+
22+
```bash
23+
Error: Parse error
24+
----------------------^
25+
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
26+
```
27+
28+
Below is the JSON data that caused the error:
29+
30+
```json
31+
{
32+
"location": "San Francisco",
33+
"current_weather": {
34+
"temperature": 18,
35+
"unit": "Celsius",
36+
"conditions": "Cloudy
37+
},
38+
"forecast": {
39+
"day": "Monday",
40+
"high": 22,
41+
"low": 15,
42+
"precipitation": 10
43+
}
44+
}
45+
```
46+
47+
## Example prompt
48+
49+
```Why is my JSON object invalid and how can I fix it?```
50+
51+
## Example response
52+
53+
{% data reusables.copilot.example-prompts.response-is-an-example %}
54+
55+
{% data variables.product.prodname_copilot_short %} might suggest that your JSON is invalid because it is missing a closing quote for the `conditions` value. Here is the corrected JSON:
56+
57+
```json
58+
{
59+
"location": "San Francisco",
60+
"current_weather": {
61+
"temperature": 18,
62+
"unit": "Celsius",
63+
"conditions": "Cloudy"
64+
},
65+
"forecast": {
66+
"day": "Monday",
67+
"high": 22,
68+
"low": 15,
69+
"precipitation": 10
70+
}
71+
}
72+
```
73+
74+
In this example response, {% data variables.product.prodname_copilot_short %}'s suggestions include fixing the closing quote for the `conditions` value, which resolves the JSON parsing error.
75+
76+
## Further reading
77+
78+
{% data reusables.copilot.example-prompts.further-reading-items %}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Handling API rate limits
3+
shortTitle: Handle API rate limits
4+
intro: '{% data variables.product.prodname_copilot_chat_short %} can help handle API rate limits by suggesting code that detects implements retry logic.'
5+
versions:
6+
feature: copilot
7+
category:
8+
- 'Debugging code'
9+
complexity:
10+
- Intermediate
11+
octicon: bug
12+
topics:
13+
- Copilot
14+
---
15+
16+
When making requests to APIs, it's common to encounter rate limits that restrict the number of calls you can make within a certain time frame. {% data variables.product.prodname_copilot_chat %} can help you handle these limits by suggesting code to detect rate limit responses and automatically retry requests after a delay.
17+
18+
## Example scenario
19+
20+
The following Python code fetches weather data from an external API. If the API has rate limits, requests may fail when limits are exceeded, and your app may need a way to handle these responses gracefully by implementing retry logic.
21+
22+
```python
23+
from flask import Flask, request
24+
import requests
25+
26+
app = Flask(__name__)
27+
28+
WEATHER_API_URL = "https://api.example.com/weather"
29+
30+
@app.route('/get_weather', methods=['GET'])
31+
def get_weather():
32+
city = request.args.get('city')
33+
# Simulate an API request to the external weather service
34+
response = requests.get(WEATHER_API_URL, params={"city": city})
35+
weather_data = response.json()
36+
37+
return weather_data
38+
```
39+
40+
## Example prompt
41+
42+
`How can I handle API rate limits within get_weather().`
43+
44+
## Example response
45+
46+
{% data reusables.copilot.example-prompts.response-is-an-example %}
47+
48+
{% data variables.product.prodname_copilot_short %} might suggest code that implements a retry mechanism with exponential backoff to limit the frequency of retry attempts.
49+
50+
For example:
51+
52+
```python
53+
import requests
54+
from flask import Flask, request
55+
from requests.adapters import HTTPAdapter
56+
from requests.packages.urllib3.util.retry import Retry
57+
58+
app = Flask(__name__)
59+
60+
WEATHER_API_URL = "https://api.example.com/weather"
61+
62+
def requests_retry_session(
63+
retries=3,
64+
backoff_factor=0.3,
65+
status_forcelist=(500, 502, 504),
66+
session=None,
67+
):
68+
session = session or requests.Session()
69+
retry = Retry(
70+
total=retries,
71+
read=retries,
72+
connect=retries,
73+
backoff_factor=backoff_factor,
74+
status_forcelist=status_forcelist,
75+
)
76+
adapter = HTTPAdapter(max_retries=retry)
77+
session.mount("http://", adapter)
78+
session.mount("https://", adapter)
79+
return session
80+
81+
@app.route('/get_weather', methods=['GET'])
82+
def get_weather():
83+
city = request.args.get('city')
84+
try:
85+
response = requests_retry_session().get(WEATHER_API_URL, params={"city": city})
86+
response.raise_for_status()
87+
weather_data = response.json()
88+
except requests.exceptions.RequestException as e:
89+
return {"error": str(e)}, 500
90+
91+
return weather_data
92+
```
93+
94+
In this example, {% data variables.product.prodname_copilot_short %}'s suggestions include setting up a retry session that allows the code to automatically retry requests if they fail due to specific status codes (500, 502, 504). The `backoff_factor` gradually increases the delay between retries, helping avoid exceeding the API's rate limit further.
95+
96+
## Further reading
97+
98+
{% data reusables.copilot.example-prompts.further-reading-items %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Debugging errors
3+
intro: 'Discover ways that you can use {% data variables.product.prodname_copilot %} to debug errors during development.'
4+
versions:
5+
feature: copilot
6+
topics:
7+
- Copilot
8+
children:
9+
- /debugging-invalid-json
10+
- /handling-api-rate-limits
11+
---
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: 'Documenting legacy code'
3+
shortTitle: Document legacy code
4+
intro: '{% data variables.product.prodname_copilot_chat_short %} can help with documenting legacy code.'
5+
versions:
6+
feature: copilot
7+
category:
8+
- 'Documenting code'
9+
complexity:
10+
- Simple
11+
octicon: book
12+
topics:
13+
- Copilot
14+
---
15+
Working with legacy code can be challenging for developers, especially when the code is complex or not well-documented. In such cases, it can be helpful to use Copilot Chat to explain unclear or complex code to other developers or to document it for future reference.
16+
17+
## Example scenario
18+
19+
The block of COBOL below connects to a database and inserts a record. The code lacks documentation, which makes it difficult to understand what it does and how it works.
20+
21+
```text
22+
IDENTIFICATION DIVISION.
23+
PROGRAM-ID. INSERT-RECORD.
24+
25+
ENVIRONMENT DIVISION.
26+
27+
DATA DIVISION.
28+
WORKING-STORAGE SECTION.
29+
01 WS-STATUS-FLAGS.
30+
05 WS-DB-STATUS PIC X(2).
31+
88 WS-SUCCESS VALUE "00".
32+
05 WS-SQLCODE PIC S9(9) COMP.
33+
05 WS-ERROR-MSG PIC X(50).
34+
35+
LINKAGE SECTION.
36+
01 LS-PARAMETERS.
37+
05 LS-PERSON-RECORD.
38+
10 PERSON-ID PIC 9(6).
39+
10 PERSON-NAME PIC X(50).
40+
10 PERSON-AGE PIC 9(3).
41+
05 LS-RESULT PIC X.
42+
88 SUCCESS VALUE 'T'.
43+
88 FAILED VALUE 'F'.
44+
45+
PROCEDURE DIVISION USING LS-PARAMETERS.
46+
PERFORM INSERT-AND-VALIDATE
47+
GOBACK
48+
.
49+
50+
INSERT-AND-VALIDATE.
51+
EXEC SQL
52+
INSERT INTO persons (id, name, age)
53+
VALUES (:PERSON-ID, :PERSON-NAME, :PERSON-AGE)
54+
END-EXEC
55+
56+
IF SQLCODE = 0
57+
EXEC SQL COMMIT END-EXEC
58+
SET SUCCESS TO TRUE
59+
ELSE
60+
EXEC SQL ROLLBACK END-EXEC
61+
SET FAILED TO TRUE
62+
STRING "DB Error: " SQLCODE
63+
DELIMITED BY SIZE
64+
INTO WS-ERROR-MSG
65+
DISPLAY WS-ERROR-MSG
66+
END-IF
67+
.
68+
```
69+
70+
## Example prompt
71+
72+
We can use a simple prompt to ask {% data variables.product.prodname_copilot_chat_short %} to document the code.
73+
74+
`Document this code`
75+
76+
> [!NOTE] If you want the documentation generated in another format like Markdown, you can specify the desired output format in the prompt. For example: `Document this code in Markdown`.
77+
78+
## Example response
79+
80+
{% data reusables.copilot.example-prompts.response-is-an-example %}
81+
82+
Copilot will provide a text overview of the code block, and an updated version with comments explaining the logic and purpose of each section.
83+
84+
For example:
85+
86+
```text
87+
*****************************************************************
88+
* Program: INSERT-RECORD
89+
* Purpose: Inserts a person record into the database with
90+
* transaction management and error handling
91+
*****************************************************************
92+
IDENTIFICATION DIVISION.
93+
PROGRAM-ID. INSERT-RECORD.
94+
95+
ENVIRONMENT DIVISION.
96+
97+
DATA DIVISION.
98+
WORKING-STORAGE SECTION.
99+
* Status flags and error handling variables
100+
01 WS-STATUS-FLAGS.
101+
05 WS-DB-STATUS PIC X(2). *> Database status
102+
88 WS-SUCCESS VALUE "00". *> Success flag
103+
05 WS-SQLCODE PIC S9(9) COMP. *> SQL return code
104+
05 WS-ERROR-MSG PIC X(50). *> Error message
105+
106+
LINKAGE SECTION.
107+
* Input/Output parameters
108+
01 LS-PARAMETERS.
109+
05 LS-PERSON-RECORD.
110+
10 PERSON-ID PIC 9(6). *> Person ID
111+
10 PERSON-NAME PIC X(50). *> Person name
112+
10 PERSON-AGE PIC 9(3). *> Person age
113+
05 LS-RESULT PIC X. *> Operation result
114+
88 SUCCESS VALUE 'T'. *> Success flag
115+
88 FAILED VALUE 'F'. *> Failure flag
116+
117+
PROCEDURE DIVISION USING LS-PARAMETERS.
118+
PERFORM INSERT-AND-VALIDATE
119+
GOBACK
120+
.
121+
122+
*****************************************************************
123+
* Handles database insertion with transaction management
124+
*****************************************************************
125+
INSERT-AND-VALIDATE.
126+
EXEC SQL
127+
INSERT INTO persons (id, name, age)
128+
VALUES (:PERSON-ID, :PERSON-NAME, :PERSON-AGE)
129+
END-EXEC
130+
131+
IF SQLCODE = 0
132+
EXEC SQL COMMIT END-EXEC *> Commit if successful
133+
SET SUCCESS TO TRUE
134+
ELSE
135+
EXEC SQL ROLLBACK END-EXEC *> Rollback if failed
136+
SET FAILED TO TRUE
137+
STRING "DB Error: " SQLCODE
138+
DELIMITED BY SIZE
139+
INTO WS-ERROR-MSG
140+
DISPLAY WS-ERROR-MSG
141+
END-IF
142+
.
143+
```
144+
145+
## Further reading
146+
147+
{% data reusables.copilot.example-prompts.further-reading-items %}

0 commit comments

Comments
 (0)