Tutorial Tutorial Tutorial Developer Platform Developer Platform Developer Platform ~56 phút ~56 min ~56 នាទី Đồng bộ 2026-06-10 Synced 2026-06-10 ធ្វើសមកាល 2026-06-10

Tạo AI Code Executor Build an AI code executor បង្កើតកម្មវិធីប្រតិបត្តិកូដ AI

Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page. មគ្គុទ្ទេសក៍លម្អិតធ្វើសមកាលពី docs Cloudflare — ផ្នែកនីមួយៗមានតំណទៅទីតាំងត្រូវគ្នានៅទំព័រផ្លូវការ។

← Danh mục ← Catalog ← បញ្ជី

Giải thích nhanh Quick context បរិបទរហ័ស

Thuộc Developer Platform — thực hành triển khai code, API và dữ liệu trên edge/serverless. Tutorial «Xây dựng AI code executor» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Docs gốc chia khoảng 7 bước chính; bản tóm tắt dưới đây giúp bạn nắm khung trước khi làm theo từng lệnh.

Use Claude to generate Python code from natural language and execute it securely in sandboxes.

ប្រើ Claude ដើម្បីបង្កើតកូដ Python ពីភាសា natural ហើយប្រតិបត្តិវាដោយសុវត្ថិភាពក្នុងប្រអប់ខ្សាច់។

Lưu ý Note ចំណាំ

Lưu ý trước khi làm Notes before you start ចំណាំមុនពេលចាប់ផ្តើម

  • Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức.
  • Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ.
  • Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production.
  • This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
  • Open the Official docs link below for CLI commands, code snippets, and full screenshots.
  • Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.
  • នេះគឺជាការសង្ខេបនៅលើ Orange Cloud Learning Hub — វាមិនជំនួសឯកសារផ្លូវការទេ។
  • បើកតំណឯកសារផ្លូវការខាងក្រោមសម្រាប់ពាក្យបញ្ជា CLI កូដ snippets និងរូបថតអេក្រង់ពេញ។
  • Cloudflare ឯកសារផ្លាស់ប្តូរជាញឹកញាប់ — ផ្ទៀងផ្ទាត់កាលបរិច្ឆេទដែលបានពិនិត្យចុងក្រោយនៅលើទំព័រផ្លូវការមុនពេលប្រើប្រាស់ផលិតកម្ម។

Tài liệu gốc — rà soát lần cuối: 8 months ago Official docs — last reviewed: 8 months ago ឯកសារផ្លូវការ — ពិនិត្យចុងក្រោយ: 8 months ago

Overview Overview ទិដ្ឋភាពទូទៅ

Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «ទិដ្ឋភាពទូទៅ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Xây dựng một hệ thống thực thi mã AI sử dụng Sandbox SDK và Claude. Chuyển các câu hỏi ngôn ngữ tự nhiên thành mã Python, thực hiện nó một cách an toàn và trả về kết quả.
Build an AI-powered code execution system using Sandbox SDK and Claude. Turn natural language questions into Python code, execute it securely, and return results.
បង្កើតប្រព័ន្ធប្រតិបត្តិកូដដែលដំណើរការដោយ AI ដោយប្រើ Sandbox SDK និង Claude ។ បង្វែរសំណួរភាសា nat ទៅជាកូដ Python ប្រតិបត្តិវាដោយសុវត្ថិភាព និងផ្តល់លទ្ធផលមកវិញ។
Time to complete: 20 minutes
Time to complete: 20 minutes
Time to complete: 20 នាទី។

Những gì bạn sẽ xây dựng What you'll build អ្វីដែលអ្នកនឹងសាងសង់

Phần «What you'll build» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "What you'll build" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «អ្វីដែលអ្នកនឹងសាងសង់» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Một API mà chấp nhận câu hỏi như "What's the 100th Fibonacci number?", sử dụng Claude để tạo ra mã Python, thực hiện nó trong một hộp cát cô lập, và trả về kết quả.
An API that accepts questions like "What's the 100th Fibonacci number?", uses Claude to generate Python code, executes it in an isolated sandbox, and returns the results.
API ដែលទទួលយកសំណួរដូចជា "តើលេខ Fibonacci ទី 100 ជាអ្វី?" ប្រើ Claude ដើម្បីបង្កើតកូដ Python ប្រតិបត្តិវានៅក្នុងប្រអប់ខ្សាច់ដាច់ពីគេ ហើយផ្តល់លទ្ធផលមកវិញ។

Điều kiện Prerequisites តម្រូវការជាមុន

Phần «Yêu cầu trước» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Prerequisites" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «តម្រូវការជាមុន» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
  1. Đăng ký cho một Cloudflare account ↗.
  2. Cài đặt Node.js ↗
  1. Sign up for a Cloudflare account ↗.
  2. Install Node.js ↗.
  1. ចុះឈ្មោះសម្រាប់ Cloudflare គណនី>។
  2. ដំឡើង Node.js ↗។
Node.js Trình quản lý phiên bản
Node.js version manager
កម្មវិធីគ្រប់គ្រងកំណែ Node.js
Sử dụng trình quản lý phiên bản nút như Volta ↗ hoặc nvm ↗ để tránh các vấn đề quyền và thay đổi các phiên bản Node.js. Wrangler, được thảo luận sau trong hướng dẫn này, yêu cầu một phiên bản nút của 16.17.0 hoặc mới hơn.
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0 or later.
ប្រើកម្មវិធីគ្រប់គ្រងកំណែ Node ដូចជា Volta ↗ ឬ vm" class="linker""> ↗ ដើម្បីជៀសវាងបញ្ហាការអនុញ្ញាត និងផ្លាស់ប្តូរកំណែ Node.js ។ __TERM27_2_code ទាមទារកំណែក្រោយនេះ ការណែនាំ class="tutorial-inline-code">16.17.0 ឬក្រោយ។
Bạn cũng sẽ cần:
You'll also need:
អ្នកក៏នឹងត្រូវការផងដែរ៖

Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)

1.Điều Tạo dự án của bạn 1. Create your project 1. បង្កើតគម្រោងរបស់អ្នក។

Tạo new Sandbox SDK project:

Read the "1. Create your project" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «1. បង្កើតគម្រោងរបស់អ្នក។» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Tạo dự án Sandbox SDK mới:
Create a new Sandbox SDK project:
បង្កើតគម្រោង Sandbox SDK ថ្មី៖
Mã sản phẩm: npm yarn pnpm
npm yarn pnpm
npm yarn pnpm
text
npm create cloudflare@latest -- ai-code-executor --template=cloudflare/sandbox-sdk/examples/minimal
text
yarn create cloudflare ai-code-executor --template=cloudflare/sandbox-sdk/examples/minimal
text
pnpm create cloudflare@latest ai-code-executor --template=cloudflare/sandbox-sdk/examples/minimal
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
cd ai-code-executor

2.Đối với Cài đặt dependencies 2. Install dependencies 2. ដំឡើងភាពអាស្រ័យ

Phần «Cài đặt dependencies» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "2. Install dependencies" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «2. ដំឡើងភាពអាស្រ័យ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Cài đặt Anthropic SDK:
Install the Anthropic SDK:
ដំឡើង Anthropic SDK៖
npm yarn pnpm bun
npm yarn pnpm bun
npm yarn pnpm ប៊ុន
text
npm i @anthropic-ai/sdk
text
yarn add @anthropic-ai/sdk
text
pnpm add @anthropic-ai/sdk
text
bun add @anthropic-ai/sdk

3 Cái Tạo Code Executor của bạn 3. Build your code executor 3. បង្កើតកម្មវិធីប្រតិបត្តិកូដរបស់អ្នក។

Phần «Build your code executor» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "3. Build your code executor" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «3. បង្កើតកម្មវិធីប្រតិបត្តិកូដរបស់អ្នក។» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Thay thế nội dung của src/index.ts:
Replace the contents of src/index.ts:
ជំនួសខ្លឹមសារនៃ src/index.ts៖
TypeScript Đánh giá
TypeScript
ប្រភេទScript
text
import { getSandbox, type Sandbox } from '@cloudflare/sandbox';

import Anthropic from '@anthropic-ai/sdk';


export { Sandbox } from '@cloudflare/sandbox';


interface Env {

  Sandbox: DurableObjectNamespace<Sandbox>;

  ANTHROPIC_API_KEY: string;

}


export default {

  async fetch(request: Request, env: Env): Promise<Response> {

    if (request.method !== 'POST' || new URL(request.url).pathname !== '/execute') {

      return new Response('POST /execute with { "question": "your question" }');

    }


    try {

      const { question } = await request.json();


      if (!question) {

        return Response.json({ error: 'Question is required' }, { status: 400 });

      }


      // Use Claude to generate Python code

      const anthropic = new Anthropic({ apiKey: env.ANTHROPIC_API_KEY });

      const codeGeneration = await anthropic.messages.create({

        model: 'claude-sonnet-4-5',

        max_tokens: 1024,

        messages: [{

          role: 'user',

          content: `Generate Python code to answer: "${question}"


Requirements:

- Use only Python standard library

- Print the result using print()

- Keep code simple and safe


Return ONLY the code, no explanations.`

        }],

      });


      const generatedCode = codeGeneration.content[0]?.type === 'text'

        ? codeGeneration.content[0].text

        : '';


      if (!generatedCode) {

        return Response.json({ error: 'Failed to generate code' }, { status: 500 });

      }


      // Strip markdown code fences if present

      const cleanCode = generatedCode

        .replace(/^```python?\n?/, '')

        .replace(/\n?```\s*$/, '')

        .trim();


      // Execute the code in a sandbox

      const sandbox = getSandbox(env.Sandbox, 'demo-user');

      await sandbox.writeFile('/tmp/code.py', cleanCode);

      const result = await sandbox.exec('python /tmp/code.py');


      return Response.json({

        success: result.success,

        question,

        code: generatedCode,

        output: result.stdout,

        error: result.stderr

      });


    } catch (error: any) {

      return Response.json(

        { error: 'Internal server error', message: error.message },

        { status: 500 }

      );

    }

  },

};
How it works:
How it works:
How it works:
  1. Nhận câu hỏi qua POST đến /execute
  2. Sử dụng Claude để tạo mã Python
  3. Viết mã thành /tmp/code.py trong sandbox
  4. Chạy với sandbox.exec('python /tmp/code.py')
  5. Trả về cả mã và kết quả thực thi
  1. Receives a question via POST to /execute
  2. Uses Claude to generate Python code
  3. Writes code to /tmp/code.py in the sandbox
  4. Executes with sandbox.exec('python /tmp/code.py')
  5. Returns both the code and execution results
  1. ទទួលសំណួរតាមរយៈ POST ទៅ /execute
  2. ប្រើ Claude ដើម្បីបង្កើតកូដ Python
  3. សរសេរកូដទៅ /tmp/code.py ក្នុងប្រអប់ខ្សាច់
  4. ប្រតិបត្តិជាមួយ sandbox.exec('python /tmp/code.py')
  5. ត្រឡប់ទាំងកូដ និងលទ្ធផលប្រតិបត្តិ

Bốn Thiết lập các biến môi trường địa phương 4. Set up local environment variables 4. រៀបចំអថេរបរិស្ថានមូលដ្ឋាន

Tạo .dev.vars file in your project root for local development:

Read the "4. Set up local environment variables" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «4. រៀបចំអថេរបរិស្ថានមូលដ្ឋាន» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Tạo tệp .dev.vars trong nguồn gốc dự án của bạn cho phát triển cục bộ:
Create a .dev.vars file in your project root for local development:
បង្កើតឯកសារ .dev.vars នៅក្នុងគម្រោង root របស់អ្នកសម្រាប់ការអភិវឌ្ឍន៍មូលដ្ឋាន៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
echo "ANTHROPIC_API_KEY=your_api_key_here" > .dev.vars
Thay thế your<em>api</em>key_here bằng phím API thực tế của bạn từ Anthropic Console ↗.
Replace your<em>api</em>key_here with your actual API key from the Anthropic Console ↗.
ជំនួស your<em>api</em>key_here របស់អ្នកជាមួយនឹងសោ API ពិតប្រាកដរបស់អ្នកពី Tệp .dev.vars được tự động bỏ qua và chỉ được sử dụng trong quá trình phát triển cục bộ với npm run dev.
The .dev.vars file is automatically gitignored and only used during local development with npm run dev.
ឯកសារ .dev.vars ត្រូវបាន gitignored ដោយស្វ័យប្រវត្ត ហើយប្រើតែកំឡុងពេលអភិវឌ្ឍន៍មូលដ្ឋានជាមួយ npm run dev។
Khởi động máy chủ phát triển:
Start the development server:
ចាប់ផ្តើមម៉ាស៊ីនមេអភិវឌ្ឍន៍៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
npm run dev
Chạy đầu tiên xây dựng container Docker (2-3 phút). Các cuộc đua tiếp theo nhanh hơn nhiều.
First run builds the Docker container (2-3 minutes). Subsequent runs are much faster.
ការរត់ដំបូងបង្កើត Docker container (2-3 នាទី) ។ ការរត់ជាបន្តបន្ទាប់គឺលឿនជាង។
Thử nghiệm với Curl:
Test with curl:
សាកល្បងជាមួយ curl:
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
curl -X POST http://localhost:8787/execute \

  -H "Content-Type: application/json" \

  -d '{"question": "What is the 10th Fibonacci number?"}'
Trả lời :
Response:
ការឆ្លើយតប៖
text
{

  "success": true,

  "question": "What is the 10th Fibonacci number?",

  "code": "def fibonacci(n):\n    if n <= 1:\n        return n\n    return fibonacci(n-1) + fibonacci(n-2)\n\nprint(fibonacci(10))",

  "output": "55\n",

  "error": ""

}

Sáu triển khai 6. Deploy 6. ដាក់ពង្រាយ

Phần «Deploy» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "6. Deploy" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «6. ដាក់ពង្រាយ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Xử lý nhân viên của bạn:
Deploy your Worker:
ដាក់ពង្រាយកម្មកររបស់អ្នក៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
npx wrangler deploy
Sau đó đặt phím Anthropic API của bạn làm bí mật sản xuất:
Then set your Anthropic API key as a production secret:
បន្ទាប់មកកំណត់សោ Anthropic API របស់អ្នកជាអាថ៌កំបាំងផលិតកម្ម៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
npx wrangler secret put ANTHROPIC_API_KEY
Nhấn API từ Anthropic Console ↗ khi được nhắc.
Paste your API key from the Anthropic Console ↗ when prompted.
បិទភ្ជាប់សោ API របស់អ្នកពី Anthropic Console ↗ នៅពេលត្រូវបានសួរ។
Warning
Warning
ការព្រមាន
Sau khi triển khai đầu tiên, chờ 2-3 phút để cung cấp container. Kiểm tra trạng thái với npx wrangler containers list.
After first deployment, wait 2-3 minutes for container provisioning. Check status with npx wrangler containers list.
បន្ទាប់ពីការដាក់ពង្រាយលើកដំបូង រង់ចាំ 2-3 នាទីសម្រាប់ការរៀបចំកុងតឺន័រ។ ពិនិត្យស្ថានភាពជាមួយ npx wrangler containers list។

Bảy Kiểm tra triển khai của bạn 7. Test your deployment 7. សាកល្បងការដាក់ពង្រាយរបស់អ្នក។

Phần «Test your deployment» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "7. Test your deployment" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «7. សាកល្បងការដាក់ពង្រាយរបស់អ្នក។» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Hãy thử các câu hỏi khác nhau:
Try different questions:
សាកល្បងសំណួរផ្សេងៗ៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
# Factorial

curl -X POST https://ai-code-executor.YOUR_SUBDOMAIN.workers.dev/execute \

  -H "Content-Type: application/json" \

  -d '{"question": "Calculate the factorial of 5"}'


# Statistics

curl -X POST https://ai-code-executor.YOUR_SUBDOMAIN.workers.dev/execute \

  -H "Content-Type: application/json" \

  -d '{"question": "What is the mean of [10, 20, 30, 40, 50]?"}'


# String manipulation

curl -X POST https://ai-code-executor.YOUR_SUBDOMAIN.workers.dev/execute \

  -H "Content-Type: application/json" \

  -d '{"question": "Reverse the string \"Hello World\""}'

Những gì bạn xây dựng What you built អ្វីដែលអ្នកបានសាងសង់

Phần «What you built» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "What you built" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «អ្វីដែលអ្នកបានសាងសង់» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Bạn đã tạo ra một hệ thống thực thi mã AI mà:
You created an AI code execution system that:
អ្នកបានបង្កើតប្រព័ន្ធប្រតិបត្តិកូដ AI ដែល៖
  • Chấp nhận câu hỏi ngôn ngữ tự nhiên
  • Tạo mã Python với Claude
  • Thực hiện mã an toàn trong các hộp cát bị cô lập
  • Trả về kết quả với xử lý lỗi
  • Accepts natural language questions
  • Generates Python code with Claude
  • Executes code securely in isolated sandboxes
  • Returns results with error handling
  • ទទួលយក natសំណួរភាសាអ៊ូរ៉ាល់
  • បង្កើតកូដ Python ជាមួយ Claude
  • ប្រតិបត្តិកូដដោយសុវត្ថិភាពនៅក្នុងប្រអប់ខ្សាច់ដាច់ដោយឡែក
  • ត្រឡប់លទ្ធផលជាមួយនឹងការគ្រប់គ្រងកំហុស

Bước tiếp theo Next steps ជំហានបន្ទាប់

Phần «Bước tiếp theo» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Next steps" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «ជំហានបន្ទាប់» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗

Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)

Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs). មើលមគ្គុទ្ទេសក៍ពេញលើ developers.cloudflare.com (រូបភាព, tab កំណត់)។

Tài liệu gốc ↗ Official docs ↗ ឯកសារផ្លូវការ ↗