0
0
GenaiHow-ToBeginner · 4 min read

How to Use Prompts for Extraction in AI Models

To use prompts for extraction, design clear and specific instructions that guide the AI to pull out the exact information you want from text. Use structured or natural language prompts that focus on the target data, such as asking for names, dates, or key facts.
📐

Syntax

A prompt for extraction typically includes a clear instruction and the text to extract from. It can be structured as:

  • Extract [information] from the following text:
  • [Your text here]
  • Answer:

This format tells the AI exactly what to look for and where.

python
prompt = "Extract the date from the following text:\n\n'The event will be held on July 20, 2024.'\n\nAnswer:"
💻

Example

This example shows how to use a prompt to extract a date from a sentence using OpenAI's GPT model via Python.

python
import openai

openai.api_key = 'your-api-key'

prompt = "Extract the date from the following text:\n\n'The event will be held on July 20, 2024.'\n\nAnswer:"

response = openai.Completion.create(
    model='text-davinci-003',
    prompt=prompt,
    max_tokens=10,
    temperature=0
)

extracted_date = response.choices[0].text.strip()
print(f"Extracted date: {extracted_date}")
Output
Extracted date: July 20, 2024
⚠️

Common Pitfalls

Common mistakes when using prompts for extraction include:

  • Being too vague or broad in the prompt, causing unclear or incomplete answers.
  • Not specifying the format or type of information expected.
  • Including irrelevant text that confuses the model.

Always keep prompts simple, focused, and explicit.

python
wrong_prompt = "Tell me something about this text:\n\n'The event will be held on July 20, 2024.'\n\nAnswer:"

right_prompt = "Extract the date from the following text:\n\n'The event will be held on July 20, 2024.'\n\nAnswer:"
📊

Quick Reference

Prompt ElementPurposeExample
InstructionTells the model what to extractExtract the date from the text:
Input TextThe text containing the data'The event is on July 20, 2024.'
Answer CueSignals where the answer should startAnswer:

Key Takeaways

Use clear and specific instructions in your prompts to guide extraction.
Keep prompts focused on the exact information you want to extract.
Avoid vague language and irrelevant details in the prompt.
Test and refine prompts to improve extraction accuracy.
Use answer cues like 'Answer:' to help the model know where to respond.