|
| 1 | +--- |
| 2 | +title: AI Server API Usage |
| 3 | +--- |
| 4 | + |
| 5 | +# AI Server API Usage |
| 6 | + |
| 7 | +AI Server provides a unified API to process requests for AI services to access LLMs, Image Generation, Transcription, and more. The API is designed to be simple to use and easy to integrate into your applications providing many supported languages and frameworks. |
| 8 | + |
| 9 | +## Making a Chat Request |
| 10 | + |
| 11 | +To make a chat request to AI Server, you can use the `/api/CreateOpenAiChat` endpoint. This endpoint requires a `CreateOpenAiChat` request DTO that contains a property matching the `OpenAI` API. |
| 12 | + |
| 13 | +```csharp |
| 14 | +var response = client.Post(new CreateOpenAiChat { |
| 15 | + Request = new OpenAiChat { |
| 16 | + Model = "gpt-3.5-turbo", |
| 17 | + Messages = new List<OpenAiMessage> { |
| 18 | + new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." }, |
| 19 | + new OpenAiMessage { Role = "user", Content = "How do I ..." } |
| 20 | + } |
| 21 | + } |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +Additional optional features on the request to enhance the usage of AI Server include: |
| 26 | + |
| 27 | +- **RefId**: A unique identifier for the request specified by the client to more easily track the progress of the request. |
| 28 | +- **Provider**: Force the request to use a specific provider, overriding the selection logic. |
| 29 | +- **ReplyTo**: A HTTP URL to send the response to on completion of the request. |
| 30 | +- **Tag**: A tag to help categorize the request for easier tracking. |
| 31 | + |
| 32 | +## Using the AI Server Request DTOs with other OpenAI compatible APIs |
| 33 | + |
| 34 | +One advantage of using AI Server is that it provides a common set of request DTOs in 11 different languages that are compatible with OpenAI's API. This allows you to switch between OpenAI and AI Server without changing your client code. |
| 35 | +This means you can switch to using typed APIs in your preferred language with your existing service providers OpenAI compatible APIs, and optionally switch to AI Server when you're ready to self-host your AI services for better value. |
| 36 | + |
| 37 | +```csharp |
| 38 | +// Using OpenAI API |
| 39 | +var request = new OpenAiChat { |
| 40 | + Model = "gpt-3.5-turbo", |
| 41 | + Messages = new List<OpenAiMessage> { |
| 42 | + new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." }, |
| 43 | + new OpenAiMessage { Role = "user", Content = "How do I ..." } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +var response = await client.PostAsync("https://api.openai.com/v1/", request); |
| 48 | +``` |
0 commit comments