# Custom Agents Guide

#### TOC

- [Adding Custom Agents](#adding-custom-agents)
  - [`A` Add through the Agent Marketplace](#a-add-through-the-agent-marketplace)
  - [`B` Create a Custom Agent](#b-create-a-custom-agent)
- [Basic Concepts of Prompts](#basic-concepts-of-prompts)
  - [How to write a structured prompt](#how-to-write-a-structured-prompt)
  - [How to improve quality and effectiveness](#how-to-improve-quality-and-effectiveness)
- [Model Concepts](#model-concepts)
  - [ChatGPT](#chatgpt)
- [Model Parameter Concepts](#model-parameter-concepts)
  - [`temperature`](#temperature)
  - [`top_p`](#top_p)
  - [`presence_penalty`](#presence_penalty)
  - [`frequency_penalty`](#frequency_penalty)
- [Further Reading](#further-reading)

## Adding Custom Agents

As the fundamental unit of HanzoChat, adding and iterating on agents is crucial. Now you can add agents to your favorites list in two ways:

### `A` Add through the Agent Marketplace

If you're new to writing prompts, you might want to browse the Agent Marketplace in HanzoChat. Here, you can find commonly used agents submitted by others and add them to your list with just one click, making it very convenient.

![](https://github-production-user-asset-6210df.s3.amazonaws.com/17870709/279588466-4c32041b-a8e6-4703-ba4a-f91b7800e359.png)

### `B` Create a Custom Agent

When you need to handle specific tasks, you'll want to consider creating a custom agent to help you solve the problem. You can add and configure the agent in detail using the following steps:

![](https://github-production-user-asset-6210df.s3.amazonaws.com/17870709/279587283-a3ea8dfd-70fb-47ee-ab00-e3911ac6a939.png)
![](https://github-production-user-asset-6210df.s3.amazonaws.com/17870709/279587292-a3d102c6-f61e-4578-91f1-c0a4c97588e1.png)

> \[!NOTE]
>
> Quick setting tip: You can conveniently modify the prompt by using the quick edit button in the sidebar.

![](https://github-production-user-asset-6210df.s3.amazonaws.com/17870709/279587294-388d1877-193e-4a50-9fe8-8fbcc3ccefa0.png)
![](https://github-production-user-asset-6210df.s3.amazonaws.com/17870709/279587298-333da153-13b8-4557-a0a2-cff55e7bc1c0.png)

Continue reading to understand the writing techniques and common model parameter settings for prompts.

<br/>

## Basic Concepts of Prompts

Generative AI is very useful, but it requires human guidance. In most cases, generative AI is like a capable intern who needs clear instructions to perform well. Being able to guide generative AI correctly is a powerful skill. You can guide generative AI by sending a prompt, which is typically a text instruction. The prompt is the input you provide to the agent, and it will influence the output. A good prompt should be structured, clear, concise, and directive.

### How to write a structured prompt

> \[!TIP]
>
> A structured prompt refers to the construction of the prompt having clear logic and structure. For example, if you want the model to generate an article, your prompt may need to include the topic of the article, its outline, and its style.

Let's look at a basic example of a discussion question:

> _"What are the most urgent environmental issues our planet faces, and what can individuals do to help address these problems?"_

We can turn this into a simple prompt by answering the following question upfront.

```
Answer the following question:
What are the most urgent environmental issues our planet faces, and what can individuals do to help address these problems?
```

Since the results generated by this prompt are inconsistent, with some only consisting of one or two sentences, it is not ideal for a typical discussion answer that should have multiple paragraphs. A good prompt should provide specific formatting and content instructions. You need to eliminate ambiguity in the language to improve consistency and quality. Here's a better prompt:

```
Write an in-depth essay that includes an introduction, body paragraphs, and a conclusion, answering the following question:
What are the most urgent environmental issues our planet faces, and what can individuals do to help address these problems?
```

The second prompt generates longer outputs with better structure. The use of the word "essay" in the prompt is intentional because agent can understand the definition of an essay, making it more likely to generate coherent and structured answers.

<br/>

### How to improve quality and effectiveness

> \[!TIP]
>
> There are several ways to improve the quality and effectiveness of prompts:
>
> - Be as clear as possible about your needs. The model will try to fulfill your requirements, so if your requirements are not clear, the output may not meet your expectations.
> - Use correct grammar and spelling. The model will try to mimic your language style, so if your language style is problematic, the output may also be problematic.
> - Provide sufficient contextual information. The model will generate output based on the contextual information you provide, so if you provide insufficient contextual information, it may not be able to generate the desired results.

After formulating effective prompts for discussion questions, you now need to refine the generated results. This may involve adjusting the output to fit constraints such as word count or combining concepts from different generated results.

A simple iteration method is to generate multiple outputs and examine them to understand the concepts and structure used by agent. Once the outputs have been evaluated, you can select the most suitable ones and combine them into a coherent answer. Another iterative approach is to start gradually and then expand step by step. This requires more than one prompt: an initial prompt for writing the first one or two paragraphs, followed by additional prompts to expand on the content already written. Here's a potential philosophical discussion prompt:

> _"Is mathematics invented or discovered? Explain your answer with careful reasoning."_

Add it to a simple prompt like this:

```
Write an opening paragraph answering the following question:
Is mathematics invented or discovered? Explain your answer with careful reasoning.
```

I generated a few answers and found one that I liked:

```
Philosophers and mathematicians have debated whether mathematics is invented or discovered.
Some argue that the concepts and principles of mathematics exist independently of human thought, so they can be considered discovered rather than invented.
For example, the concepts of numbers, ratios, and geometric shapes are observed in the natural world and can be abstracted and studied systematically.
```

It's a good start. Then I used another prompt to expand:

```
I'm writing a detailed essay answering the following prompt:
Is mathematics invented or discovered? Explain your answer with careful reasoning. Here's where I'm at so far: Philosophers and mathematicians have debated whether mathematics is invented or discovered.
Some argue that the concepts and principles of mathematics exist independently of human thought, so they can be considered discovered rather than invented.
For example, the concepts of numbers, ratios, and geometric shapes are observed in the natural world and can be abstracted and studied systematically. Write the next paragraph of my essay.
```

I used this prompt to generate several expansions and selected one that I liked, resulting in the following:

```
Philosophers and mathematicians have debated whether mathematics is invented or discovered.
Some argue that the concepts and principles of mathematics exist independently of human thought, so they can be considered discovered rather than invented.
For example, the concepts of numbers, ratios, and geometric shapes are observed in the natural world and can be abstracted and studied systematically.
On the other hand, some argue that mathematics is largely an invention of human thought.
Mathematical principles are based on logical reasoning, which is a characteristic of human thought.
For example, Euclidean geometry is based on abstract axioms and assumptions that are accepted as true without needing to be proven.
Therefore, geometry can be seen as an invention of human thought rather than a discovery.
Similarly, mathematical formulas and equations are used to model and predict physical phenomena, which are the result of human reasoning.
```

Using expansion prompts, we can gradually write and iterate at each step. This is useful for situations where you need to generate higher-quality output and want to make incremental modifications.

<br/>

## Model Concepts

### ChatGPT

- **gpt-3.5-turbo**: The fastest generating chatgpt model currently available, but it may sacrifice some text quality. The context length is 4k.
- **gpt-3.5-turbo-16k**: Same as gpt-4, but with an increased context limit of 16k tokens and a higher cost rate.
- **gpt-4**: ChatGPT 4.0 has improved language understanding and generation capabilities compared to 3.5. It has a better understanding of context and can generate more accurate and natural responses. This is due to improvements in the GPT-4 model, including better language modeling and deeper semantic understanding, but it may be slower than other models. The context length is 8k.
- **gpt-4-32k**: Same as gpt-4, but with an increased context limit of 32k tokens and a higher cost rate.

<br/>

## Model Parameter Concepts

LLM may seem magical, but it is essentially a probability problem. The neural network generates a set of candidate words from the pre-trained model based on the input text and selects the highest probability as the output. Most of the related parameters are about sampling (i.e., how to select the output from the candidate words).

### `temperature`

Controls the randomness of the model's output. Higher values increase randomness. In general, if you input the same prompt multiple times, the model's output will be different each time.

- Set to 0 for a fixed output for each prompt.
- Lower values make the output more focused and deterministic.
- Higher values make the output more random and creative.

> \[!NOTE]
>
> Generally, the longer and clearer the prompt, the better the quality and confidence of the generated output. In this case, you can increase the temperature value. Conversely, if the prompt is short and ambiguous, setting a higher temperature value will make the model's output less stable.

<br/>

### `top_p`

Top-p nucleus sampling is another sampling parameter that is different from temperature. Before the model generates the output, it generates a set of tokens. In top-p sampling mode, the candidate word list is dynamic and selected from the tokens based on a percentage. Top-p introduces randomness to the selection of tokens, allowing other high-scoring tokens to have a chance of being selected instead of always choosing the highest-scoring one.

> \[!NOTE]
>
> Top-p is similar to randomness. In general, it is not recommended to change it together with the randomness parameter, temperature.

<br/>

### `presence_penalty`

The presence penalty parameter can be seen as a punishment for repetitive content in the generated text. When this parameter is set high, the generative model will try to avoid generating repeated words, phrases, or sentences. Conversely, if the presence penalty parameter is low, the generated text may contain more repeated content. By adjusting the value of the presence penalty parameter, you can control the originality and diversity of the generated text. The importance of this parameter is mainly reflected in the following aspects:

- Increasing the originality and diversity of the generated text: In some application scenarios, such as creative writing or generating news headlines, it is desirable for the generated text to have high originality and diversity. By increasing the value of the presence penalty parameter, the probability of generating repeated content in the generated text can be effectively reduced, thereby improving its originality and diversity.
- Preventing generation loops and meaningless content: In some cases, the generative model may produce repetitive and meaningless text that fails to convey useful information. By appropriately increasing the value of the presence penalty parameter, the probability of generating this type of meaningless content can be reduced, thereby improving the readability and usefulness of the generated text.

> \[!NOTE]
>
> It is worth noting that the presence penalty parameter, along with other parameters such as temperature and top-p, collectively affect the quality of the generated text. Compared to other parameters, the presence penalty parameter focuses more on the originality and repetitiveness of the text, while the temperature and top-p parameters have a greater impact on the randomness and determinism of the generated text. By adjusting these parameters properly, comprehensive control of the quality of the generated text can be achieved.

<br/>

### `frequency_penalty`

Frequency penalty is a mechanism that penalizes frequent occurrences of new vocabulary in the generated text, reducing the likelihood of the model repeating the same words. The higher the value, the more likely it is to reduce repeated words.

- `-2.0` When the morning news starts playing, I noticed that my TV now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now now _(The most frequent word is "now" with a percentage of 44.79%)_
- `-1.0` He always watches the news in the morning, watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching watching _(The most frequent word is "watching" with a percentage of 57.69%)_
- `0.0` When the morning sun shines into the small restaurant, a tired mailman appears at the door, holding a bag of mail in his hand. The owner warmly prepares breakfast for him, and he starts sorting the mail while enjoying his breakfast. **_(The most frequent word is "the" with a percentage of 8.45%)_**
- `1.0` A deep sleep girl is awakened by a warm sunbeam. She sees the first ray of sunlight in the morning, surrounded by the sounds of birds and the fragrance of flowers, everything is full of vitality. _(The most frequent word is "the" with a percentage of 5.45%)_
- `2.0` Every morning, he sits on the balcony to have breakfast. In the gentle sunset, everything looks very peaceful. However, one day, as he was about to pick up his breakfast, an optimistic little bird flew by, bringing him a good mood for the day. _(The most frequent word is "the" with a percentage of 4.94%)_

## Further Reading

- **Learn Prompting** - <https://learnprompting.org/docs/intro>
