> ## Documentation Index
> Fetch the complete documentation index at: https://docs.galileo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Prompts

> Learn how to create and use prompt templates in experiments

{/*<!-- markdownlint-enable MD044 -->*/}

Prompts in Galileo allow you to create, store, and reuse LLM prompts across your experiments. They provide a structured way to manage your LLM interactions.

## Prompts in the Galileo console

<img src="https://mintcdn.com/v2galileo/agrnpHSizEFGQdED/images/prompts-in-console.png?fit=max&auto=format&n=agrnpHSizEFGQdED&q=85&s=25a6b026f6debf641fff80ad27eb86e1" alt="Prompts in the Galileo console" width="2532" height="886" data-path="images/prompts-in-console.png" />

The **Prompts** page of the Galileo console shows you the existing prompts associated with your selected project. Click on **All prompts** to view all prompts in your organization. Click the **Create Prompt** button to create a new prompt.

## Prompts in code

### Create prompts

<CodeGroup>
  ```python Python theme={null}
  from galileo import Message, MessageRole
  from galileo.prompts import create_prompt

  # Create a prompt with system and user messages
  prompt = create_prompt(
      name="storyteller-prompt",
      template=[
          Message(role=MessageRole.system,
                  content="You are a great storyteller."),
          Message(role=MessageRole.user,
                  content="""
                  Please write a short story about
                  the following topic: {{topic}}
                  """)
      ]
  )
  ```

  ```python Python (Beta) theme={null}
  from galileo import Message, MessageRole, Prompt

  # Create a prompt with system and user messages
  prompt = Prompt(
      name="storyteller-prompt",
      messages=[
          Message(role=MessageRole.system,
                  content="You are a great storyteller."),
          Message(role=MessageRole.user,
                  content="""
                  Please write a short story about
                  the following topic: {{topic}}
                  """)
      ],
  )
  prompt.create()
  ```

  ```typescript TypeScript theme={null}
  import { createPrompt} from "galileo";

  const template = await createPrompt({
    template: [
      {
        role: "system",
        content: "You are a great storyteller."
      },
      {
        role: "user",
        content: `
          Please write a short story about
          the following topic: {topic}
          `
      },
    ],
    name: "storyteller-prompt",
  });
  ```
</CodeGroup>

#### Connect prompt templates to dataset inputs

When you use datasets in Galileo, the attributes stored in the input in your dataset are made available to your prompt templates using mustache templating.  This allows you to create dynamic prompts that adapt to the data in each row.

Suppose you have the following dataset:

<CodeGroup>
  ```python Python theme={null}
  test_data = [
      { "input": { "city": "Rome, Italy", "days": "5" } },
      { "input": { "city": "Paris, France", "days": "3" } },
  ]
  ```

  ```typescript TypeScript theme={null}
  const testData = [
      { input: { "city": "Rome, Italy", "days": "5" } },
      { input: { "city": "Paris, France", "days": "3" } },
  ];
  ```
</CodeGroup>

To reference fields from your dataset in your prompt, use double curly braces:

<CodeGroup>
  ```python Python theme={null}
  from galileo import Message, MessageRole

  message = Message(role=MessageRole.user,
          content="""
          Plan a {{ days }}-day travel itinerary
          for a trip to {{ city }}.
          """)
  ```

  ```typescript TypeScript theme={null}
  {
    role: "user",
    content: `
      Plan a {{ days }}-day travel itinerary
      for a trip to {{ city }}.
      `
  }
  ```
</CodeGroup>

* `{{ city }}` will be replaced with the value of the `city` field inside the `input` dictionary.
* `{{ days }}` will be replaced with the value of the `days` field inside the `input` dictionary.

### Get prompts

Once prompts have been created in Galileo, they can be retrieved by name. For project level prompts, pass in either the project name or Id.

<CodeGroup>
  ```python Python theme={null}
  from galileo.prompts import get_prompt

  # Get an existing prompt
  prompt = get_prompt(
      name="storyteller-prompt"
  )
  ```

  ```python Python (Beta) theme={null}
  from galileo import Prompt

  # Get an existing prompt
  prompt = Prompt.get(name="storyteller-prompt")
  ```

  ```typescript TypeScript theme={null}
  import { getPrompt } from "galileo";

  const prompt = await getPrompt({
    name: "storyteller-prompt",
  });
  ```
</CodeGroup>

### List prompts

To list all prompt templates in a project or organization:

<CodeGroup>
  ```python Python theme={null}
  from galileo.prompts import get_prompts

  # List all prompt templates in a project
  templates = get_prompts()

  # Print template names
  for template in templates:
      print(f"Template: {template.name}")
  ```

  ```python Python (Beta) theme={null}
  from galileo import Prompt

  # List all prompt templates
  templates = Prompt.list()

  # Print template names
  for template in templates:
      print(f"Template: {template.name}")
  ```

  ```typescript TypeScript theme={null}
  import { getPrompts } from "galileo";

  // Search list of prompts by name
  const prompts = await getPrompts({
    name: "story",
  });

  // Log the prompt names
  prompts.forEach((prompt) => console.log(prompt.name));
  ```
</CodeGroup>

### Delete prompts

To delete a prompt:

<CodeGroup>
  ```python Python theme={null}
  from galileo.prompts import delete_prompt

  # Delete a prompt
  delete_prompt(
      name="storyteller-prompt"
  )
  ```

  ```python Python (Beta) theme={null}
  from galileo import Prompt

  # Get and delete a prompt
  prompt = Prompt.get(name="storyteller-prompt")
  if prompt is None:
      raise ValueError("Prompt 'storyteller-prompt' not found")
  prompt.delete()
  ```

  ```typescript TypeScript theme={null}
  import { deletePrompt } from "galileo";

  await deletePrompt({
    name: "storyteller-prompt",
  });
  ```
</CodeGroup>

### Use prompts in experiments

Prompts can be used in experiments to evaluate different prompt templates:

<CodeGroup>
  ```python Python theme={null}
  from galileo.datasets import get_dataset
  from galileo.experiments import run_experiment
  from galileo.prompts import get_prompt
  from galileo import GalileoMetrics

  # Get an existing dataset
  dataset = get_dataset(
      name="countries"
  )

  # Get an existing prompt
  prompt = get_prompt(
      name="geography-prompt"
  )

  # Run an experiment with the dataset and prompt
  results = run_experiment(
      "geography-experiment",
      dataset=dataset,
      prompt_template=prompt,
      metrics=[GalileoMetrics.completeness],
      project="my-project",
  )
  ```

  ```python Python (Beta) theme={null}
  from galileo import Dataset, Experiment, Metric, Prompt

  # Get an existing dataset
  dataset = Dataset.get(name="countries")
  if dataset is None:
      raise ValueError("Dataset 'countries' not found")

  # Get an existing prompt
  prompt = Prompt.get(name="geography-prompt")
  if prompt is None:
      raise ValueError("Prompt 'geography-prompt' not found")

  # Create and run the experiment in one step
  # (create() triggers the run automatically)
  experiment = Experiment(
      name="geography-experiment",
      dataset=dataset,
      prompt=prompt,
      metrics=[Metric.metrics.completeness],
      project_name="my-project",
  )
  experiment.create()
  ```

  ```typescript TypeScript theme={null}
  import {
    GalileoMetrics, getDataset,
    getPrompt, runExperiment
  } from "galileo";

  // Get an existing dataset
  const dataset = await getDataset({
      name: "countries"
  });

  // Get an existing prompt
  const prompt = await getPrompt({
      name: "geography-prompt",
    });

  // Run an experiment with the dataset and prompt
  await runExperiment({
      name: "geography-experiment",
      dataset: dataset,
      promptTemplate: prompt,
      metrics: [GalileoMetrics.correctness],
      projectName: "my-project",
    });
  ```
</CodeGroup>

### Best practices

When working with prompts:

1. Use descriptive names that reflect the prompt's purpose
2. Include clear system messages to set context
3. Document any required input variables
4. Version your prompt templates appropriately
5. Test prompt templates with various inputs before using in production

## Related resources

<CardGroup cols={2}>
  <Card title="Datasets" icon="database" horizontal href="/sdk-api/experiments/datasets">
    Learn about more datasets, the data driving your experiments.
  </Card>

  <Card title="Experiments" icon="flask" horizontal href="/sdk-api/experiments/experiments">
    Learn how to use datasets and experiments to improve your application.
  </Card>
</CardGroup>
