Để xây dựng một workflow (tạm dịch: quy trình định sẵn) prompt evaluation (tạm dịch: đánh giá prompt) riêng, trước hết bạn cần viết một prompt tốt, rồi tạo dữ liệu kiểm thử để xem prompt hoạt động ra sao. Hãy cùng thiết lập một hệ thống đánh giá cho prompt giúp người dùng viết code dành riêng cho AWS.
Xác định mục tiêu
Prompt của chúng ta cần giúp người dùng tạo ba loại đầu ra cho các trường hợp sử dụng AWS:
- Python code
- File cấu hình JSON
- Regular expressions
Yêu cầu quan trọng là khi người dùng nhờ làm một việc, chúng ta trả về nội dung gọn gàng theo một trong ba định dạng này, không kèm giải thích, tiêu đề hay phần cuối.

Đây là prompt ban đầu của chúng ta (phiên bản 1):
prompt = f"""
Please provide a solution to the following task:
{task}
"""Tạo dataset để đánh giá
Một dataset để đánh giá chứa các input mà chúng ta sẽ đưa vào prompt. Với mỗi cặp prompt và input, chúng ta sẽ chạy prompt rồi phân tích kết quả.
Dataset của chúng ta sẽ là một array gồm các object JSON. Mỗi object có một property "task" mô tả việc chúng ta muốn Claude thực hiện. Chúng ta có thể tự tạo dataset này hoặc dùng Claude để tạo tự động.

Vì đang tạo dữ liệu kiểm thử, đây là lúc rất hợp để dùng một model nhanh hơn như Haiku, thay vì dùng model Claude đầy đủ.
Tạo dữ liệu kiểm thử bằng code
Hãy viết một function tự động tạo test dataset (tạm dịch: bộ dữ liệu kiểm thử). Trước tiên, chúng ta cần các helper function để làm việc với Claude:
def add_user_message(messages, text):
user_message = {"role": "user", "content": text}
messages.append(user_message)
def add_assistant_message(messages, text):
assistant_message = {"role": "assistant", "content": text}
messages.append(assistant_message)
def chat(messages, system=None, temperature=1.0, stop_sequences=[]):
params = {
"model": model,
"max_tokens": 1000,
"messages": messages,
"temperature": temperature
}
if system:
params["system"] = system
if stop_sequences:
params["stop_sequences"] = stop_sequences
response = client.messages.create(**params)
return response.content[0].textGiờ hãy viết function tạo dataset:
def generate_dataset():
prompt = """
Generate an evaluation dataset for a prompt evaluation. The dataset will be used to evaluate prompts that generate Python, JSON, or Regex specifically for AWS-related tasks. Generate an array of JSON objects, each representing task that requires Python, JSON, or a Regex to complete.
Example output:
```json
[
{
"task": "Description of task",
},
...additional
]
```
* Focus on tasks that can be solved by writing a single Python function, a single JSON object, or a single regex
* Focus on tasks that do not require writing much code
Please generate 3 objects.
"""Để parse phản hồi JSON cho đúng, chúng ta sẽ dùng prefilling (tạm dịch: viết sẵn phần đầu câu trả lời) và stop sequences (tạm dịch: chuỗi ký tự để dừng):
messages = []
add_user_message(messages, prompt)
add_assistant_message(messages, "```json")
text = chat(messages, stop_sequences=["```"])
return json.loads(text)Thử tạo dataset
Hãy chạy function và xem chúng ta nhận được những test case nào:
dataset = generate_dataset()
print(dataset)Function này sẽ trả về ba test case khác nhau, bao phủ các đầu ra mà chúng ta nhắm tới: Python function, cấu hình JSON và regular expressions cho các tác vụ dành riêng cho AWS.
Lưu dataset
Khi đã có dataset, chúng ta sẽ lưu nó vào một file để dễ tải lại lúc đánh giá:
with open('dataset.json', 'w') as f:
json.dump(dataset, f, indent=2)Bước này tạo file dataset.json trong cùng thư mục với notebook của bạn. File chứa danh sách tác vụ, sẵn sàng để dùng cho prompt evaluation.
Với phần nền tảng này, giờ bạn đã có một cách có hệ thống để tạo dữ liệu kiểm thử, nhằm đánh giá prompt của mình hoạt động tốt đến đâu với các loại tác vụ viết code liên quan đến AWS.