Menghasilkan teks dengan model bahasa

Pengantar Amazon Bedrock

Nikhil Rangarajan

Data Scientist

Mengirim prompt ke model bahasa

  • Prompt: teks masukan yang Anda kirim ke model
    • "Explain how AWS Lambda works"

Ikon yang mewakili prompt.

Pengantar Amazon Bedrock

Mengirim prompt ke model bahasa

  • Prompt: teks masukan yang Anda kirim ke model
    • "Explain how AWS Lambda works"
  • Completion: teks yang dihasilkan model
    • "AWS Lambda is a serverless compute service that..."

Ikon yang mewakili prompt dan completion.

Pengantar Amazon Bedrock

Mengirim prompt ke model bahasa

  • Prompt: teks masukan yang Anda kirim ke model
    • "Explain how AWS Lambda works"
  • Completion: teks yang dihasilkan model
    • "AWS Lambda is a serverless compute service that..."
  • Response:
    • Teks yang dihasilkan (completion)
    • Metadata (token, info model)
    • Informasi status

Tiga ikon berurutan mewakili prompt, completion, dan response.

Pengantar Amazon Bedrock

Menerapkan teknik dasar rekayasa prompt

  • Teknik untuk hasil lebih baik:
    • Kejelasan dan spesifik: tulis instruksi rinci dan tepat
    • Penetapan peran: beri persona untuk meningkatkan konteks
    • Instruksi format: tambahkan batasan untuk membimbing respons
"Explain the benefits of exercise 
in simple terms."
"You are a nutritionist. Explain the 
benefits of a balanced diet."
"List the top 3 benefits of cloud 
computing in bullet points."
Pengantar Amazon Bedrock

Teknik parsing teks lanjutan

  • Tangani key yang hilang/tidak terduga
data = json.loads(response['body'].read())
if 'completion' in data:
    output = data['completion']

else: output = "Key not found"
  • Parsing JSON bertingkat untuk struktur sangat bersarang
try:
    nova_output = data.decode()["output"]

except (KeyError, IndexError) as e: nova_output = f"Error: {str(e)}"
Pengantar Amazon Bedrock

Pemrosesan respons dan penanganan teks

  • Masalah Unicode/encoding: tangani karakter khusus pada respons teks
    # Ensure proper encoding
    output = data['completion'].encode('utf-8').decode('unicode_escape')
    print(output)
    
  • Memproses respons besar: pecah atau potong output besar agar mudah digunakan
    # Truncate response for display
    output = data['completion'][:500]  # Limit to 500 characters
    print(output)
    
Pengantar Amazon Bedrock

Ekstraksi kategori dengan Bedrock

  • Contoh: mengklasifikasikan data teks
  • Gunakan prompt terstruktur dengan opsi kategori jelas
  • Beri instruksi tegas untuk output satu kategori
  • Proses respons model secara efisien
  • Hasil: Kategorisasi teks akurat

Ikon yang mewakili klasifikasi teks

Pengantar Amazon Bedrock

Ekstraksi kategori dengan Bedrock

def categorize_ticket(ticket, categories):
  prompt = f"Categorize into one: {', '.join(categories)}. 
  Ticket: {ticket}"
  response = bedrock.invoke_model(
    modelId='amazon.nova-micro-v1:0',
    body=json.dumps({"messages": [{"role": "user", "content": 
                                   [{"text": prompt}]}]}))

return json.loads(nova_response.get("body").read().decode())["output"]
{"output": {"message": {"role": "assistant","content": [
        {
          "text": "Customer Service"
        }]}, "stop_reason": "stop_sequence"}}
Pengantar Amazon Bedrock

Ayo berlatih!

Pengantar Amazon Bedrock

Preparing Video For Download...