|
1 | 1 | ---
|
2 | 2 | title: Getting Started with Code2Prompt
|
3 |
| -description: A step-by-step guide to installing and running Code2Prompt for the first time |
| 3 | +description: A comprehensive tutorial introducing Code2Prompt's core functionality and its use across CLI, SDK, and MCP integrations. |
4 | 4 | ---
|
5 | 5 |
|
6 | 6 | import { Aside } from "@astrojs/starlight/components";
|
7 | 7 | import { Tabs, TabItem } from "@astrojs/starlight/components";
|
8 |
| - |
9 |
| -Welcome to Code2Prompt! This guide will help you get started with installing and running Code2Prompt for the first time. |
10 |
| - |
11 |
| -## Installation |
12 |
| - |
13 |
| -Check out the [installation instructions](/docs/how_to/install) |
14 |
| - |
15 |
| -## Using Code2Prompt |
16 |
| - |
17 |
| -Generate a prompt from a codebase directory: |
18 |
| - |
19 |
| -```sh |
20 |
| -code2prompt path/to/codebase |
21 |
| -``` |
22 |
| - |
23 |
| -Use a custom Handlebars template file: |
24 |
| - |
25 |
| -```sh |
26 |
| -code2prompt path/to/codebase --template path/to/template.hbs |
27 |
| -``` |
28 |
| - |
29 |
| -Filter files using glob patterns: |
30 |
| - |
31 |
| -```sh |
32 |
| -code2prompt path/to/codebase --include="*.rs,*.toml" |
33 |
| -``` |
34 |
| - |
35 |
| -Exclude files using glob patterns: |
36 |
| - |
37 |
| -```sh |
38 |
| -code2prompt path/to/codebase --exclude="*.txt,*.md" |
39 |
| -``` |
40 |
| - |
41 |
| -Display the token count of the generated prompt: |
42 |
| - |
43 |
| -```sh |
44 |
| -code2prompt path/to/codebase --tokens |
| 8 | +import { Card, CardGrid } from "@astrojs/starlight/components"; |
| 9 | + |
| 10 | +<Card title="Tutorial Overview"> |
| 11 | + Welcome to Code2Prompt! This tutorial provides a comprehensive introduction to |
| 12 | + using Code2Prompt to generate AI-ready prompts from your codebases. We'll |
| 13 | + explore its core functionality and demonstrate its usage across different |
| 14 | + integration methods: Command Line Interface (CLI), Software Development Kit |
| 15 | + (SDK), and Model Context Protocol (MCP). |
| 16 | +</Card> |
| 17 | + |
| 18 | +## What is Code2Prompt? |
| 19 | + |
| 20 | +Code2Prompt is a versatile tool designed to bridge the gap between your codebase and Large Language Models (LLMs). It intelligently extracts relevant code snippets, applies powerful filtering, and formats the information into structured prompts optimized for LLM consumption. This simplifies tasks like code documentation, bug detection, refactoring, and more. |
| 21 | + |
| 22 | +Code2Prompt offers different integration points: |
| 23 | + |
| 24 | +<Tabs> |
| 25 | + <TabItem label="Core" icon="seti:rust"> |
| 26 | + A core rust library that provides the foundation for code ingestion and |
| 27 | + prompt |
| 28 | + </TabItem> |
| 29 | + <TabItem label="CLI" icon="seti:powershell"> |
| 30 | + A user-friendly command-line interface for quick prompt generation. Ideal |
| 31 | + for interactive use and one-off tasks. |
| 32 | + </TabItem> |
| 33 | + <TabItem label="SDK" icon="seti:python"> |
| 34 | + A powerful Software Development Kit (SDK) for seamless integration into your |
| 35 | + Python projects. Perfect for automating prompt generation within larger |
| 36 | + workflows. |
| 37 | + </TabItem> |
| 38 | + <TabItem label="MCP" icon="seti:db"> |
| 39 | + A Model Context Protocol (MCP) server for advanced integration with LLM |
| 40 | + agents. Enables sophisticated, real-time interactions with your codebase. |
| 41 | + </TabItem> |
| 42 | +</Tabs> |
| 43 | + |
| 44 | +## 📥 Installation |
| 45 | + |
| 46 | +For detailed installation instructions for all methods (CLI, SDK, MCP), please refer to the comprehensive [Installation Guide](/docs/how_to/install). |
| 47 | + |
| 48 | +## 🏁 Generating Prompts: A CLI Example |
| 49 | + |
| 50 | +Let's start with a simple example using the CLI. Create a sample project: |
| 51 | + |
| 52 | +```bash |
| 53 | +mkdir -p my_project/{src,tests} |
| 54 | +touch my_project/src/main.rs my_project/tests/test_1.rs |
| 55 | +echo 'fn main() { println!("Hello, world!"); }' > my_project/src/main.rs |
45 | 56 | ```
|
46 | 57 |
|
47 |
| -Specify a tokenizer for token count: |
| 58 | +Now, generate a prompt: |
48 | 59 |
|
49 |
| -```sh |
50 |
| -code2prompt path/to/codebase --tokens --encoding=p50k |
| 60 | +```bash |
| 61 | +code2prompt my_project |
51 | 62 | ```
|
52 | 63 |
|
53 |
| -Supported tokenizers: `cl100k`, `p50k`, `p50k_edit`, `r50k_bas`. |
| 64 | +This copies a prompt to your clipboard. You can customize this: |
54 | 65 |
|
55 |
| -<Aside>See [Tokenizers](../explanations/tokenizers) for more details</Aside> |
| 66 | +- **Filtering:** `code2prompt my_project --include="*.rs" --exclude="tests/*"` (includes only `.rs` files, excludes `tests` directory) |
| 67 | +- **Output File:** `code2prompt my_project --output-file=my_prompt.txt` |
| 68 | +- **JSON Output:** `code2prompt my_project -O json` (structured JSON output) |
| 69 | +- **Custom Templates:** `code2prompt my_project -t my_template.hbs` (requires creating `my_template.hbs`) |
56 | 70 |
|
57 |
| -Save the generated prompt to an output file: |
| 71 | +See the [Learn Context Filtering](/docs/tutorials/learn_filters) and [Learn Handlebar Templates](/docs/tutorials/learn_templates) tutorials to learn more advanced usages. |
58 | 72 |
|
59 |
| -```sh |
60 |
| -code2prompt path/to/codebase --output-file=output.txt |
61 |
| -``` |
| 73 | +## 🐍 SDK Integration (Python) |
62 | 74 |
|
63 |
| -Print output as JSON: |
| 75 | +For programmatic control, use the Python SDK: |
64 | 76 |
|
65 |
| -```sh |
66 |
| -code2prompt path/to/codebase -O json |
67 |
| -``` |
| 77 | +```python |
| 78 | +from code2prompt_rs import Code2Prompt |
68 | 79 |
|
69 |
| -The JSON output will have the following structure: |
70 |
| - |
71 |
| -```json |
72 |
| -{ |
73 |
| - "prompt": "<Generated Prompt>", |
74 |
| - "directory_name": "codebase", |
75 |
| - "token_count": 1234, |
76 |
| - "model_info": "ChatGPT models, text-embedding-ada-002", |
77 |
| - "files": [] |
| 80 | +config = { |
| 81 | + "path": "my_project", |
| 82 | + "include_patterns": ["*.rs"], |
| 83 | + "exclude_patterns": ["tests/*"], |
78 | 84 | }
|
79 |
| -``` |
80 | 85 |
|
81 |
| -Generate a Git commit message (for staged files): |
82 |
| - |
83 |
| -```sh |
84 |
| -code2prompt path/to/codebase --diff --template templates/write-git-commit.hbs |
| 86 | +c2p = Code2Prompt(**config) |
| 87 | +prompt = c2p.generate_prompt() |
| 88 | +print(prompt) |
85 | 89 | ```
|
86 | 90 |
|
87 |
| -Generate a Pull Request with branch comparing (for staged files): |
88 |
| - |
89 |
| -```sh |
90 |
| -code2prompt path/to/codebase --git-diff-branch 'main, development' --git-log-branch 'main, development' --template templates/write-github-pull-request.hbs |
91 |
| -``` |
| 91 | +This requires installing the SDK (`pip install code2prompt_rs`). Refer to the SDK documentation for more details. |
92 | 92 |
|
93 |
| -Add line numbers to source code blocks: |
| 93 | +## 🤖 MCP Server Integration (Advanced) |
94 | 94 |
|
95 |
| -```sh |
96 |
| -code2prompt path/to/codebase --line-number |
97 |
| -``` |
| 95 | +For advanced integration with LLM agents, run the `code2prompt` MCP server (see the installation guide for details). This allows agents to request code context dynamically. This is an advanced feature, and further documentation is available on the project's website. |
98 | 96 |
|
99 |
| -Disable wrapping code inside markdown code blocks: |
100 |
| - |
101 |
| -```sh |
102 |
| -code2prompt path/to/codebase --no-codeblock |
103 |
| -``` |
104 |
| - |
105 |
| -Include hidden files and directories: |
106 |
| - |
107 |
| -```sh |
108 |
| -code2prompt path/to/codebase --hidden |
109 |
| -``` |
110 |
| - |
111 |
| -Skip .gitignore rules: |
112 |
| - |
113 |
| -```sh |
114 |
| -code2prompt path/to/codebase --no-ignore |
115 |
| -``` |
| 97 | +<Card title="Next Steps"> |
| 98 | + Explore the advanced tutorials and documentation to master Code2Prompt's |
| 99 | + capabilities and integrate it into your workflows. |
| 100 | +</Card> |
0 commit comments