openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.56k stars 466 forks source link

Azure Chat with Custom Data problem #348

Closed jmoleiroamc closed 3 months ago

jmoleiroamc commented 3 months ago

Hi!

I'm trying to use a Chat with custom Data on Azure. The documentation, says that I must add the dataSource parameter to the options when I create a chat. This is the sample JSON

{
  "dataSources": [
    {
      "type": "AzureCognitiveSearch",
      "parameters": {
        "endpoint": "'$search_endpoint'",
        "indexName": "'$search_index'",
        "semanticConfiguration": "default",
        "queryType": "simple",
        "fieldsMapping": {},
        "inScope": true,
        "roleInformation": "You are an AI assistant that helps people find information.",
        "filter": null,
        "strictness": 3,
        "topNDocuments": 5,
        "key": "'$search_key'"
      }
    }
  ],
  "messages": [
    {
      "role": "system",
      "content": "You are an AI assistant that helps people find information."
    }
  ],
  "deployment": "my-deployment",
  "temperature": 0,
  "top_p": 1,
  "max_tokens": 800,
  "stop": null,
  "stream": true,
  "frequency_penalty": 0,
  "presence_penalty": 0,
  "azureSearchEndpoint": "https://my-chat-url.search.windows.net",
  "azureSearchKey": "***",
  "azureSearchIndexName": "my-index"
}

This is how I add the option

$opts['dataSources'] = [];

$_option = [];
$_option["type"] = "AzureCognitiveSearch";
$_option["parameters"] = [];

$_option["parameters"]['endpoint'] = $ds_endpoint;
$_option["parameters"]['indexName'] = $ds_index;
$_option["parameters"]['semanticConfiguration'] = $semantic_configuration;
$_option["parameters"]['queryType'] = $query_type;
$_option["parameters"]['fieldsMapping'] = $fields_mapping;
$_option["parameters"]['inScope'] = $use_custom_data_only;
$_option["parameters"]['roleInformation'] = $prompt;
$_option["parameters"]['filter'] = $filter;
$_option["parameters"]['strictness'] = $strictness;
$_option["parameters"]['topNDocuments'] = $top_n_documents;
$_option["parameters"]['key'] = $ds_key;

$opts['azureSearchEndpoint'] = $ds_endpoint;
$opts['azureSearchIndexName'] = $ds_index;
$opts['azureSearchKey'] = $ds_key;

$result = $open_ai->chat()->create($opts);   

But Ii get an exception:

Unrecognized request arguments supplied: azureSearchEndpoint, azureSearchIndexName, azureSearchKey, dataSources

The problem only happens when I try to use this feature. Any idea?

Regards

J

jmoleiroamc commented 3 months ago

I found out the problem, and it's in the client URL. I created the client like this

    $open_ai = OpenAI::factory()
    ->withBaseUri($azure_endpoint)
    ->withHttpHeader('api-key', $azure_openai_api_key)
    ->withQueryParam('api-version', $azure_openai_api_version)
    ->make();

But for using with custom data, the url is slightly different. Instead of /deployments/{my_deployment}/chat/completions, in this case I need to use /deployments/{my_deployment}/extensions/chat/completions (note the added /extensions)

    $open_ai = OpenAI::factory()
    ->withBaseUri($azure_endpoint."/extensions")
    ->withHttpHeader('api-key', $azure_openai_api_key)
    ->withQueryParam('api-version', $azure_openai_api_version)
    ->make();

With this change I get a data validation error from the API, but that's on my end.

So, if you get the error Unrecognized request arguments supplied, double check the service url.

I hope this help someone with this problem.

Regards

J