Exploring Model Parameters

Welcome back! In the previous lesson, you learned how to send a simple message to DeepSeek's language model and receive a response using Go. Now, let's take a step further by exploring model parameters that allow you to customize the AI's responses. These parameters are essential for tailoring the model's behavior to your needs. In this lesson, we will focus on four key parameters: MaxTokens, Temperature, PresencePenalty, and FrequencyPenalty. Understanding these parameters will help you control the creativity, length, and content of the AI's explanations.

Using openai.Int and openai.Float

When setting parameters in the openai-go client, you may notice that some fields require you to wrap values using helper functions like openai.Int() and openai.Float(). This is because the API expects pointers to these values, allowing you to omit parameters by passing nil. Using openai.Int() and openai.Float() makes it easy to provide optional values in a type-safe way.

For example:

  • openai.Int(150) returns a pointer to the integer value 150.
  • openai.Float(0.6) returns a pointer to the float value 0.6.

This pattern is used for all optional numeric parameters in the API.

Controlling Response Length with MaxTokens

The MaxTokens parameter sets a hard limit on the number of tokens the AI can generate in its response. A "token" can be a whole word or just part of a word. For example, "tutor" might be one token, while "explanation" could be split into multiple tokens. The exact number of tokens depends on the language and the model.

When you set MaxTokens, you specify the maximum number of tokens the AI can produce. This is a strict limit, meaning the model will stop generating text once it reaches this count, even if the answer is incomplete.

Here's how you can set MaxTokens to 150 in Go:

By setting MaxTokens to 150, you limit the length of the AI's response. This is useful for managing usage and controlling the cost of API requests. Keep in mind that this parameter does not make the model summarize or shorten its response; it simply stops the output when the limit is reached.

Exploring Temperature

The Temperature parameter controls the randomness or creativity of the AI's responses. A lower value, such as 0.2, makes the output more focused and predictable. A higher value, like 0.8, encourages the AI to generate more diverse and creative responses.

Here's how you can set Temperature to 0.6 in Go:

With a Temperature of 0.6, the AI is likely to provide an explanation that balances creativity and factual accuracy. Experimenting with different values will help you find the right setting for your use case. Lower values are good for precise answers, while higher values are better for creative or varied responses.


Encouraging New Topics with PresencePenalty

The PresencePenalty parameter encourages the AI to introduce new concepts in its explanations. It works by penalizing the model for using words that have already appeared in the conversation, promoting diversity in the dialogue. A low value, such as 0.0, means the AI is less discouraged from repeating words. A higher value, like 1.0, strongly encourages the AI to explore new topics.

Here's how you can set PresencePenalty to 0.5 in Go:

With a PresencePenalty of 0.5, the AI is more likely to introduce new ideas and provide varied explanations. This is useful when you want to expose users to a broader range of related topics.

Reducing Repetition with FrequencyPenalty

The FrequencyPenalty parameter helps reduce repetition in the AI's explanations by discouraging the repeated use of the same words or phrases. A low value, such as 0.0, allows for more repetition, which can be useful for reinforcing key concepts. A higher value, such as 1.0, reduces repetition, promoting more dynamic and varied explanations.

While PresencePenalty and FrequencyPenalty both influence repetition, they serve different purposes:

  • PresencePenalty: Encourages the AI to bring up new concepts by penalizing the use of words already present in the conversation.
  • FrequencyPenalty: Reduces repetition by penalizing the repeated use of the same words or phrases within a single response.

Here's how you can set FrequencyPenalty to 0.2 in Go:

By applying a FrequencyPenalty of 0.2, you can reduce redundancy in the AI's explanations while still allowing for some repetition when necessary.


Example: Implementing Model Parameters in Code

Let's bring it all together with a complete Go code example that incorporates all four parameters:

In this example, all four parameters are used to customize the AI's response. Notice how each numeric parameter is wrapped with openai.Int() or openai.Float(). This is required by the SDK to properly handle optional values.


Summary and Preparation for Practice

In this lesson, you learned how to use model parameters to customize AI responses in Go, and how to use the openai.Int() and openai.Float() helpers to set these parameters. You explored the Temperature, MaxTokens, PresencePenalty, and FrequencyPenalty parameters and saw how to apply them in code. These tools allow you to control the creativity, length, and content of the AI's explanations.

As you move on to the practice exercises, try experimenting with different parameter settings to see their effects firsthand. This hands-on practice will reinforce what you've learned and prepare you for the next unit, where you'll explore more advanced features.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal