Skip to content

FIND Prompt

Prompt Engineering: Extracting Specific Information

Section titled “Prompt Engineering: Extracting Specific Information”

A common task in prompt engineering is to guide a Large Language Model (LLM) like ChatGPT to extract specific pieces of information from a given text or from its own generated response. While LLMs don’t have a literal “FIND” command, you can design prompts that instruct them to locate and present relevant details based on your criteria, enhancing the precision and utility of the output.

Instead of a specific directive, you instruct the LLM through natural language to perform the extraction. This involves clearly defining what information you’re looking for and often specifying the format for the extracted data.

Consider this conceptual approach: You provide text and ask the LLM to identify and list certain elements within it.

User: Here is a customer review: "I absolutely love the new QuietTune headphones! The noise cancellation is incredible, and the battery lasts for ages. My only complaint is that the carrying case feels a bit flimsy. Overall, a great purchase from AudioMax, delivered on September 15th."
Please extract the product name, brand name, any positive mentions, any negative mentions, and the delivery date. Present them as a list.
LLM: Okay, here's the extracted information from the review:
* **Product Name:** QuietTune headphones
* **Brand Name:** AudioMax
* **Positive Mentions:** Noise cancellation is incredible, battery lasts for ages, great purchase
* **Negative Mentions:** Carrying case feels a bit flimsy
* **Delivery Date:** September 15th

In this example, the user’s prompt clearly specifies what to ‘find’ or extract and the desired output format. The LLM understands these instructions and processes the text accordingly.

Best Practices for Information Extraction Prompts

Section titled “Best Practices for Information Extraction Prompts”

To effectively guide LLMs in extracting information, consider these best practices:

  • Be Specific and Unambiguous: Clearly define what information you want to extract. Use precise terms. Instead of ‘find important parts,’ say ‘extract all dates mentioned’ or ‘identify the key people involved.’
  • Specify the Source: Indicate whether the information should be extracted from a provided text or if the LLM should generate text and then extract from its own generation.
  • Define the Output Format: Request the information in a structured format like a list, bullet points, JSON, or a table. This makes the output easier to parse and use. Example: ‘Extract the names and email addresses, and provide them in a JSON array of objects.’
  • Use Contextual Prompts: Provide the text from which to extract information clearly demarcated within the prompt. If asking the LLM to generate and then extract, ensure the generation instructions are clear first.
  • Iterate and Refine: If the initial extraction isn’t accurate, refine your prompt. You might need to rephrase, add more constraints, or provide an example (few-shot prompting).
  • Consider Chain-of-Thought for Complex Extractions: For multi-step extractions (e.g., ‘Find all companies mentioned, then for each company, find their CEO if mentioned’), you might break it down or ask the LLM to ‘think step by step.‘

Example Application: Python Implementation for Entity Extraction

Section titled “Example Application: Python Implementation for Entity Extraction”

Let’s explore a Python script that uses the OpenAI API to extract specific entities from a user-provided text.

In this example, we define a function extract_information() that takes a text and a list of entities to extract. The prompt guides ChatGPT to find these entities within the text.

from openai import OpenAI
import os
# client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# For this example, use a placeholder. Replace with your actual key.
client = OpenAI(api_key='YOUR_API_KEY')
def extract_entities_from_text(text_to_analyze, entities_to_extract):
# entities_to_extract should be a string like "person names, locations, and organizations"
prompt_content = f"Analyze the following text and extract all {entities_to_extract}. Present the extracted information clearly, categorizing by entity type if multiple types are requested.\n\nText to analyze:\n"""
{text_to_analyze}
"""\n\nExtracted Information:"
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo", # Or a more current model
messages=[
{"role": "system", "content": "You are an AI assistant specialized in information extraction."},
{"role": "user", "content": prompt_content}
],
max_tokens=300,
temperature=0.2, # Lower temperature for more deterministic extraction
n=1
)
return response.choices[0].message.content
except Exception as e:
return f"An error occurred: {e}"
# Example usage:
user_text = "Dr. Eleanor Vance from Acme Corp visited Paris last week to meet with Jean Dupont of Beta Innovations."
entities = "person names, organization names, and locations"
extracted_info = extract_entities_from_text(user_text, entities)
print(extracted_info)

When the script runs, it sends the text and the extraction instruction to the LLM. The LLM then processes this and returns the requested information.

Extracted Information:
**Person Names:**
* Dr. Eleanor Vance
* Jean Dupont
**Organization Names:**
* Acme Corp
* Beta Innovations
**Locations:**
* Paris

In this chapter, we explored how to engineer prompts to guide LLMs in extracting specific information. While there isn’t a literal ‘FIND’ command, by clearly specifying what to look for, the source text, and the desired output format, you can effectively instruct LLMs to locate and present relevant details.

Mastering these techniques involves being specific in your instructions, providing clear context, requesting structured output, and iterating on your prompts. This capability is crucial for tasks like data mining from unstructured text, populating databases, or quickly identifying key information in documents.