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

[Chat Completions] Request payload definition #21 #263

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

jihyunmoon16
Copy link
Contributor

@jihyunmoon16 jihyunmoon16 commented Aug 24, 2024

CreateChatCompletionRequest

참고 링크: https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2024-06-01/inference.json


CreateChatCompletionRequest

"createChatCompletionRequest": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/chatCompletionsRequestCommon"
          },
          {
            "properties": {
              "messages": {
                "description": "A list of messages comprising the conversation so far. [Example Python code](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb).",
                "type": "array",
                "minItems": 1,
                "items": {
                  "$ref": "#/components/schemas/chatCompletionRequestMessage"
                }
              },
              "data_sources": {
                "type": "array",
                "description": "  The configuration entries for Azure OpenAI chat extensions that use them.\n  This additional specification is only compatible with Azure OpenAI.",
                "items": {
                  "$ref": "#/components/schemas/azureChatExtensionConfiguration"
                }
              },
              "n": {
                "type": "integer",
                "minimum": 1,
                "maximum": 128,
                "default": 1,
                "example": 1,
                "nullable": true,
                "description": "How many chat completion choices to generate for each input message."
              },
              "seed": {
                "type": "integer",
                "minimum": -9223372036854775808,
                "maximum": 9223372036854775807,
                "default": 0,
                "example": 1,
                "nullable": true,
                "description": "If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result.Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend."
              },
              "logprobs": {
                "description": "Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the `content` of `message`. This option is currently not available on the `gpt-4-vision-preview` model.",
                "type": "boolean",
                "default": false,
                "nullable": true
              },
              "top_logprobs": {
                "description": "An integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability. `logprobs` must be set to `true` if this parameter is used.",
                "type": "integer",
                "minimum": 0,
                "maximum": 5,
                "nullable": true
              },
              "response_format": {
                "type": "object",
                "description": "An object specifying the format that the model must output. Used to enable JSON mode.",
                "properties": {
                  "type": {
                    "$ref": "#/components/schemas/chatCompletionResponseFormat"
                  }
                }
              },
              "tools": {
                "description": "A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.",
                "type": "array",
                "minItems": 1,
                "items": {
                  "$ref": "#/components/schemas/chatCompletionTool"
                }
              },
              "tool_choice": {
                "$ref": "#/components/schemas/chatCompletionToolChoiceOption"
              },
              "functions": {
                "description": "Deprecated in favor of `tools`. A list of functions the model may generate JSON inputs for.",
                "type": "array",
                "minItems": 1,
                "maxItems": 128,
                "items": {
                  "$ref": "#/components/schemas/chatCompletionFunction"
                }
              },
              "function_call": {
                "description": "Deprecated in favor of `tool_choice`. Controls how the model responds to function calls. \"none\" means the model does not call a function, and responds to the end-user. \"auto\" means the model can pick between an end-user or calling a function.  Specifying a particular function via `{\"name\":\\ \"my_function\"}` forces the model to call that function. \"none\" is the default when no functions are present. \"auto\" is the default if functions are present.",
                "oneOf": [
                  {
                    "type": "string",
                    "enum": [
                      "none",
                      "auto"
                    ],
                    "description": "`none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function."
                  },
                  {
                    "type": "object",
                    "description": "Specifying a particular function via `{\"name\": \"my_function\"}` forces the model to call that function.",
                    "properties": {
                      "name": {
                        "type": "string",
                        "description": "The name of the function to call."
                      }
                    },
                    "required": [
                      "name"
                    ]
                  }
                ]
              }
            }
          }
        ],
        "required": [
          "messages"
        ]
      },

ChatCompletionsRequestCommon

      "chatCompletionsRequestCommon": {
        "type": "object",
        "properties": {
          "temperature": {
            "description": "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.\nWe generally recommend altering this or `top_p` but not both.",
            "type": "number",
            "minimum": 0,
            "maximum": 2,
            "default": 1,
            "example": 1,
            "nullable": true
          },
          "top_p": {
            "description": "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.\nWe generally recommend altering this or `temperature` but not both.",
            "type": "number",
            "minimum": 0,
            "maximum": 1,
            "default": 1,
            "example": 1,
            "nullable": true
          },
          "stream": {
            "description": "If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a `data: [DONE]` message.",
            "type": "boolean",
            "nullable": true,
            "default": false
          },
          "stop": {
            "description": "Up to 4 sequences where the API will stop generating further tokens.",
            "oneOf": [
              {
                "type": "string",
                "nullable": true
              },
              {
                "type": "array",
                "items": {
                  "type": "string",
                  "nullable": false
                },
                "minItems": 1,
                "maxItems": 4,
                "description": "Array minimum size of 1 and maximum of 4"
              }
            ],
            "default": null
          },
          "max_tokens": {
            "description": "The maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will be (4096 - prompt tokens).",
            "type": "integer",
            "default": 4096
          },
          "presence_penalty": {
            "description": "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.",
            "type": "number",
            "default": 0,
            "minimum": -2,
            "maximum": 2
          },
          "frequency_penalty": {
            "description": "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.",
            "type": "number",
            "default": 0,
            "minimum": -2,
            "maximum": 2
          },
          "logit_bias": {
            "description": "Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.",
            "type": "object",
            "nullable": true
          },
          "user": {
            "description": "A unique identifier representing your end-user, which can help Azure OpenAI to monitor and detect abuse.",
            "type": "string",
            "example": "user-1234",
            "nullable": false
          }
        }
      },

ChatCompletionRequestMessage

      "chatCompletionRequestMessage": {
        "type": "object",
        "properties": {
          "role": {
            "$ref": "#/components/schemas/chatCompletionRequestMessageRole"
          }
        },
        "discriminator": {
          "propertyName": "role",
          "mapping": {
            "system": "#/components/schemas/chatCompletionRequestMessageSystem",
            "user": "#/components/schemas/chatCompletionRequestMessageUser",
            "assistant": "#/components/schemas/chatCompletionRequestMessageAssistant",
            "tool": "#/components/schemas/chatCompletionRequestMessageTool",
            "function": "#/components/schemas/chatCompletionRequestMessageFunction"
          }
        },
        "required": [
          "role"
        ]
      },

ChatCompletionRequestMessageRole

      "chatCompletionRequestMessageRole": {
        "type": "string",
        "enum": [
          "system",
          "user",
          "assistant",
          "tool",
          "function"
        ],
        "description": "The role of the messages author.",
        "x-ms-enum": {
          "name": "ChatCompletionRequestMessageRole",
          "modelAsString": true,
          "values": [
            {
              "value": "system",
              "description": "The message author role is system."
            },
            {
              "value": "user",
              "description": "The message author role is user."
            },
            {
              "value": "assistant",
              "description": "The message author role is assistant."
            },
            {
              "value": "tool",
              "description": "The message author role is tool."
            },
            {
              "value": "function",
              "description": "Deprecated. The message author role is function."
            }
          ]
        }
      },

ChatCompletionRequestMessageSystem

      "chatCompletionRequestMessageSystem": {
        "allOf": [
          {
            "$ref": "#/components/schemas/chatCompletionRequestMessage"
          },
          {
            "type": "object",
            "properties": {
              "content": {
                "type": "string",
                "description": "The contents of the message.",
                "nullable": true
              }
            }
          }
        ],
        "required": [
          "content"
        ]
      },

ChatCompletionRequestMessageUser

   "chatCompletionRequestMessageUser": {
        "allOf": [
          {
            "$ref": "#/components/schemas/chatCompletionRequestMessage"
          },
          {
            "type": "object",
            "properties": {
              "content": {
                "oneOf": [
                  {
                    "type": "string",
                    "description": "The contents of the message."
                  },
                  {
                    "type": "array",
                    "description": "An array of content parts with a defined type, each can be of type `text` or `image_url` when passing in images. You can pass multiple images by adding multiple `image_url` content parts. Image input is only supported when using the `gpt-4-visual-preview` model.",
                    "minimum": 1,
                    "items": {
                      "$ref": "#/components/schemas/chatCompletionRequestMessageContentPart"
                    }
                  }
                ],
                "nullable": true
              }
            }
          }
        ],
        "required": [
          "content"
        ]
      },

ChatCompletionRequestMessageContentPart

      "chatCompletionRequestMessageContentPart": {
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/components/schemas/chatCompletionRequestMessageContentPartType"
          }
        },
        "discriminator": {
          "propertyName": "type",
          "mapping": {
            "text": "#/components/schemas/chatCompletionRequestMessageContentPartText",
            "image_url": "#/components/schemas/chatCompletionRequestMessageContentPartImage"
          }
        },
        "required": [
          "type"
        ]
      },

ChatCompletionRequestMessageAssistant

 "chatCompletionRequestMessageAssistant": {
        "allOf": [
          {
            "$ref": "#/components/schemas/chatCompletionRequestMessage"
          },
          {
            "type": "object",
            "properties": {
              "content": {
                "type": "string",
                "description": "The contents of the message.",
                "nullable": true
              },
              "tool_calls": {
                "type": "array",
                "description": "The tool calls generated by the model, such as function calls.",
                "items": {
                  "$ref": "#/components/schemas/chatCompletionMessageToolCall"
                }
              },
              "context": {
                "$ref": "#/components/schemas/azureChatExtensionsMessageContext"
              }
            }
          }
        ],
        "required": [
          "content"
        ]
      },

ChatCompletionRequestMessageTool

 "chatCompletionRequestMessageTool": {
        "allOf": [
          {
            "$ref": "#/components/schemas/chatCompletionRequestMessage"
          },
          {
            "type": "object",
            "nullable": true,
            "properties": {
              "tool_call_id": {
                "type": "string",
                "description": "Tool call that this message is responding to."
              },
              "content": {
                "type": "string",
                "description": "The contents of the message.",
                "nullable": true
              }
            }
          }
        ],
        "required": [
          "tool_call_id",
          "content"
        ]
      },

AzureChatExtensionConfiguration

      "azureChatExtensionConfiguration": {
        "required": [
          "type"
        ],
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/components/schemas/azureChatExtensionType"
          }
        },
        "description": "  A representation of configuration data for a single Azure OpenAI chat extension. This will be used by a chat\n  completions request that should use Azure OpenAI chat extensions to augment the response behavior.\n  The use of this configuration is compatible only with Azure OpenAI.",
        "discriminator": {
          "propertyName": "type",
          "mapping": {
            "azure_search": "#/components/schemas/azureSearchChatExtensionConfiguration",
            "azure_cosmos_db": "#/components/schemas/azureCosmosDBChatExtensionConfiguration"
          }
        }
      },

AzureChatExtensionType

      "azureChatExtensionType": {
        "type": "string",
        "description": "  A representation of configuration data for a single Azure OpenAI chat extension. This will be used by a chat\n  completions request that should use Azure OpenAI chat extensions to augment the response behavior.\n  The use of this configuration is compatible only with Azure OpenAI.",
        "enum": [
          "azure_search",
          "azure_cosmos_db"
        ],
        "x-ms-enum": {
          "name": "AzureChatExtensionType",
          "modelAsString": true,
          "values": [
            {
              "name": "azureSearch",
              "value": "azure_search",
              "description": "Represents the use of Azure Search as an Azure OpenAI chat extension."
            },
            {
              "name": "azureCosmosDB",
              "value": "azure_cosmos_db",
              "description": "Represents the use of Azure Cosmos DB as an Azure OpenAI chat extension."
            }
          ]
        }
      },

AzureSearchChatExtensionConfiguration

      "azureSearchChatExtensionConfiguration": {
        "required": [
          "parameters"
        ],
        "description": "A specific representation of configurable options for Azure Search when using it as an Azure OpenAI chat\nextension.",
        "allOf": [
          {
            "$ref": "#/components/schemas/azureChatExtensionConfiguration"
          },
          {
            "properties": {
              "parameters": {
                "$ref": "#/components/schemas/azureSearchChatExtensionParameters"
              }
            }
          }
        ],
        "x-ms-discriminator-value": "azure_search"
      },

AzureCosmosDBChatExtensionConfiguration

      "azureCosmosDBChatExtensionConfiguration": {
        "required": [
          "parameters"
        ],
        "description": "A specific representation of configurable options for Azure Cosmos DB when using it as an Azure OpenAI chat\nextension.",
        "allOf": [
          {
            "$ref": "#/components/schemas/azureChatExtensionConfiguration"
          },
          {
            "properties": {
              "parameters": {
                "$ref": "#/components/schemas/azureCosmosDBChatExtensionParameters"
              }
            }
          }
        ],
        "x-ms-discriminator-value": "azure_cosmos_db"
      },

AzureCosmosDBChatExtensionParameters

  "azureCosmosDBChatExtensionParameters": {
        "required": [
          "authentication",
          "container_name",
          "database_name",
          "embedding_dependency",
          "fields_mapping",
          "index_name"
        ],
        "type": "object",
        "properties": {
          "authentication": {
            "$ref": "#/components/schemas/onYourDataConnectionStringAuthenticationOptions"
          },
          "top_n_documents": {
            "type": "integer",
            "description": "The configured top number of documents to feature for the configured query.",
            "format": "int32"
          },
          "in_scope": {
            "type": "boolean",
            "description": "Whether queries should be restricted to use of indexed data."
          },
          "strictness": {
            "maximum": 5,
            "minimum": 1,
            "type": "integer",
            "description": "The configured strictness of the search relevance filtering. The higher of strictness, the higher of the precision but lower recall of the answer.",
            "format": "int32"
          },
          "role_information": {
            "type": "string",
            "description": "Give the model instructions about how it should behave and any context it should reference when generating a response. You can describe the assistant's personality and tell it how to format responses. There's a 100 token limit for it, and it counts against the overall token limit."
          },
          "database_name": {
            "type": "string",
            "description": "The MongoDB vCore database name to use with Azure Cosmos DB."
          },
          "container_name": {
            "type": "string",
            "description": "The name of the Azure Cosmos DB resource container."
          },
          "index_name": {
            "type": "string",
            "description": "The MongoDB vCore index name to use with Azure Cosmos DB."
          },
          "fields_mapping": {
            "$ref": "#/components/schemas/azureCosmosDBFieldMappingOptions"
          },
          "embedding_dependency": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/onYourDataEndpointVectorizationSource"
              },
              {
                "$ref": "#/components/schemas/onYourDataDeploymentNameVectorizationSource"
              }
            ]
          }
        },
        "description": "Parameters to use when configuring Azure OpenAI On Your Data chat extensions when using Azure Cosmos DB for\nMongoDB vCore."
      },

OnYourDataConnectionStringAuthenticationOptions

      "onYourDataConnectionStringAuthenticationOptions": {
        "required": [
          "connection_string"
        ],
        "description": "The authentication options for Azure OpenAI On Your Data when using a connection string.",
        "allOf": [
          {
            "$ref": "#/components/schemas/onYourDataAuthenticationOptions"
          },
          {
            "properties": {
              "connection_string": {
                "type": "string",
                "description": "The connection string to use for authentication."
              }
            }
          }
        ],
        "x-ms-discriminator-value": "connection_string"
      },

OnYourDataAuthenticationOptions

      "onYourDataAuthenticationOptions": {
        "required": [
          "type"
        ],
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/components/schemas/onYourDataAuthenticationType"
          }
        },
        "description": "The authentication options for Azure OpenAI On Your Data.",
        "discriminator": {
          "propertyName": "type",
          "mapping": {
            "api_key": "#/components/schemas/onYourDataApiKeyAuthenticationOptions",
            "connection_string": "#/components/schemas/onYourDataConnectionStringAuthenticationOptions",
            "system_assigned_managed_identity": "#/components/schemas/onYourDataSystemAssignedManagedIdentityAuthenticationOptions",
            "user_assigned_managed_identity": "#/components/schemas/onYourDataUserAssignedManagedIdentityAuthenticationOptions"
          }
        }
      },

ChatCompletionResponseFormat

 "chatCompletionResponseFormat": {
        "type": "string",
        "enum": [
          "text",
          "json_object"
        ],
        "default": "text",
        "example": "json_object",
        "nullable": true,
        "description": "Setting to `json_object` enables JSON mode. This guarantees that the message the model generates is valid JSON.",
        "x-ms-enum": {
          "name": "ChatCompletionResponseFormat",
          "modelAsString": true,
          "values": [
            {
              "value": "text",
              "description": "Response format is a plain text string."
            },
            {
              "value": "json_object",
              "description": "Response format is a JSON object."
            }
          ]
        }
      },

ChatCompletionTool

   "chatCompletionTool": {
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/components/schemas/chatCompletionToolType"
          },
          "function": {
            "type": "object",
            "properties": {
              "description": {
                "type": "string",
                "description": "A description of what the function does, used by the model to choose when and how to call the function."
              },
              "name": {
                "type": "string",
                "description": "The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64."
              },
              "parameters": {
                "$ref": "#/components/schemas/chatCompletionFunctionParameters"
              }
            },
            "required": [
              "name",
              "parameters"
            ]
          }
        },
        "required": [
          "type",
          "function"
        ]
      },

ChatCompletionToolType

     "chatCompletionToolType": {
        "type": "string",
        "enum": [
          "function"
        ],
        "description": "The type of the tool. Currently, only `function` is supported.",
        "x-ms-enum": {
          "name": "ChatCompletionToolType",
          "modelAsString": true,
          "values": [
            {
              "value": "function",
              "description": "The tool type is function."
            }
          ]
        }
      },

ChatCompletionFunctionParameters

  "chatCompletionFunctionParameters": {
        "type": "object",
        "description": "The parameters the functions accepts, described as a JSON Schema object. See the [guide](/docs/guides/gpt/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.",
        "additionalProperties": true
      },

ChatCompletionToolChoiceOption

      "chatCompletionToolChoiceOption": {
        "description": "Controls which (if any) function is called by the model. `none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function. Specifying a particular function via `{\"type\": \"function\", \"function\": {\"name\": \"my_function\"}}` forces the model to call that function.",
        "oneOf": [
          {
            "type": "string",
            "description": "`none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function.",
            "enum": [
              "none",
              "auto",
              "required"
            ]
          },
          {
            "$ref": "#/components/schemas/chatCompletionNamedToolChoice"
          }
        ]
      },

ChatCompletionNamedToolChoice

      "chatCompletionNamedToolChoice": {
        "type": "object",
        "description": "Specifies a tool the model should use. Use to force the model to call a specific function.",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "function"
            ],
            "description": "The type of the tool. Currently, only `function` is supported."
          },
          "function": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "description": "The name of the function to call."
              }
            },
            "required": [
              "name"
            ]
          }
        }
      },

@jihyunmoon16 jihyunmoon16 marked this pull request as draft August 24, 2024 05:12
@justinyoo justinyoo marked this pull request as ready for review August 24, 2024 05:47
@justinyoo justinyoo linked an issue Aug 24, 2024 that may be closed by this pull request
@justinyoo
Copy link
Contributor

@jihyunmoon16 아까 낮에 논의한 것 처럼

  • ChatCompletionTool 개체의 function 어트리뷰트는 별도의 클래스로 만들어 보세요.
  • 클래스 👉 JSON 직렬화 테스트 검토해 주세요.
  • JSON 👉 클래스 비직렬화 테스트 검토해 주세요.

@jihyunmoon16
Copy link
Contributor Author

테스트 코드 작성했는데 data_sources 필드는 다형성? 부분도 고려해야 할 것 같고 어떤식으로 클래스를 만들어야 할 지 고민입니다. 테스트도 계속 실패해요. 웹사이트의 examples json 파일을 참고해서 제일 기본인 required 필드만 적용해봤습니다.

코드 구현이 힘든 부분이 이 data sources 파라미터예요.

  "body": {
      "messages": [
        {
          "role": "user",
          "content": "can you tell me how to care for a cat?"
        },
        {
          "role": "assistant",
          "content": "Content of the completion [doc1].",
          "context": {
            "intent": "cat care"
          }
        },
        {
          "role": "user",
          "content": "how about dog?"
        }
      ],
      "data_sources": [
        {
          "type": "azure_search",
          "parameters": {
            "endpoint": "https://your-search-endpoint.search.windows.net/",
            "authentication": {
              "type": "user_assigned_managed_identity",
              "managed_identity_resource_id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{resource-name}"
            },
            "index_name": "{index name}",
            "query_type": "vector",
            "embedding_dependency": {
              "type": "deployment_name",
              "deployment_name": "{embedding deployment name}"
            },
            "in_scope": true,
            "top_n_documents": 5,
            "strictness": 3,
            "role_information": "You are an AI assistant that helps people find information.",
            "fields_mapping": {
              "content_fields_separator": "\\n",
              "content_fields": [
                "content"
              ],
              "filepath_field": "filepath",
              "title_field": "title",
              "url_field": "url",
              "vector_fields": [
                "contentvector"
              ]
            }
          }
        }
      ]
    }

테스트 코드를 작성하다가 궁금한 부분이 있는데 createChatCompletionRequest의 경우 request payload 여서 비직렬화만 할 것 같은데 이때도 직렬화 테스트가 필요할까요?

@justinyoo justinyoo marked this pull request as draft August 31, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Chat Completions] Request payload definition
2 participants