Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown error due to some settings #7

Closed
doggy8088 opened this issue Mar 26, 2024 · 5 comments
Closed

Unknown error due to some settings #7

doggy8088 opened this issue Mar 26, 2024 · 5 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@doggy8088
Copy link
Contributor

The following code will produce Response status code does not indicate success: 400 (Bad Request). error. I have no idea.

async Task Main()
{
	var googleAI = new GoogleAI(apiKey: Util.GetPassword("GEMINI_API_KEY"));
	var model = googleAI.GenerativeModel(model: Model.GeminiPro,
		generationConfig: new GenerationConfig()
		{
			TopK = 1,
			TopP = 1,
			Temperature = 0.9f
		},
		safetySettings: new List<SafetySetting>()
		{
			new SafetySetting() { Category = HarmCategory.HarmCategoryHarassment, Threshold = HarmBlockThreshold.BlockOnlyHigh },
			new SafetySetting() { Category = HarmCategory.HarmCategoryHateSpeech, Threshold = HarmBlockThreshold.BlockOnlyHigh },
			new SafetySetting() { Category = HarmCategory.HarmCategorySexuallyExplicit, Threshold = HarmBlockThreshold.BlockOnlyHigh },
			new SafetySetting() { Category = HarmCategory.HarmCategoryDangerousContent, Threshold = HarmBlockThreshold.BlockOnlyHigh }
		});

	var count = await model.CountTokens("Hello World");
	count.Dump();
}

LINQPad Query: https://share.linqpad.net/entk5npb.linq

@doggy8088
Copy link
Contributor Author

doggy8088 commented Mar 26, 2024

I know the root cause now. The generationConfig and the safetySettings affected its request payload. That's invalid.

Here is what I think. The technical details should be hidden from the library user. The model should be reuse between generatecontent and counttokens. So maybe we can change CountTokens a bit that remove these two parts of the payload (generationConfig and the safetySettings) from the request automatically?

At this moment, I have to write two different model object to handle two different API methods:

var modelCountTokens = googleAI.GenerativeModel(model: Model.GeminiPro);

var modelGenerateContent = googleAI.GenerativeModel(model: Model.GeminiPro,
	generationConfig: new GenerationConfig()
	{
		TopK = 1,
		TopP = 1,
		Temperature = 0.9f
	},
	safetySettings: new List<SafetySetting>()
	{
		new SafetySetting() { Category = HarmCategory.HarmCategoryHarassment, Threshold = HarmBlockThreshold.BlockOnlyHigh },
		new SafetySetting() { Category = HarmCategory.HarmCategoryHateSpeech, Threshold = HarmBlockThreshold.BlockOnlyHigh },
		new SafetySetting() { Category = HarmCategory.HarmCategorySexuallyExplicit, Threshold = HarmBlockThreshold.BlockOnlyHigh },
		new SafetySetting() { Category = HarmCategory.HarmCategoryDangerousContent, Threshold = HarmBlockThreshold.BlockOnlyHigh }
	});

@jochenkirstaetter
Copy link
Contributor

Is this related to #8 while using SafetySettings associated with PaLM 2 models?

@jochenkirstaetter jochenkirstaetter added duplicate This issue or pull request already exists enhancement New feature or request labels Mar 26, 2024
@jochenkirstaetter
Copy link
Contributor

New version 0.9.1 should make it clearer.

Plus, there is the official documentation about Safety settings in Gemini that apply.

Hope this helps. Closing the issue (for now).

@doggy8088
Copy link
Contributor Author

The current (v0.9.1) usage is much better. I just move the generationConfig and safetySettings settings to the GenerateContent() method.

var response = await model.GenerateContent(prompt,
	generationConfig: new GenerationConfig()
	{
		TopK = 1,
		TopP = 1,
		Temperature = 0.9f
	},
	safetySettings: new List<SafetySetting>()
	{
		new SafetySetting() { Category = HarmCategory.HarmCategoryHarassment, Threshold = HarmBlockThreshold.BlockOnlyHigh },
		new SafetySetting() { Category = HarmCategory.HarmCategoryHateSpeech, Threshold = HarmBlockThreshold.BlockOnlyHigh },
		new SafetySetting() { Category = HarmCategory.HarmCategorySexuallyExplicit, Threshold = HarmBlockThreshold.BlockOnlyHigh },
		new SafetySetting() { Category = HarmCategory.HarmCategoryDangerousContent, Threshold = HarmBlockThreshold.BlockOnlyHigh }
	});

@jochenkirstaetter
Copy link
Contributor

Cool, thanks for the feedback.
Actually, you can use both locations to specify GenerationConfig and SafetySettings.

The ones you configure at the model level apply to all requests following whereas the attributes you pass into a request are for that request only. See GenerateContent(string) as an example.

Hmm, just noticed that same is currently missing with GenerateContent(request). That method should have the following block.

            request.GenerationConfig ??= _generationConfig;
            request.SafetySettings ??= _safetySettings;
            request.Tools ??= _tools;

Going to add this and to GenerateContentStream, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants