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

Fix OpenAI returns "Unrecognized request argument supplied: api_key" #54

Merged
merged 1 commit into from
Dec 12, 2023

Conversation

eltonfonseca
Copy link
Contributor

@eltonfonseca eltonfonseca commented Dec 12, 2023

Hello guys!

I was testing this lib, when I came across the error Unrecognized request argument supplied: api_key that ChatGPT than returned when execute the LLMChain.run

So I looked deeper into what the problem was and discovered that this line bellow:

def for_api(%ChatOpenAI{} = openai, messages, functions) do
  %{
    model: openai.model,
    api_key: openai.api_key, # this line is not necessary, because this go into body request
    temperature: openai.temperature,
    frequency_penalty: openai.frequency_penalty,
    n: openai.n,
    stream: openai.stream,
    messages: Enum.map(messages, &ForOpenAIApi.for_api/1),
    response_format: set_response_format(openai)
  }
  |> Utils.conditionally_add_to_map(:seed, openai.seed)
  |> Utils.conditionally_add_to_map(:functions, get_functions_for_api(functions))
end

When I removed the line api_key: openai.api_key the response is success from ChatGTP.

Error with api_key sended on body:

{
    "error": {
	"message": "Unrecognized request argument supplied: api_key",
	"type": "invalid_request_error",
	"param": null,
	"code": null
  }
}

After fix this, API returns sucess!

{
	"id": "chatcmpl-8UkwsICWfiAusriGgaMCR9IH0SjZX",
	"object": "chat.completion",
	"created": 1702341002,
	"model": "gpt-4-0613",
	"choices": [
		{
			"index": 0,
			"message": {
				"role": "assistant",
				"content": "A palavra \"paralelepípedo\" tem 14 letras."
			},
			"finish_reason": "stop"
		}
	],
	"usage": {
		"prompt_tokens": 18,
		"completion_tokens": 14,
		"total_tokens": 32
	},
	"system_fingerprint": null
}

Sorry if I'm wrong

@brainlid
Copy link
Owner

Thanks for reporting this issue! You are correct, that should not be passed to the OpenAI API.

❤️💛💙💜

@brainlid brainlid merged commit b0d89a0 into brainlid:main Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants