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

Phân tích dữ liệu với AI Analyze data with AI វិភាគទិន្នន័យជាមួយ 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 «Analyze data với AI» 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 6 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.

Upload CSV files, generate analysis code with Claude, and return visualizations.

ផ្ទុកឡើងឯកសារ CSV បង្កើតកូដវិភាគជាមួយ Claude និងបង្ហាញការមើលឃើញឡើងវិញ។

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 phân tích dữ liệu được hỗ trợ bởi AI chấp nhận tải lên CSV, sử dụng Claude để tạo mã phân tích Python, thực hiện nó trong hộp cát và trả về hình ảnh.
Build an AI-powered data analysis system that accepts CSV uploads, uses Claude to generate Python analysis code, executes it in sandboxes, and returns visualizations.
បង្កើតប្រព័ន្ធវិភាគទិន្នន័យដែលដំណើរការដោយ AI ដែលទទួលយកការបង្ហោះ CSV ប្រើប្រាស់ Claude ដើម្បីបង្កើតកូដការវិភាគ Python ប្រតិបត្តិវានៅក្នុងប្រអប់ខ្សាច់ និងបង្ហាញការមើលឃើញឡើងវិញ។
Time to complete: 25 minutes
Time to complete: 25 minutes
Time to complete៖ 25 នាទី។

Đ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 -- analyze-data --template=cloudflare/sandbox-sdk/examples/minimal
text
yarn create cloudflare analyze-data --template=cloudflare/sandbox-sdk/examples/minimal
text
pnpm create cloudflare@latest analyze-data --template=cloudflare/sandbox-sdk/examples/minimal
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
cd analyze-data

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 ផ្លូវការ ↗
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 Xây dựng phân tích handler 3. Build the analysis handler 3. បង្កើតអ្នកដោះស្រាយការវិភាគ

Phần «Build analysis handler» — đọ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 the analysis handler" 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ế src/index.ts:
Replace src/index.ts:
ជំនួស src/index.ts៖
TypeScript Đánh giá
TypeScript
ប្រភេទScript
text
import { getSandbox, proxyToSandbox, 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> {

    const proxyResponse = await proxyToSandbox(request, env);

    if (proxyResponse) return proxyResponse;


    if (request.method !== "POST") {

      return Response.json(

        { error: "POST CSV file and question" },

        { status: 405 },

      );

    }


    try {

      const formData = await request.formData();

      const csvFile = formData.get("file") as File;

      const question = formData.get("question") as string;


      if (!csvFile || !question) {

        return Response.json(

          { error: "Missing file or question" },

          { status: 400 },

        );

      }


      // Upload CSV to sandbox

      const sandbox = getSandbox(env.Sandbox, `analysis-${Date.now()}`);

      const csvPath = "/workspace/data.csv";

      await sandbox.writeFile(csvPath, await csvFile.text());


      // Analyze CSV structure

      const structure = await sandbox.exec(

        `python3 -c "import pandas as pd; df = pd.read_csv('${csvPath}'); print(f'Rows: {len(df)}'); print(f'Columns: {list(df.columns)[:5]}')"`,

      );


      if (!structure.success) {

        return Response.json(

          { error: "Failed to read CSV", details: structure.stderr },

          { status: 400 },

        );

      }


      // Generate analysis code with Claude

      const code = await generateAnalysisCode(

        env.ANTHROPIC_API_KEY,

        csvPath,

        question,

        structure.stdout,

      );


      // Write and execute the analysis code

      await sandbox.writeFile("/workspace/analyze.py", code);

      const result = await sandbox.exec("python /workspace/analyze.py");


      if (!result.success) {

        return Response.json(

          { error: "Analysis failed", details: result.stderr },

          { status: 500 },

        );

      }


      async function streamToBase64(stream) {

        const blob = await new Response(stream).blob();

        const buffer = await blob.arrayBuffer();

        const bytes = new Uint8Array(buffer);


        // Convert to base64

        let binary = '';

        for (let i = 0; i < bytes.length; i++) {

          binary += String.fromCharCode(bytes[i]);

        }

        return btoa(binary);

      }


      // Check for generated chart

      let chart = null;

      try {

        const { content, mimeType } = await sandbox.readFile("/workspace/chart.png", {

          encoding: "none"

        });

        chart = `data:${mimeType};base64,${await streamToBase64(content)}`;

      } catch {

        // No chart generated

      }


      await sandbox.destroy();


      return Response.json({

        success: true,

        output: result.stdout,

        chart,

        code,

      });

    } catch (error: any) {

      return Response.json({ error: error.message }, { status: 500 });

    }

  },

};


async function generateAnalysisCode(

  apiKey: string,

  csvPath: string,

  question: string,

  csvStructure: string,

): Promise<string> {

  const anthropic = new Anthropic({ apiKey });


  const response = await anthropic.messages.create({

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

    max_tokens: 2048,

    messages: [

      {

        role: "user",

        content: `CSV at ${csvPath}:

${csvStructure}


Question: "${question}"


Generate Python code that:

- Reads CSV with pandas

- Answers the question

- Saves charts to /workspace/chart.png if helpful

- Prints findings to stdout


Use pandas, numpy, matplotlib.`,

      },

    ],

    tools: [

      {

        name: "generate_python_code",

        description: "Generate Python code for data analysis",

        input_schema: {

          type

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\nSANDBOX_TRANSPORT=rpc" > .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 ពិតប្រាកដរបស់អ្នកពី SANDBOX_TRANSPORT là cần thiết để sử dụng các API phát trực tuyến tập tin mới.
The SANDBOX_TRANSPORT is required to use the new file streaming APIs.
SANDBOX_TRANSPORT តម្រូវឱ្យប្រើឯកសារថ្មី streaming APIs ។
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។
Tải về mẫu CSV:
Download a sample CSV:
ទាញយកគំរូ CSV៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
# Create a test CSV

echo "year,rating,title

2020,8.5,Movie A

2021,7.2,Movie B

2022,9.1,Movie C" > test.csv
Khởi động Dev Server:
Start the dev server:
ចាប់ផ្តើមម៉ាស៊ីនមេ dev៖
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
npm run dev
Thử nghiệm với Curl:
Test with curl:
សាកល្បងជាមួយ curl:
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
curl -X POST http://localhost:8787 \

  -F "file=@test.csv" \

  -F "question=What is the average rating by year?"
Trả lời :
Response:
ការឆ្លើយតប៖
text
{

  "success": true,

  "output": "Average ratings by year:\n2020: 8.5\n2021: 7.2\n2022: 9.1",

  "chart": "data:image/png;base64,...",

  "code": "import pandas as pd\nimport matplotlib.pyplot as plt\n..."

}

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
ការព្រមាន
Chờ 2-3 phút sau khi triển khai đầu tiên để cung cấp container.
Wait 2-3 minutes after first deployment for container provisioning.
រង់ចាំ 2-3 នាទីបន្ទាប់ពីការដាក់ពង្រាយលើកដំបូងសម្រាប់ការផ្គត់ផ្គង់កុងតឺន័រ។

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 ផ្លូវការ ↗
Một hệ thống phân tích dữ liệu AI mà:
An AI data analysis system that:
ប្រព័ន្ធវិភាគទិន្នន័យ AI ដែល៖
  • Tải file CSV lên sandbox
  • Sử dụng công cụ Calling của Claude để tạo mã phân tích
  • Chạy Python với pandas và matplotlib
  • Trả về text output và visualizations
  • Uploads CSV files to sandboxes
  • Uses Claude's tool calling to generate analysis code
  • Executes Python with pandas and matplotlib
  • Returns text output and visualizations
  • ផ្ទុកឡើងឯកសារ CSV ទៅកាន់ប្រអប់ខ្សាច់
  • ប្រើការហៅឧបករណ៍របស់ Claude ដើម្បីបង្កើតកូដវិភាគ
  • ប្រតិបត្តិ Python ជាមួយ pandas និង matplotlib
  • ត្រឡប់លទ្ធផលអត្ថបទ និងការមើលឃើញ

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 ផ្លូវការ ↗
json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/sandbox/","name":"Sandbox SDK"}},{"@type":"ListItem","position":3,"item":{"@id":"/sandbox/tutorials/","name":"Tutorials"}},{"@type":"ListItem","position":4,"item":{"@id":"/sandbox/tutorials/analyze-data-with-ai/","name":"Analyze data with AI"}}]}

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 ↗ ឯកសារផ្លូវការ ↗