Man

Development & AI | Alper Akgun

LLM Prompting

September, 2023

To get the best output from an LLM, consider starting with the use of better prompts as your initial strategy. Only turn to finetuning or more advanced, albeit costlier models when your prompts have reached their peak effectiveness. Failing to do so may result in missed opportunities for cost savings and enhanced performance.

LLMs are fundamentally completion models. They perceive the task as filling in a document with both human and AI responses.

Prompts & Templates

Prompts, are textual instructions and information conveyed to large language models by humans. Chat-based models undergo fine-tuning using datasets that instruct them on responding to human-provided instructions.

Here's an example raw system prompt for a model. The engine trims the instructions and responses, so that the user feels like chatting.

This is a system prompt.

### Instruction: This is usually a user prompt.
Blah?

### Response: This is my response, as an AI agent.

Blah blah!

### Instruction: this is another user prompt...
            

Most LLM models are finetuned using a particular template, and they will behave suboptimally when prompted with different templates. When working with a model, understand the LLM's training and system template.


[INST]
  << SYS >>
      You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
  << /SYS >>

{prompt}
[/INST]


-- Others
### Human: your prompt here
### Assistant:
            

Few-shot prompting in the context of prompting refers to the ability of a language model, to generate meaningful responses or complete tasks with only a small number of examples or instructions. This approach is particularly valuable when you want the model to perform a specific task without extensive training data. In the following example, the LLM will learn from the examples and responde.

"This is awesome!" ➡️ Positive
"This is bad!" ➡️ Negative
"Wow that movie was great!" ➡️ Positive
"What a horrible show!" ➡️ Negative
"You rock" ➡️ 
            

You can force an LLM to responde in a specific format, or a specific manner using few-shot prompting.

Ruby:
puts "I love the Ruby language"

A JS Object file for age equals 12:
{ age: 12 }

Human: Provide a JSON to answer this question: `Give me a JSON for a student?`

Assistant: { firstName: 'Alice', lastName: 'Doe', grade: 'A++' }

Human: Provide a JSON to answer this question: `Give me a JSON for a book?`

Assistant: { title: 'Alice in Wonderland', pages: 200 }

Human: Provide a JSON to answer this question: `Give me a country`

Assistant:
            

Instruction prompting involves providing an LLM with a specific task or direction. Despite its simplicity, LLMs can comprehend and execute highly intricate instructions. Expect to encounter instruction prompting frequently throughout this course.

Role prompting is a method employed to influence the style of AI-generated text and enhance its problem-solving capabilities. Utilizing role prompting is straightforward; you merely instruct the AI to assume roles like "food critic" or "detective."

Mixing various prompting strategies can yield more robust and efficient prompts. Nearly all prompts you create will incorporate multiple strategies. As you explore and enhance your prompts, contemplate the synergy of different techniques to attain your intended outcomes. Giving a role, an instruction, a question, a context and some examples can be very powerful.

RAF (Retrieval Augmented Generation) takes an input and adds external set of relevant documents as a context.

Self-Consistency aims to replace chain-of-thought prompting by sampling diverse reasoning paths through few-shot CoT.

Tree of Thoughts explores thoughts as intermediate steps towards solving a problem. Search algorithms enable evaluation of thoughts.

Generate Knowledge Prompting involves generating some knowledges about the entities in some input and adding explicit "Knowledge" entries into the prompt input.

ReAct framework; LLMs generate both reasoning traces and task-specific actions. LLms can interact with external tools to get more information that leads to more reliable output. This makes ReAct better than CoT prompting.

LLM Manipulation

In prompting the following can help improve results:
- A facts section
- A rules section
- Escaping data, or results from other context
- Formatting data like YAML, CSV, JSON etc
- Step by step thinking

LLM Settings

Managing LLM settings like temperature, top-p, maximum length, and related parameters is crucial when working with language models. These settings provide control over the model's output, tailoring it to particular tasks or applications. They govern factors like response randomness, response length, and repetition frequency, all of which enhance your interactions with the AI.