go-openai
go-openai copied to clipboard
Updated checkPromptType function to handle prompt list in completions
Describe the change The checkPromptType function has been updated to support validation of []interface{} slices, ensuring that each element in the slice is of type string. Currently if list of string is provided in prompt field ErrCompletionRequestPromptTypeNotSupported error is returned. Example: for this completions request-
{
"model": "gemma-vllm",
"prompt": ["Explain transformers in atelast 100 words or more","Hello whoare you"],
"max_tokens": 150
}
This error will occur-
Completion error: the type of CompletionRequest.Prompt only supports string and []string
This is because arrays in JSON are decoded into Go as slices of interface{} ([]interface{}), not as slices of a specific type like []string. Therefore, when a JSON request containing a list of strings is unmarshalled, the resulting value of request.Prompt is a []interface{}. The checkPromptType function only checked for string and []string, so its returning false when request.Prompt is a []interface{} even if all elements within that slice are strings, because []interface{} is not the same type as []string.
This change allows better validation of prompt types, accommodating the scenario where the prompt is provided as a list of strings in an interface{} slice.
Describe your solution The solution involves adding a new type check for []interface{} in the checkPromptType function. The code checks if prompt is of type []interface{} and, if so, iterates over the slice to ensure all elements are strings. If any element is not a string, the validation fails. This logic is integrated alongside the existing checks for string and []string types. This enhancement ensures that the function properly handles various input types for prompts and raises the ErrCompletionRequestPromptTypeNotSupported error if an unsupported type is provided.