Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 2.05 KB

File metadata and controls

72 lines (53 loc) · 2.05 KB

Microsoft.Extensions.AI - OpenAI Web API Example

This project contains a minimal Web API that show how to use the OpenAI reference implementation in the Microsoft.Extensions.AI.OpenAI NuGet package.

Prerequisites

Setup

  1. In the OpenAIWebAPI project directory, create a file called appsettings.local.json with the following content.

    {
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft.AspNetCore": "Warning"
            }
        },
        "AllowedHosts": "*",
        "AI": {
            "OpenAI": {
                "Key": "YOUR-API-KEY",
                "Chat": {
                    "ModelId": "gpt-4o-mini"
                },
                "Embedding": {
                    "ModelId": "text-embedding-3-small"
                }
            }
        }
    }
  2. Replace the value of the Key with your OpenAI API key.

Quick Start

Visual Studio

  1. Open the OpenAIExamples.sln solution
  2. Set OpenAIWebAPI as the startup project.
  3. Press F5.

Visual Studio Code

  1. Open your terminal

  2. Navigate to the OpenAIWebAPI project directory

  3. Run the applicaton using dotnet run

    dotnet run
    

Test your application

PowerShell

Chat
$response = Invoke-RestMethod -Uri 'http://localhost:5208/chat' -Method Post -Headers @{'Content-Type'='application/json'} -Body '"What is AI?"'; $response.message.contents.text

Embeddings

$response = Invoke-RestMethod -Uri 'http://localhost:5208/embedding' -Method Post -Headers @{'Content-Type'='application/json'} -Body '"What is AI?"'; $response.vector