Skip to content

Question: get started with AzureOpenAIย #181

Closed

Description

@WeihanLi

Great thanks for the project, I'm trying to use it with Azure OpenAI service, but do not know how to find the DeploymentId, could you please help, thanks

Activity

  1. WeihanLi commented on Mar 18, 2023

    @WeihanLi
    ContributorAuthor

    I'm using the following code for the AzureOpenAI

    var options = new OpenAiOptions()
    {
    	BaseDomain = " https://spark.openai.azure.com",
    	ApiKey = apiKey,
    	DeploymentId = "gpt35",
    	ResourceName = "spark",
    	ProviderType = ProviderType.Azure,
    };
    var openAiService = new OpenAIService(options);
    var completionResult = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
    {
    	Messages = new List<ChatMessage>
    	{
    		ChatMessage.FromSystem("You are a helpful assistant."),
    		ChatMessage.FromUser("What's new in .NET 6")
    	},
    	Model = Models.ChatGpt3_5Turbo // "gpt-35-turbo" also not working
    });

    completionResult has error as follows:

    {"Code":"404","Message":"Resource not found"}
    

    While I could call the AzureOpenAI API by following the rest api docs, after some research, I found that the API is not the same, wondering that maybe there's something wrong with my way calling AzureOpenAI.

    For details, the raw Azure OpenAI request likes below:

    POST https://spark.openai.azure.com/openai/deployments/gpt35/completions?api-version=2022-12-01
    Host: spark.openai.azure.com
    Content-Type: application/json
    api-key: <api-key>
    
    {
      "prompt": "<|im_start|>system\nThe system is a helpful AI assistant.\n<|im_end|>\n<|im_start|>user\nWhat's new with .NET 6?\n<|im_end|>\n<|im_start|>assistant",
      "max_tokens": 800,
      "temperature": 1,
      "frequency_penalty": 0,
      "presence_penalty": 0,
      "top_p": 0.95,
      "stop": ["<|im_end|>"]
    }
    

    The request fired with before code like follows:

    POST https://spark.openai.azure.com/openai/deployments/gpt35/chat/completions?api-version=2022-12-01 HTTP/1.1
    Host: spark.openai.azure.com
    api-key: <api-key>
    Content-Type: application/json; charset=utf-8
    
    {"messages":[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"What\u0027s new in .NET 6"}],"model":"gpt-3.5-turbo"}
    

    And besides, the model deployment I created as follows:

    image

  2. kayhantolga commented on Mar 18, 2023

    @kayhantolga
    Member

    I recently got access to Azure OpenAI. I couldn't find a chance to try services before releasing the SDK. I will check this issue as soon as possible.
    I have used this documentation as a reference, it may help you too.
    https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference

  3. realchrisparker commented on Mar 20, 2023

    @realchrisparker

    I am tracking this also as I started using this library today with Azure OpenAI service and have yet to get it working.

  4. changed the title [-]Question: where to find the DeploymentId for AzureOpenAI[/-] [+]Question: get started with AzureOpenAI[/+] on Mar 21, 2023
  5. added this to the 8.4.2 milestone on Mar 21, 2023
  6. kayhantolga commented on Mar 21, 2023

    @kayhantolga
    Member

    It appears that Azure has decided to implement a different approach from that of the OpenAI team. Additionally, they have chosen different model names. ๐Ÿ˜ฎโ€๐Ÿ’จ
    I will address these issues in the next version.

    https://learn.microsoft.com/en-gb/azure/cognitive-services/openai/how-to/chatgpt

  7. realchrisparker commented on Mar 21, 2023

    @realchrisparker

    I did some quick test code with Azure OpenAI and this worked.

    CompletionMessage completionMessage = new( message );
    HttpRequestMessage request = new()
    {
    Method = HttpMethod.Post,
    RequestUri = new Uri( $"https://{resourceName}.openai.azure.com/openai/deployments/{deploymentName}/completions?api-version=2022-12-01" ),
    Content = new StringContent( completionMessage.ToString(), Encoding.UTF8, "application/json" ),
    };
    request.Headers.Clear();
    request.Headers.Accept.Add( new MediaTypeWithQualityHeaderValue( "application/json" ) );
    request.Headers.Add( "api-key", apiKey );

                var response = await _httpClient.SendAsync( request );
    

    Not saying this is the code you should use but giving guidance on what normally works with httpclient.
    The completionMessage is a quick class I created mimicking the following JSON.
    {
    "prompt": "",
    "temperature": 0.7,
    "top_p": 0.95,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "max_tokens": 800,
    "stop": [
    "<|im_end|>"
    ]
    }

  8. modified the milestones: 8.4.2, 8.4.3 on Mar 28, 2023
  9. doggy8088 commented on Apr 1, 2023

    @doggy8088
    Contributor

    @kayhantolga May I ask which Betalgo.OpenAI.GPT3 version can I use for AOAI?

    Also, I think the ResourceName property is not necessary for OpenAiOptions in AOAI.

  10. magols commented on Apr 13, 2023

    @magols

    Hello, I just got access to Azure OpenAI GPT-4 at work today and got my existing app (OpenAI API 3.5-turbo) working by changing the Program.cs to

    builder.Services.AddOpenAIService(options =>
    {
        options.ProviderType = ProviderType.Azure;
        options.ResourceName = builder.Configuration["OpenAIServiceOptions:ResourceName"];
        options.DeploymentId = builder.Configuration["OpenAIServiceOptions:DeploymentId"];
        options.ApiVersion = "2023-03-15-preview";
        options.ApiKey = builder.Configuration["OpenAIServiceOptions:ApiKey"];
    
    });
    

    and changing the model from Models.ChatGpt3_5Turbo to "gpt-4" (the 8K version) to like this.

    
     var stream =  Ai.ChatCompletion.CreateCompletionAsStream(new ChatCompletionCreateRequest()
                    {
                        Model = "gpt-4",
                        MaxTokens = 2000,
                        Temperature = 0.9f,
                        Messages = conv.Messages.Select(m => new ChatMessage(m.Role, m.Content)).ToList()
    
                    });
    
    

    Initially I got a 404 Not Found until I spied on the Azure OpenAI Studio Chat playground and saw it was using a different API version. When plugging in the "2023-03-15-preview" as version and also using the new model string I now get chat completions!

  11. modified the milestones: 8.4.3, 6.8.6 on May 8, 2023
  12. remixtedi commented on May 9, 2023

    @remixtedi

    If the client is configured to use Azure OpenAI service then Image generation doesn't work because it is not available on Azure, is there a way to have the client configured for both platforms? to use Azure for chat completions and OpenAI Dalle for images?

  13. kayhantolga commented on May 11, 2023

    @kayhantolga
    Member

    Released a new version, Please have a look at the wiki page.
    https://github.com/betalgo/openai/wiki/Azure-OpenAI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions